jbisal commited on
Commit
1ae6984
·
verified ·
1 Parent(s): a97f2d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -32
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(links: list) -> dict:
43
- """A tool that fetches the boxscores data from the provided list of URLs.
44
  Args:
45
- links: A list of strings representing the URLs to the box score of each of last night's games.
46
  """
47
  try:
48
  box_scores = {}
49
- for url in links:
50
- response = requests.get(url)
51
- response.raise_for_status() # Raise exception for HTTP errors
 
 
 
 
 
 
52
 
53
- soup = BeautifulSoup(response.text, 'html.parser')
54
- pattern = r"<h1>(.*?) at (.*?) Box Score"
55
- match = re.search(pattern, str(soup.find('div', id="content").find('h1')))
56
 
57
- if match:
58
- team1 = match.group(1)
59
- team2 = match.group(2)
60
-
61
- # Read HTML tables
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
- if len(tables) > 8:
71
- df_team2 = tables[8].to_dict(orient='records')
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
- # If regex pattern did not match
81
- box_scores[url] = [{"Error": "Team names not found in the page title"}]
 
 
 
 
 
 
 
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