DKethan commited on
Commit
9ec1197
Β·
verified Β·
1 Parent(s): 2cf2514

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -79
app.py CHANGED
@@ -4,57 +4,10 @@ import requests
4
  from bs4 import BeautifulSoup
5
  from datetime import datetime
6
 
7
- # -------------------- PAGE CONFIG --------------------
8
- st.set_page_config(
9
- page_title="IPL Score Stream",
10
- layout="wide",
11
- initial_sidebar_state="collapsed"
12
- )
13
-
14
- # -------------------- CUSTOM CSS FOR CENTERING & STYLING --------------------
15
- st.markdown("""
16
- <style>
17
- /* Center the entire app */
18
- div[data-testid="stAppViewContainer"] {
19
- max-width: 1000px;
20
- margin: 0 auto;
21
- padding: 2rem 2rem;
22
- }
23
-
24
- /* Padding between columns */
25
- div[data-testid="column"] {
26
- padding: 0 1rem;
27
- }
28
-
29
- /* Header spacing tweaks */
30
- .stMarkdown h1 {
31
- text-align: center;
32
- margin-bottom: 0.3rem;
33
- }
34
-
35
- .stMarkdown h2, .stMarkdown h3 {
36
- margin-top: 1rem;
37
- margin-bottom: 0.5rem;
38
- }
39
-
40
- /* Bullet lists spacing */
41
- .stMarkdown ul {
42
- line-height: 1.5rem;
43
- }
44
- </style>
45
- """, unsafe_allow_html=True)
46
-
47
- # -------------------- HEADER --------------------
48
- st.markdown(f"""
49
- <h1>🏏 IPL Score Stream</h1>
50
- <p style='text-align: center; font-size: 16px; color: gray;'>
51
- ⏱️ Last updated: {datetime.now().strftime('%H:%M:%S')}
52
- </p>
53
- """, unsafe_allow_html=True)
54
 
55
  st_autorefresh(interval=5000, limit=None, key="ipl_autorefresh")
56
 
57
- # -------------------- CONSTANTS --------------------
58
  CURRENT_YEAR = datetime.now().year
59
  YEAR_CODE_MAPPING = {2025: "9237"}
60
  YEAR_CODE = YEAR_CODE_MAPPING.get(CURRENT_YEAR, "default_code")
@@ -63,27 +16,32 @@ IPL_SERIES_URL = f"{BASE_URL}/cricket-series/{YEAR_CODE}/indian-premier-league-{
63
  IPL_MATCHES_URL = f"{IPL_SERIES_URL}/matches"
64
  LIVE_KEYWORDS = ["won the toss", "opt", "elect", "need", "needs", "chose to"]
65
 
66
- # -------------------- DATA FUNCTIONS --------------------
67
  def fetch_live_ipl_matches():
68
  url = f"{BASE_URL}/cricket-match/live-scores"
69
  headers = {"User-Agent": "Mozilla/5.0"}
70
  soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
 
71
  live_matches = []
72
  sections = soup.find_all("div", class_="cb-col cb-col-100 cb-plyr-tbody cb-rank-hdr cb-lv-main")
73
  for section in sections:
74
  header = section.find("h2", class_="cb-lv-grn-strip")
75
  if not header or not header.find("span", class_="cb-plus-ico cb-ico-live-stream"):
76
  continue
 
77
  match_blocks = section.find_all("div", class_="cb-mtch-lst")
78
  for block in match_blocks:
79
  title_tag = block.find("h3", class_="cb-lv-scr-mtch-hdr")
80
  match_title = title_tag.get_text(strip=True).rstrip(',') if title_tag else "N/A"
 
81
  team1 = block.find("div", class_="cb-hmscg-bwl-txt")
82
  score1 = team1.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team1 else "N/A"
 
83
  team2 = block.find("div", class_="cb-hmscg-bat-txt")
84
  score2 = team2.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team2 else "N/A"
 
85
  status_tag = block.find("div", class_="cb-text-live")
86
  status = status_tag.text.strip() if status_tag else ""
 
87
  if any(k in status.lower() for k in LIVE_KEYWORDS):
88
  live_matches.append({
89
  "Match Title": match_title,
@@ -103,6 +61,7 @@ def get_recent_match_results():
103
  headers = {"User-Agent": "Mozilla/5.0"}
104
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
105
  result_blocks = soup.find_all("a", href=True)
 
106
  results = []
107
  for tag in result_blocks:
108
  if "won by" in tag.text.lower():
@@ -112,51 +71,54 @@ def get_recent_match_results():
112
  date_tag = parent.find("div", class_="schedule-date") if parent else None
113
  date = date_tag.text.strip() if date_tag else "β€”"
114
  results.append((date, desc, link))
 
115
  return results[-5:][::-1] if results else []
116
 
117
  def get_upcoming_matches():
118
  headers = {"User-Agent": "Mozilla/5.0"}
119
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
120
  matches = []
 
121
  for block in soup.select("div.cb-series-matches"):
122
  title_tag = block.select_one("a.text-hvr-underline")
123
  venue_tag = block.select_one("div.text-gray")
124
  time_tag = block.select_one("a.cb-text-upcoming")
 
125
  if title_tag and venue_tag and time_tag:
126
  title = title_tag.text.strip()
127
  venue = venue_tag.text.strip()
 
128
  link = BASE_URL + title_tag["href"]
129
  matches.append(f"- [{title} at {venue}]({link})")
 
130
  return matches[:5]
131
 
132
- # -------------------- UI PANELS --------------------
133
- def left_panel():
134
- st.subheader("🟒 Live Matches")
135
- matches = fetch_live_ipl_matches()
136
- if matches:
137
- for match in matches:
138
- st.markdown(f"#### πŸ”₯ {match['Match Title']}")
139
- st.markdown(f"🏏 **Batting Side**: {match['Batting Score']}")
140
- st.markdown(f"🎯 **Bowling Side**: {match['Bowling Score']}")
141
- st.markdown(f"πŸ“£ **Status**: {match['Status']}")
142
- st.markdown("---")
143
- else:
144
- st.warning("❌ No live IPL matches currently.")
145
  st.markdown("---")
146
- st.subheader("πŸ“° Latest IPL News")
147
- for title, link in get_recent_news():
148
- st.markdown(f"- [{title}]({link})")
149
-
150
- def right_panel():
151
- st.subheader("πŸ“… Recent Match Results")
152
- for _, desc, link in get_recent_match_results():
153
- st.markdown(f"- [{desc}]({link})")
154
- st.divider()
155
- st.subheader("πŸ—“οΈ Upcoming Matches")
156
- for line in get_upcoming_matches():
157
- st.markdown(line)
158
-
159
- # -------------------- LAYOUT --------------------
160
- left_col, right_col = st.columns([1, 1])
161
- with left_col: left_panel()
162
- with right_col: right_panel()
 
 
 
 
4
  from bs4 import BeautifulSoup
5
  from datetime import datetime
6
 
7
+ st.set_page_config(page_title="IPL Score Stream", layout="centered")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  st_autorefresh(interval=5000, limit=None, key="ipl_autorefresh")
10
 
 
11
  CURRENT_YEAR = datetime.now().year
12
  YEAR_CODE_MAPPING = {2025: "9237"}
13
  YEAR_CODE = YEAR_CODE_MAPPING.get(CURRENT_YEAR, "default_code")
 
16
  IPL_MATCHES_URL = f"{IPL_SERIES_URL}/matches"
17
  LIVE_KEYWORDS = ["won the toss", "opt", "elect", "need", "needs", "chose to"]
18
 
 
19
  def fetch_live_ipl_matches():
20
  url = f"{BASE_URL}/cricket-match/live-scores"
21
  headers = {"User-Agent": "Mozilla/5.0"}
22
  soup = BeautifulSoup(requests.get(url, headers=headers).text, "html.parser")
23
+
24
  live_matches = []
25
  sections = soup.find_all("div", class_="cb-col cb-col-100 cb-plyr-tbody cb-rank-hdr cb-lv-main")
26
  for section in sections:
27
  header = section.find("h2", class_="cb-lv-grn-strip")
28
  if not header or not header.find("span", class_="cb-plus-ico cb-ico-live-stream"):
29
  continue
30
+
31
  match_blocks = section.find_all("div", class_="cb-mtch-lst")
32
  for block in match_blocks:
33
  title_tag = block.find("h3", class_="cb-lv-scr-mtch-hdr")
34
  match_title = title_tag.get_text(strip=True).rstrip(',') if title_tag else "N/A"
35
+
36
  team1 = block.find("div", class_="cb-hmscg-bwl-txt")
37
  score1 = team1.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team1 else "N/A"
38
+
39
  team2 = block.find("div", class_="cb-hmscg-bat-txt")
40
  score2 = team2.find_all("div", class_="cb-ovr-flo")[1].text.strip() if team2 else "N/A"
41
+
42
  status_tag = block.find("div", class_="cb-text-live")
43
  status = status_tag.text.strip() if status_tag else ""
44
+
45
  if any(k in status.lower() for k in LIVE_KEYWORDS):
46
  live_matches.append({
47
  "Match Title": match_title,
 
61
  headers = {"User-Agent": "Mozilla/5.0"}
62
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
63
  result_blocks = soup.find_all("a", href=True)
64
+
65
  results = []
66
  for tag in result_blocks:
67
  if "won by" in tag.text.lower():
 
71
  date_tag = parent.find("div", class_="schedule-date") if parent else None
72
  date = date_tag.text.strip() if date_tag else "β€”"
73
  results.append((date, desc, link))
74
+
75
  return results[-5:][::-1] if results else []
76
 
77
  def get_upcoming_matches():
78
  headers = {"User-Agent": "Mozilla/5.0"}
79
  soup = BeautifulSoup(requests.get(IPL_MATCHES_URL, headers=headers).text, "html.parser")
80
  matches = []
81
+
82
  for block in soup.select("div.cb-series-matches"):
83
  title_tag = block.select_one("a.text-hvr-underline")
84
  venue_tag = block.select_one("div.text-gray")
85
  time_tag = block.select_one("a.cb-text-upcoming")
86
+
87
  if title_tag and venue_tag and time_tag:
88
  title = title_tag.text.strip()
89
  venue = venue_tag.text.strip()
90
+ start = time_tag.text.strip()
91
  link = BASE_URL + title_tag["href"]
92
  matches.append(f"- [{title} at {venue}]({link})")
93
+
94
  return matches[:5]
95
 
96
+ st.subheader("🟒 Live Matches")
97
+ matches = fetch_live_ipl_matches()
98
+ if matches:
99
+ for match in matches:
100
+ st.markdown(f"#### πŸ”₯ {match['Match Title']}")
101
+ st.markdown(f"🏏 **Batting Side**: {match['Batting Score']}")
102
+ st.markdown(f"🎯 **Bowling Side**: {match['Bowling Score']}")
103
+ st.markdown(f"πŸ“£ **Status**: {match['Status']}")
 
 
 
 
 
104
  st.markdown("---")
105
+ else:
106
+ st.warning("❌ No live IPL matches currently.")
107
+ st.markdown("---")
108
+
109
+
110
+ st.subheader("πŸ“… Recent Match Results")
111
+ for _, desc, link in get_recent_match_results():
112
+ st.markdown(f"- [{desc}]({link})")
113
+
114
+ st.divider()
115
+ st.subheader("πŸ—“οΈ Upcoming Matches")
116
+ for line in get_upcoming_matches():
117
+ st.markdown(line)
118
+
119
+ st.divider()
120
+
121
+ st.subheader("πŸ“° Latest IPL News")
122
+ for title, link in get_recent_news():
123
+ st.markdown(f"- [{title}]({link})")
124
+