Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,47 @@ from streamlit_autorefresh import st_autorefresh
|
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from datetime import datetime
|
| 6 |
-
import pandas as pd
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
BASE_URL = "https://www.cricbuzz.com"
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# -------------------- LIVE IPL FUNCTION --------------------
|
| 18 |
def fetch_live_ipl_matches():
|
| 19 |
url = f"{BASE_URL}/cricket-match/live-scores"
|
| 20 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 21 |
-
|
| 22 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
| 23 |
|
| 24 |
live_matches = []
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
for section in match_sections:
|
| 28 |
header = section.find("h2", class_="cb-lv-grn-strip")
|
| 29 |
if not header or not header.find("span", class_="cb-plus-ico cb-ico-live-stream"):
|
| 30 |
continue
|
|
@@ -34,85 +53,94 @@ def fetch_live_ipl_matches():
|
|
| 34 |
title_tag = block.find("h3", class_="cb-lv-scr-mtch-hdr")
|
| 35 |
match_title = title_tag.get_text(strip=True).rstrip(',') if title_tag else "N/A"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
status_tag = block.find("div", class_="cb-text-live")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
return live_matches
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
st.markdown("---")
|
| 94 |
-
|
| 95 |
-
st.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
st.markdown("---")
|
| 113 |
-
|
| 114 |
-
st.subheader("π All Recent Results")
|
| 115 |
-
for result in recent_results:
|
| 116 |
-
st.markdown(f"- {result}")
|
| 117 |
-
else:
|
| 118 |
-
st.warning("β No recent results found.")
|
|
|
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
| 7 |
+
# Dynamically get the current year
|
| 8 |
+
CURRENT_YEAR = datetime.now().year
|
| 9 |
|
| 10 |
+
# Define the unique code for the year (e.g., 2025 -> 9237)
|
| 11 |
+
YEAR_CODE_MAPPING = {
|
| 12 |
+
2025: "9237",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Get the unique code for the current year
|
| 16 |
+
YEAR_CODE = YEAR_CODE_MAPPING.get(CURRENT_YEAR, "default_code")
|
| 17 |
|
| 18 |
+
# Construct URLs
|
| 19 |
BASE_URL = "https://www.cricbuzz.com"
|
| 20 |
+
IPL_SERIES_URL = f"{BASE_URL}/cricket-series/{YEAR_CODE}/indian-premier-league-{CURRENT_YEAR}"
|
| 21 |
+
IPL_MATCHES_URL = f"{IPL_SERIES_URL}/matches"
|
| 22 |
+
LIVE_KEYWORDS = ["won the toss", "opt", "elect", "need", "needs", "chose to"]
|
| 23 |
+
|
| 24 |
+
st.markdown("""
|
| 25 |
+
<style>
|
| 26 |
+
.main > div { max-width: 100%; padding-left: 1rem; padding-right: 1rem; }
|
| 27 |
+
</style>
|
| 28 |
+
""", unsafe_allow_html=True)
|
| 29 |
+
|
| 30 |
+
st.markdown(f"""
|
| 31 |
+
<h1 style='text-align: center;'>π IPL Score Stream</h1>
|
| 32 |
+
<p style='text-align: center; font-size: 16px; color: gray;'>
|
| 33 |
+
β±οΈ Last updated: {datetime.now().strftime('%H:%M:%S')}
|
| 34 |
+
</p>
|
| 35 |
+
""", unsafe_allow_html=True)
|
| 36 |
+
|
| 37 |
+
st_autorefresh(interval=5000, limit=None, key="ipl_autorefresh")
|
| 38 |
|
|
|
|
| 39 |
def fetch_live_ipl_matches():
|
| 40 |
url = f"{BASE_URL}/cricket-match/live-scores"
|
| 41 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 42 |
+
soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
|
|
|
|
| 43 |
|
| 44 |
live_matches = []
|
| 45 |
+
sections = soup.find_all("div", class_="cb-col cb-col-100 cb-plyr-tbody cb-rank-hdr cb-lv-main")
|
| 46 |
+
for section in sections:
|
|
|
|
| 47 |
header = section.find("h2", class_="cb-lv-grn-strip")
|
| 48 |
if not header or not header.find("span", class_="cb-plus-ico cb-ico-live-stream"):
|
| 49 |
continue
|
|
|
|
| 53 |
title_tag = block.find("h3", class_="cb-lv-scr-mtch-hdr")
|
| 54 |
match_title = title_tag.get_text(strip=True).rstrip(',') if title_tag else "N/A"
|
| 55 |
|
| 56 |
+
team1 = block.find("div", class_="cb-hmscg-bwl-txt")
|
| 57 |
+
score1 = team1.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team1 else "N/A"
|
| 58 |
|
| 59 |
+
team2 = block.find("div", class_="cb-hmscg-bat-txt")
|
| 60 |
+
score2 = team2.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team2 else "N/A"
|
| 61 |
|
| 62 |
status_tag = block.find("div", class_="cb-text-live")
|
| 63 |
+
status = status_tag.text.strip() if status_tag else ""
|
| 64 |
+
|
| 65 |
+
if any(k in status.lower() for k in LIVE_KEYWORDS):
|
| 66 |
+
live_matches.append({
|
| 67 |
+
"Match Title": match_title,
|
| 68 |
+
"Batting Score": score2,
|
| 69 |
+
"Bowling Score": score1,
|
| 70 |
+
"Status": status
|
| 71 |
+
})
|
| 72 |
return live_matches
|
| 73 |
|
| 74 |
+
def get_recent_news():
|
| 75 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 76 |
+
soup = BeautifulSoup(requests.get(IPL_SERIES_URL, headers=headers).text, "html.parser")
|
| 77 |
+
tags = soup.find_all("a", class_="cb-nws-hdln-ancr")
|
| 78 |
+
return [(tag.text.strip(), BASE_URL + tag["href"]) for tag in tags[:10]]
|
| 79 |
+
|
| 80 |
+
def get_recent_match_results():
|
| 81 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 82 |
+
soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
|
| 83 |
+
result_blocks = soup.find_all("a", href=True)
|
| 84 |
+
|
| 85 |
+
results = []
|
| 86 |
+
for tag in result_blocks:
|
| 87 |
+
if "won by" in tag.text.lower():
|
| 88 |
+
desc = tag.text.strip()
|
| 89 |
+
link = BASE_URL + tag["href"]
|
| 90 |
+
parent = tag.find_parent("div", class_="cb-col-100 cb-col")
|
| 91 |
+
date_tag = parent.find("div", class_="schedule-date") if parent else None
|
| 92 |
+
date = date_tag.text.strip() if date_tag else "β"
|
| 93 |
+
results.append((date, desc, link))
|
| 94 |
+
|
| 95 |
+
return results[-5:][::-1] if results else []
|
| 96 |
+
|
| 97 |
+
def get_upcoming_matches():
|
| 98 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 99 |
+
soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
|
| 100 |
+
matches = []
|
| 101 |
+
|
| 102 |
+
for block in soup.select("div.cb-series-matches"):
|
| 103 |
+
title_tag = block.select_one("a.text-hvr-underline")
|
| 104 |
+
venue_tag = block.select_one("div.text-gray")
|
| 105 |
+
time_tag = block.select_one("a.cb-text-upcoming")
|
| 106 |
+
|
| 107 |
+
if title_tag and venue_tag and time_tag:
|
| 108 |
+
title = title_tag.text.strip()
|
| 109 |
+
venue = venue_tag.text.strip()
|
| 110 |
+
start = time_tag.text.strip()
|
| 111 |
+
link = BASE_URL + title_tag["href"]
|
| 112 |
+
matches.append(f"- [{title} at {venue}]({link})")
|
| 113 |
+
|
| 114 |
+
return matches[:5]
|
| 115 |
+
|
| 116 |
+
def left_panel():
|
| 117 |
+
st.subheader("π’ Live Matches")
|
| 118 |
+
matches = fetch_live_ipl_matches()
|
| 119 |
+
if matches:
|
| 120 |
+
for match in matches:
|
| 121 |
+
st.markdown(f"#### π₯ {match['Match Title']}")
|
| 122 |
+
st.markdown(f"π **Batting Side**: {match['Batting Score']}")
|
| 123 |
+
st.markdown(f"π― **Bowling Side**: {match['Bowling Score']}")
|
| 124 |
+
st.markdown(f"π£ **Status**: {match['Status']}")
|
| 125 |
+
st.markdown("---")
|
| 126 |
+
else:
|
| 127 |
+
st.warning("β No live IPL matches currently.")
|
| 128 |
st.markdown("---")
|
| 129 |
+
|
| 130 |
+
st.subheader("π° Latest IPL News")
|
| 131 |
+
for title, link in get_recent_news():
|
| 132 |
+
st.markdown(f"- [{title}]({link})")
|
| 133 |
+
|
| 134 |
+
def right_panel():
|
| 135 |
+
st.subheader("π
Recent Match Results")
|
| 136 |
+
for _, desc, link in get_recent_match_results():
|
| 137 |
+
st.markdown(f"- [{desc}]({link})")
|
| 138 |
+
|
| 139 |
+
st.divider()
|
| 140 |
+
st.subheader("ποΈ Upcoming Matches")
|
| 141 |
+
for line in get_upcoming_matches():
|
| 142 |
+
st.markdown(line)
|
| 143 |
+
|
| 144 |
+
left_col, right_col = st.columns([5, 5])
|
| 145 |
+
with left_col: left_panel()
|
| 146 |
+
with right_col: right_panel()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|