DKethan commited on
Commit
3058e20
Β·
verified Β·
1 Parent(s): 403995d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -69
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
4
  from datetime import datetime
 
5
 
6
  IPL_TEAMS = [
7
  "Chennai Super Kings", "Delhi Capitals", "Gujarat Titans", "Kolkata Knight Riders",
@@ -13,53 +14,97 @@ BASE_URL = "https://www.cricbuzz.com"
13
 
14
  # Dynamically get the current IPL year
15
  current_year = datetime.now().year
16
- print(f"πŸ“… [INFO] Current year detected: {current_year}")
17
-
18
- # Detect team
19
- def detect_team(user_input):
20
- print(f"\n🟒 [DETECT] Input: {user_input}")
21
- for team in IPL_TEAMS:
22
- if team.lower() in user_input.lower():
23
- print(f"βœ… [DETECT] Found team: {team}")
24
- return team
25
- print("❌ [DETECT] No IPL team found")
26
- return None
27
-
28
- # Get live matches from cricbuzz homepage
29
  def get_live_scores(team_name=None):
30
- print("\n🌐 [LIVE] Fetching live scores page...")
31
  url = "https://www.cricbuzz.com/cricket-match/live-scores"
32
  headers = {"User-Agent": "Mozilla/5.0"}
33
  response = requests.get(url, headers=headers)
34
- print(f"βœ… [LIVE] Response code: {response.status_code}")
35
 
36
  soup = BeautifulSoup(response.text, "html.parser")
37
- matches = soup.find_all("div", class_="cb-col cb-col-100 cb-ltst-wgt-hdr")
38
- print(f"πŸ” [LIVE] Found {len(matches)} match blocks")
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  results = []
41
- for block in matches:
42
- text = block.get_text(separator=" ").strip()
43
- if team_name is None or team_name.lower() in text.lower():
44
- results.append("🟒 LIVE: " + text)
45
- print(f"βœ… [LIVE MATCH] {text[:100]}...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- return results[:5] if results else []
48
 
49
- # Get recent results if no live match
50
  def get_recent_results(team_name=None):
51
  try:
52
- url = f"{BASE_URL}/cricket-series/7607/indian-premier-league-{current_year}/matches"
53
- print(f"\n🌐 [RECENT] Fetching matches from: {url}")
54
  headers = {"User-Agent": "Mozilla/5.0"}
55
  response = requests.get(url, headers=headers)
56
- print(f"βœ… [RECENT] Status code: {response.status_code}")
57
 
58
  soup = BeautifulSoup(response.text, "html.parser")
59
  all_tags = soup.find_all("a", href=True)
60
- print(f"πŸ” [RECENT] Total <a> tags: {len(all_tags)}")
61
 
62
- won, lost = [], []
 
63
 
64
  for i in range(len(all_tags) - 1):
65
  tag = all_tags[i]
@@ -68,57 +113,51 @@ def get_recent_results(team_name=None):
68
  href = tag['href']
69
  title = tag.get_text().strip()
70
  result = next_tag.get_text().strip()
 
71
 
72
  if "cricket-scores" in href and any(t.lower() in title.lower() for t in IPL_TEAMS):
73
- full_url = BASE_URL + href
74
- full_text = f"{title} – {result}\n[View Match]({full_url})"
75
-
76
  if team_name and team_name.lower() not in title.lower():
 
77
  continue
78
 
79
- if "won by" in result.lower():
80
- print(f"βœ… [WON] {title} => {result}")
81
- won.append(full_text)
82
- elif "lost" in result.lower():
83
- print(f"πŸŸ₯ [LOST] {title} => {result}")
84
- lost.append(full_text)
85
 
86
- top_results = won[:5] + lost[:5]
87
- print(f"πŸ“Š [RECENT] Total displayed: {len(top_results)}")
88
- return top_results if top_results else ["No recent match results found."]
 
 
 
 
 
 
89
 
90
  except Exception as e:
91
- print(f"❌ [ERROR] Failed recent match fetch: {e}")
92
  return ["Error loading past matches."]
93
 
 
94
  # Streamlit UI
95
  st.set_page_config(page_title="IPL Chatbot", layout="centered")
96
  st.title("🏏 IPL Chatbot")
97
 
98
- user_input = st.text_input("Ask about IPL scores:", placeholder="e.g. Show me Mumbai Indians score")
99
-
100
- if user_input:
101
- print(f"\n========== USER INPUT ==========")
102
- print(user_input)
103
- print("================================")
104
-
105
- team = detect_team(user_input)
106
-
107
- scores = get_live_scores(team)
108
- if scores:
109
- st.subheader("🟒 Live Match(es):")
110
- for s in scores:
111
- st.markdown(s)
112
- else:
113
- st.warning("❌ No live match found. Showing recent results:")
114
- past = get_recent_results(team)
115
- for p in past:
116
- st.markdown(p)
117
-
118
- with st.expander("πŸ’‘ Try examples"):
119
- st.markdown("""
120
- - Show Mumbai Indians score
121
- - Delhi Capitals recent results
122
- - CSK won?
123
- - RCB vs MI today?
124
- """)
 
2
  import requests
3
  from bs4 import BeautifulSoup
4
  from datetime import datetime
5
+ import re
6
 
7
  IPL_TEAMS = [
8
  "Chennai Super Kings", "Delhi Capitals", "Gujarat Titans", "Kolkata Knight Riders",
 
14
 
15
  # Dynamically get the current IPL year
16
  current_year = datetime.now().year
17
+ print(f"πŸ“… Current year detected: {current_year}")
18
+
19
+
 
 
 
 
 
 
 
 
 
 
20
  def get_live_scores(team_name=None):
21
+ print("🌐 [LIVE] Fetching live scores page...")
22
  url = "https://www.cricbuzz.com/cricket-match/live-scores"
23
  headers = {"User-Agent": "Mozilla/5.0"}
24
  response = requests.get(url, headers=headers)
25
+ print(f"βœ… [LIVE] Response status code: {response.status_code}")
26
 
27
  soup = BeautifulSoup(response.text, "html.parser")
28
+ pattern = re.compile(fr"/live-cricket-scores/.+-match-indian-premier-league-{current_year}")
29
+ matches = [a['href'] for a in soup.find_all('a', href=True) if pattern.search(a['href'])]
30
+ print(f"πŸ” [LIVE] Matches found: {matches}")
31
+
32
+ pattern_4_score = re.compile(r'(\d+)(?:th|st|nd|rd)-match')
33
+ final_matches = sorted(
34
+ [(link, int(pattern_4_score.search(link).group(1))) for link in matches if pattern_4_score.search(link)],
35
+ key=lambda x: x[1],
36
+ reverse=True
37
+ )
38
+ print(f"πŸ” [LIVE] Final matches sorted: {final_matches}")
39
+
40
+ if not final_matches:
41
+ print("❌ [LIVE] No match blocks found. Check HTML structure or class names.")
42
+ return []
43
 
44
  results = []
45
+ seen_matches = set()
46
+
47
+ for match_url, _ in final_matches:
48
+ print(f"🌐 [LIVE] Fetching match details from: {BASE_URL + match_url}")
49
+ match_response = requests.get(BASE_URL + match_url, headers=headers)
50
+ soup = BeautifulSoup(match_response.text, "html.parser")
51
+
52
+ score_blocks = soup.select("div.cb-col.cb-col-100.cb-min-tm")
53
+ rr_score = score_blocks[0].text.strip() if len(score_blocks) > 0 else "N/A"
54
+ csk_score = score_blocks[1].text.strip() if len(score_blocks) > 1 else "N/A"
55
+ print(f"πŸ” [LIVE] Scores: {rr_score}, {csk_score}")
56
+
57
+ result_block = soup.select_one("div.cb-min-stts")
58
+ result_text = result_block.text.strip() if result_block else "Result unavailable"
59
+ print(f"πŸ” [LIVE] Result: {result_text}")
60
+
61
+ mom_block = soup.select_one("div.cb-mom-itm a.cb-link-undrln")
62
+ mom_text = mom_block.text.strip() if mom_block else "Unknown"
63
+ print(f"πŸ” [LIVE] Player of the Match: {mom_text}")
64
+
65
+ commentary_blocks = soup.find_all("p", class_="cb-com-ln", limit=3)
66
+ commentary = [c.get_text(strip=True) for c in commentary_blocks if c.get_text(strip=True)]
67
+ commentary_text = " ".join(commentary)
68
+ print(f"πŸ” [LIVE] Commentary: {commentary_text}")
69
+
70
+ # Skip match if no useful data
71
+ if rr_score == "N/A" and csk_score == "N/A" and result_text == "Result unavailable":
72
+ print("❌ [LIVE] Skipping match due to lack of useful data.")
73
+ continue
74
+
75
+ # Create a simple ID based on team scores and result
76
+ match_signature = f"{rr_score}|{csk_score}|{result_text}"
77
+ if match_signature in seen_matches:
78
+ print("❌ [LIVE] Skipping duplicate match.")
79
+ continue # Skip duplicate
80
+ seen_matches.add(match_signature)
81
+
82
+ results.append({
83
+ "1st Team Score": rr_score,
84
+ "2nd Team Score": csk_score,
85
+ "Result": result_text,
86
+ "Player of the Match": mom_text,
87
+ "Latest Commentary": commentary_text[:100]
88
+ })
89
+ print(f"βœ… [LIVE] Match added: {results[-1]}")
90
+
91
+ return results[:2] if results else []
92
 
 
93
 
 
94
  def get_recent_results(team_name=None):
95
  try:
96
+ url = f"{BASE_URL}/cricket-series/9237/Indian-Premier-League-{current_year}/matches"
97
+ print(f"🌐 [RECENT] Fetching matches from: {url}")
98
  headers = {"User-Agent": "Mozilla/5.0"}
99
  response = requests.get(url, headers=headers)
100
+ print(f"βœ… [RECENT] Response status code: {response.status_code}")
101
 
102
  soup = BeautifulSoup(response.text, "html.parser")
103
  all_tags = soup.find_all("a", href=True)
104
+ print(f"πŸ” [RECENT] Total <a> tags found: {len(all_tags)}")
105
 
106
+ results = []
107
+ seen = set()
108
 
109
  for i in range(len(all_tags) - 1):
110
  tag = all_tags[i]
 
113
  href = tag['href']
114
  title = tag.get_text().strip()
115
  result = next_tag.get_text().strip()
116
+ print(f"πŸ” [RECENT] Processing tag: {title} – {result}")
117
 
118
  if "cricket-scores" in href and any(t.lower() in title.lower() for t in IPL_TEAMS):
 
 
 
119
  if team_name and team_name.lower() not in title.lower():
120
+ print(f"❌ [RECENT] Skipping match for team: {team_name}")
121
  continue
122
 
123
+ match_id = f"{title}-{result}"
124
+ if match_id in seen:
125
+ print(f"❌ [RECENT] Skipping duplicate match: {match_id}")
126
+ continue
127
+ seen.add(match_id)
 
128
 
129
+ full_url = BASE_URL + href
130
+ full_text = f"{title} – {result})"
131
+ print(f"βœ… [RECENT] Match added: {full_text}")
132
+
133
+ results.append(full_text)
134
+
135
+ # Show up to 10 most recent results
136
+ print(f"πŸ“Š [RECENT] Total displayed results: {len(results)}")
137
+ return results[:10] if results else ["No recent match results found."]
138
 
139
  except Exception as e:
140
+ print(f"❌ [ERROR] Failed to fetch recent matches: {e}")
141
  return ["Error loading past matches."]
142
 
143
+
144
  # Streamlit UI
145
  st.set_page_config(page_title="IPL Chatbot", layout="centered")
146
  st.title("🏏 IPL Chatbot")
147
 
148
+ # Live Matches
149
+ scores = get_live_scores()
150
+ if scores:
151
+ st.subheader("🟒 Live Match:")
152
+ st.table(scores)
153
+ else:
154
+ st.warning("❌ No live match found.")
155
+
156
+ # Recent Results
157
+ recent_results = get_recent_results()
158
+ if recent_results:
159
+ st.subheader("πŸ“Š Recent Results:")
160
+ for result in recent_results:
161
+ st.markdown(result)
162
+ else:
163
+ st.warning("❌ No recent results found.")