JoelJebaraj93's picture
Update app.py
bc2b5e2 verified
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
API_KEY = "jlCyZJCZnB22qfdRJFMO71pZLHioHbNCEQa7qM10BnUVfJQ6xrXGRZF66Bj6"
st.set_page_config("Live Cricket Score", layout="wide")
st.title("🏏 Live Cricket Score")
# Fetch today's matches (even if not fully live)
@st.cache_data(ttl=60)
def fetch_today_matches():
today = datetime.today().strftime('%Y-%m-%d')
url = (
f"https://cricket.sportmonks.com/api/v2.0/fixtures/date/{today}"
f"?api_token={API_KEY}&include=localteam,visitorteam,batting,bowling,balls.commentaries"
)
response = requests.get(url)
return response.json().get("data", [])
matches = fetch_today_matches()
if not matches:
st.warning("No matches found for today.")
else:
for m in matches:
try:
lt = m["localteam"]["name"]
vt = m["visitorteam"]["name"]
lt_logo = m["localteam"]["image_path"]
vt_logo = m["visitorteam"]["image_path"]
status = m["status"]
match_type = m["type"]
is_live = m["live"]
st.markdown(f"## {lt} πŸ†š {vt} β€” *{match_type}*")
st.image([lt_logo, vt_logo], width=100)
st.write(f"**Status:** {status}")
if is_live:
st.success("πŸ”΄ LIVE NOW")
# Batting
batting = m.get("batting", {}).get("data", [])
if batting:
st.markdown("### 🏏 Batting")
bat_df = pd.DataFrame(batting)[["team_id", "player_id", "runs", "balls", "fours", "sixes"]]
st.dataframe(bat_df)
else:
st.info("No batting data yet.")
# Bowling
bowling = m.get("bowling", {}).get("data", [])
if bowling:
st.markdown("### 🎯 Bowling")
bowl_df = pd.DataFrame(bowling)[["team_id", "player_id", "overs", "runs", "wickets"]]
st.dataframe(bowl_df)
else:
st.info("No bowling data yet.")
# Commentary
balls = m.get("balls", {}).get("data", [])
if balls:
st.markdown("### πŸ“£ Latest Commentary")
for b in balls[-5:]:
over = b.get("over")
ball = b.get("ball")
comm = b.get("commentaries", [{}])[0].get("text", "")
st.markdown(f"**{over}.{ball} ➜** {comm}")
else:
st.info("No ball-by-ball data yet.")
st.markdown("---")
except Exception as e:
st.error(f"Error: {e}")