Spaces:
Sleeping
Sleeping
| 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) | |
| 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}") | |