DKethan commited on
Commit
bc8aa30
Β·
verified Β·
1 Parent(s): 329c48a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -4,29 +4,25 @@ 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;'>
@@ -34,34 +30,39 @@ st.markdown(f"""
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
50
-
51
  match_blocks = section.find_all("div", class_="cb-mtch-lst")
52
  for block in match_blocks:
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,
@@ -81,7 +82,6 @@ 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():
@@ -91,28 +91,24 @@ def get_recent_match_results():
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()
@@ -126,7 +122,6 @@ def left_panel():
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})")
@@ -135,12 +130,12 @@ 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()
 
4
  from bs4 import BeautifulSoup
5
  from datetime import datetime
6
 
7
+ # Streamlit page config
8
+ st.set_page_config(
9
+ page_title="IPL Score Stream",
10
+ layout="wide", # allows full width layout
11
+ initial_sidebar_state="collapsed"
12
+ )
13
+
14
+ # Custom CSS to center the content
 
 
 
 
 
 
 
 
 
15
  st.markdown("""
16
  <style>
17
+ div[data-testid="stAppViewContainer"] {
18
+ max-width: 1100px;
19
+ margin: 0 auto;
20
+ padding: 2rem 1rem;
21
+ }
22
  </style>
23
  """, unsafe_allow_html=True)
24
 
25
+ # Header
26
  st.markdown(f"""
27
  <h1 style='text-align: center;'>🏏 IPL Score Stream</h1>
28
  <p style='text-align: center; font-size: 16px; color: gray;'>
 
30
  </p>
31
  """, unsafe_allow_html=True)
32
 
33
+ # Auto-refresh every 5 seconds
34
  st_autorefresh(interval=5000, limit=None, key="ipl_autorefresh")
35
 
36
+ # Constants
37
+ CURRENT_YEAR = datetime.now().year
38
+ YEAR_CODE_MAPPING = {2025: "9237"}
39
+ YEAR_CODE = YEAR_CODE_MAPPING.get(CURRENT_YEAR, "default_code")
40
+ BASE_URL = "https://www.cricbuzz.com"
41
+ IPL_SERIES_URL = f"{BASE_URL}/cricket-series/{YEAR_CODE}/indian-premier-league-{CURRENT_YEAR}"
42
+ IPL_MATCHES_URL = f"{IPL_SERIES_URL}/matches"
43
+ LIVE_KEYWORDS = ["won the toss", "opt", "elect", "need", "needs", "chose to"]
44
+
45
+ # Data Fetch Functions
46
  def fetch_live_ipl_matches():
47
  url = f"{BASE_URL}/cricket-match/live-scores"
48
  headers = {"User-Agent": "Mozilla/5.0"}
49
  soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
 
50
  live_matches = []
51
  sections = soup.find_all("div", class_="cb-col cb-col-100 cb-plyr-tbody cb-rank-hdr cb-lv-main")
52
  for section in sections:
53
  header = section.find("h2", class_="cb-lv-grn-strip")
54
  if not header or not header.find("span", class_="cb-plus-ico cb-ico-live-stream"):
55
  continue
 
56
  match_blocks = section.find_all("div", class_="cb-mtch-lst")
57
  for block in match_blocks:
58
  title_tag = block.find("h3", class_="cb-lv-scr-mtch-hdr")
59
  match_title = title_tag.get_text(strip=True).rstrip(',') if title_tag else "N/A"
 
60
  team1 = block.find("div", class_="cb-hmscg-bwl-txt")
61
  score1 = team1.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team1 else "N/A"
 
62
  team2 = block.find("div", class_="cb-hmscg-bat-txt")
63
  score2 = team2.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team2 else "N/A"
 
64
  status_tag = block.find("div", class_="cb-text-live")
65
  status = status_tag.text.strip() if status_tag else ""
 
66
  if any(k in status.lower() for k in LIVE_KEYWORDS):
67
  live_matches.append({
68
  "Match Title": match_title,
 
82
  headers = {"User-Agent": "Mozilla/5.0"}
83
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
84
  result_blocks = soup.find_all("a", href=True)
 
85
  results = []
86
  for tag in result_blocks:
87
  if "won by" in tag.text.lower():
 
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
  return results[-5:][::-1] if results else []
95
 
96
  def get_upcoming_matches():
97
  headers = {"User-Agent": "Mozilla/5.0"}
98
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
99
  matches = []
 
100
  for block in soup.select("div.cb-series-matches"):
101
  title_tag = block.select_one("a.text-hvr-underline")
102
  venue_tag = block.select_one("div.text-gray")
103
  time_tag = block.select_one("a.cb-text-upcoming")
 
104
  if title_tag and venue_tag and time_tag:
105
  title = title_tag.text.strip()
106
  venue = venue_tag.text.strip()
 
107
  link = BASE_URL + title_tag["href"]
108
  matches.append(f"- [{title} at {venue}]({link})")
 
109
  return matches[:5]
110
 
111
+ # UI Layout
112
  def left_panel():
113
  st.subheader("🟒 Live Matches")
114
  matches = fetch_live_ipl_matches()
 
122
  else:
123
  st.warning("❌ No live IPL matches currently.")
124
  st.markdown("---")
 
125
  st.subheader("πŸ“° Latest IPL News")
126
  for title, link in get_recent_news():
127
  st.markdown(f"- [{title}]({link})")
 
130
  st.subheader("πŸ“… Recent Match Results")
131
  for _, desc, link in get_recent_match_results():
132
  st.markdown(f"- [{desc}]({link})")
 
133
  st.divider()
134
  st.subheader("πŸ—“οΈ Upcoming Matches")
135
  for line in get_upcoming_matches():
136
  st.markdown(line)
137
 
138
+ # 50/50 Layout
139
  left_col, right_col = st.columns([5, 5])
140
  with left_col: left_panel()
141
+ with right_col: right_panel()