Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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"π
|
| 17 |
-
|
| 18 |
-
|
| 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("
|
| 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 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
results = []
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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/
|
| 53 |
-
print(f"
|
| 54 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 55 |
response = requests.get(url, headers=headers)
|
| 56 |
-
print(f"β
[RECENT]
|
| 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 |
-
|
|
|
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
lost.append(full_text)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
print(f"β [ERROR] Failed
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
if
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|