Aktraiser commited on
Commit
ba39120
·
verified ·
1 Parent(s): 66984de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -20
app.py CHANGED
@@ -27,41 +27,79 @@ def get_nba_matches() -> str:
27
  Uses the /openers endpoint for the specified date.
28
 
29
  Returns:
30
- A human-readable string listing the upcoming NBA matches.
31
  """
32
  import requests
33
  import datetime
 
 
34
 
35
- # Utilisez la date d'aujourd'hui ou une date spécifique
36
  today = datetime.date.today().strftime("%Y-%m-%d")
37
- # Construisez l'URL avec des paramètres corrects (ici offset=0 et include=scores)
38
- url = f"https://api.apilayer.com/therundown/sports/4/openers/{today}?offset=0&include=scores"
39
 
 
 
40
  headers = {
41
  "apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
42
  }
43
 
44
- response = requests.get(url, headers=headers)
45
- if response.status_code == 200:
 
 
46
  data = response.json()
 
 
 
47
  matches = []
48
- # Parcourez la liste des événements retournés par l'API
49
- for event in data.get("events", []):
50
- # Utilisez la clé "event_date" au lieu de "start_time"
51
- event_date = event.get("event_date", "Unknown date")
52
- teams = event.get("teams", [])
53
- if len(teams) >= 2:
54
- team1 = teams[0].get("name", "Team1 N/A")
55
- team2 = teams[1].get("name", "Team2 N/A")
56
- else:
57
- team1, team2 = "Team1 N/A", "Team2 N/A"
58
- matches.append(f"{event_date}: {team1} vs {team2}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  if matches:
60
  return "\n".join(matches)
61
  else:
62
- return "No NBA matches found."
63
- else:
64
- return f"Error retrieving NBA matches: {response.status_code}"
 
 
 
65
 
66
  @tool
67
  def predict_nba_match(match_info: str) -> str:
 
27
  Uses the /openers endpoint for the specified date.
28
 
29
  Returns:
30
+ A human-readable string listing the upcoming NBA matches with detailed odds information.
31
  """
32
  import requests
33
  import datetime
34
+ from typing import Dict, Any
35
+ import json
36
 
37
+ # Get today's date
38
  today = datetime.date.today().strftime("%Y-%m-%d")
 
 
39
 
40
+ # API configuration
41
+ url = f"https://api.apilayer.com/therundown/sports/4/openers/{today}"
42
  headers = {
43
  "apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
44
  }
45
 
46
+ try:
47
+ response = requests.get(url, headers=headers)
48
+ response.raise_for_status() # Raise an exception for bad status codes
49
+
50
  data = response.json()
51
+ if not data.get("events"):
52
+ return "No NBA matches found for today."
53
+
54
  matches = []
55
+ for event in data["events"]:
56
+ # Extract basic game info
57
+ teams = event.get("teams_normalized", [])
58
+ if len(teams) < 2:
59
+ continue
60
+
61
+ away_team = teams[0] if teams[0].get("is_away") else teams[1]
62
+ home_team = teams[1] if teams[0].get("is_away") else teams[0]
63
+
64
+ # Get the lines info (odds)
65
+ lines = event.get("lines", {})
66
+ pinnacle = lines.get("3", {}) # Pinnacle is usually most reliable
67
+
68
+ # Extract odds information
69
+ spread_info = pinnacle.get("spread", {})
70
+ total_info = pinnacle.get("total", {})
71
+ moneyline_info = pinnacle.get("moneyline", {})
72
+
73
+ # Get venue information
74
+ schedule = event.get("schedule", {})
75
+ venue_name = event.get("score", {}).get("venue_name", "N/A")
76
+ venue_location = event.get("score", {}).get("venue_location", "N/A")
77
+
78
+ game_info = (
79
+ f"Game: {away_team['name']} ({away_team.get('record', 'N/A')}) @ "
80
+ f"{home_team['name']} ({home_team.get('record', 'N/A')})\n"
81
+ f"Venue: {venue_name} ({venue_location})\n"
82
+ f"Moneyline: {away_team['name']} {moneyline_info.get('moneyline_away', 'N/A')} | "
83
+ f"{home_team['name']} {moneyline_info.get('moneyline_home', 'N/A')}\n"
84
+ f"Spread: {away_team['name']} {spread_info.get('point_spread_away', 'N/A')} "
85
+ f"({spread_info.get('point_spread_away_money', 'N/A')})\n"
86
+ f"Total: O/U {total_info.get('total_over', 'N/A')} "
87
+ f"(Over: {total_info.get('total_over_money', 'N/A')} | "
88
+ f"Under: {total_info.get('total_under_money', 'N/A')})\n"
89
+ "----------------------------------------\n"
90
+ )
91
+
92
+ matches.append(game_info)
93
+
94
  if matches:
95
  return "\n".join(matches)
96
  else:
97
+ return "No NBA matches found with complete information."
98
+
99
+ except requests.exceptions.RequestException as e:
100
+ return f"Error retrieving NBA matches: {str(e)}"
101
+ except (KeyError, json.JSONDecodeError) as e:
102
+ return f"Error parsing NBA data: {str(e)}"
103
 
104
  @tool
105
  def predict_nba_match(match_info: str) -> str: