Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,46 +39,44 @@ def get_box_score_links() -> list:
|
|
39 |
# Return error message as a list to maintain consistent return type
|
40 |
return [f"Error fetching boxScore links: {str(e)}"]
|
41 |
|
42 |
-
def get_box_score_data(
|
43 |
-
"""A tool that fetches the boxscores data from
|
44 |
Args:
|
45 |
-
|
46 |
"""
|
47 |
try:
|
48 |
box_scores = {}
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
match = re.search(pattern, str(soup.find('div', id="content").find('h1')))
|
56 |
|
57 |
-
if
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
tables = pd.read_html(url)
|
63 |
-
|
64 |
-
# Check if the expected tables exist before accessing
|
65 |
-
if len(tables) > 0:
|
66 |
-
df_team1 = tables[0].to_dict(orient='records')
|
67 |
-
else:
|
68 |
-
df_team1 = [{"Error": "Team 1 data not found"}]
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
else:
|
73 |
-
df_team2 = [{"Error": "Team 2 data not found"}]
|
74 |
-
|
75 |
-
# Store box score data
|
76 |
-
box_scores[team1] = df_team1
|
77 |
-
box_scores[team2] = df_team2
|
78 |
-
|
79 |
else:
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
return box_scores
|
84 |
|
|
|
39 |
# Return error message as a list to maintain consistent return type
|
40 |
return [f"Error fetching boxScore links: {str(e)}"]
|
41 |
|
42 |
+
def get_box_score_data(url: list) -> dict:
|
43 |
+
"""A tool that fetches the boxscores data from a provided URL.
|
44 |
Args:
|
45 |
+
url: A string representing the URL to a box score of an nba game from last night.
|
46 |
"""
|
47 |
try:
|
48 |
box_scores = {}
|
49 |
+
response = requests.get(url)
|
50 |
+
response.raise_for_status() # Raise exception for HTTP errors
|
51 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
52 |
+
pattern = r"<h1>(.*?) at (.*?) Box Score"
|
53 |
+
match = re.search(pattern, str(soup.find('div', id="content").find('h1')))
|
54 |
+
|
55 |
+
if match:
|
56 |
+
team1 = match.group(1)
|
57 |
+
team2 = match.group(2)
|
58 |
|
59 |
+
# Read HTML tables
|
60 |
+
tables = pd.read_html(url)
|
|
|
61 |
|
62 |
+
# Check if the expected tables exist before accessing
|
63 |
+
if len(tables) > 0:
|
64 |
+
df_team1 = tables[0].to_dict(orient='records')
|
65 |
+
else:
|
66 |
+
df_team1 = [{"Error": "Team 1 data not found"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if len(tables) > 8:
|
69 |
+
df_team2 = tables[8].to_dict(orient='records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
else:
|
71 |
+
df_team2 = [{"Error": "Team 2 data not found"}]
|
72 |
+
|
73 |
+
# Store box score data
|
74 |
+
box_scores[team1] = df_team1
|
75 |
+
box_scores[team2] = df_team2
|
76 |
+
|
77 |
+
else:
|
78 |
+
# If regex pattern did not match
|
79 |
+
box_scores[url] = [{"Error": "Team names not found in the page title"}]
|
80 |
|
81 |
return box_scores
|
82 |
|