Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,11 +21,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 21 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 22 |
|
| 23 |
@tool
|
| 24 |
-
def get_nba_matches() -> str:
|
| 25 |
"""
|
| 26 |
A tool that retrieves upcoming NBA matches using TheRundown API.
|
| 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 |
"""
|
|
@@ -34,27 +37,54 @@ def get_nba_matches() -> str:
|
|
| 34 |
from typing import Dict, Any
|
| 35 |
import json
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
# API configuration
|
| 41 |
-
url = f"https://api.apilayer.com/therundown/sports/4/openers/{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
headers = {
|
| 43 |
"apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
|
| 44 |
}
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
|
|
@@ -63,38 +93,70 @@ def get_nba_matches() -> str:
|
|
| 63 |
|
| 64 |
# Get the lines info (odds)
|
| 65 |
lines = event.get("lines", {})
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Extract odds information
|
| 69 |
-
spread_info =
|
| 70 |
-
total_info =
|
| 71 |
-
moneyline_info =
|
| 72 |
|
| 73 |
-
# Get venue information
|
| 74 |
-
|
| 75 |
-
venue_name =
|
| 76 |
-
venue_location =
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
f"
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
f"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
f"
|
| 88 |
-
f"
|
| 89 |
-
"
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)}"
|
|
|
|
| 21 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 22 |
|
| 23 |
@tool
|
| 24 |
+
def get_nba_matches(target_date: str = None) -> str:
|
| 25 |
"""
|
| 26 |
A tool that retrieves upcoming NBA matches using TheRundown API.
|
| 27 |
Uses the /openers endpoint for the specified date.
|
| 28 |
|
| 29 |
+
Args:
|
| 30 |
+
target_date: Optional date string in YYYY-MM-DD format. If not provided, uses today's date.
|
| 31 |
+
|
| 32 |
Returns:
|
| 33 |
A human-readable string listing the upcoming NBA matches with detailed odds information.
|
| 34 |
"""
|
|
|
|
| 37 |
from typing import Dict, Any
|
| 38 |
import json
|
| 39 |
|
| 40 |
+
# Use provided date or today's date
|
| 41 |
+
if target_date is None:
|
| 42 |
+
target_date = datetime.date.today().strftime("%Y-%m-%d")
|
| 43 |
|
| 44 |
# API configuration
|
| 45 |
+
url = f"https://api.apilayer.com/therundown/sports/4/openers/{target_date}"
|
| 46 |
+
params = {
|
| 47 |
+
"offset": "0",
|
| 48 |
+
"include": "scores"
|
| 49 |
+
}
|
| 50 |
headers = {
|
| 51 |
"apikey": "7k4hKSUeWkbigKxZiNV5CQ8RSlEd72Cj"
|
| 52 |
}
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
print(f"Fetching NBA matches for {target_date}...") # Debug log
|
| 56 |
+
response = requests.get(url, headers=headers, params=params)
|
| 57 |
+
|
| 58 |
+
# Print debug information
|
| 59 |
+
print(f"URL: {response.url}")
|
| 60 |
+
print(f"Status Code: {response.status_code}")
|
| 61 |
+
|
| 62 |
response.raise_for_status() # Raise an exception for bad status codes
|
| 63 |
|
| 64 |
data = response.json()
|
| 65 |
if not data.get("events"):
|
| 66 |
+
return f"No NBA matches found for {target_date}."
|
| 67 |
|
| 68 |
matches = []
|
| 69 |
for event in data["events"]:
|
| 70 |
+
# Extract event date and time
|
| 71 |
+
event_date = event.get("event_date", "N/A")
|
| 72 |
+
if event_date != "N/A":
|
| 73 |
+
try:
|
| 74 |
+
dt = datetime.datetime.strptime(event_date, "%Y-%m-%dT%H:%M:%SZ")
|
| 75 |
+
event_date = dt.strftime("%Y-%m-%d %H:%M UTC")
|
| 76 |
+
except ValueError:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
# Get schedule information
|
| 80 |
+
schedule = event.get("schedule", {})
|
| 81 |
+
event_headline = schedule.get("event_headline", "")
|
| 82 |
+
|
| 83 |
# Extract basic game info
|
| 84 |
teams = event.get("teams_normalized", [])
|
| 85 |
+
if not teams:
|
| 86 |
+
teams = event.get("teams", []) # Fallback to regular teams if normalized not available
|
| 87 |
+
|
| 88 |
if len(teams) < 2:
|
| 89 |
continue
|
| 90 |
|
|
|
|
| 93 |
|
| 94 |
# Get the lines info (odds)
|
| 95 |
lines = event.get("lines", {})
|
| 96 |
+
|
| 97 |
+
# Try different bookmakers in order of preference
|
| 98 |
+
bookmaker = None
|
| 99 |
+
for bookmaker_id in ["3", "2", "12", "22", "23"]: # Pinnacle, Bovada, Bodog, BetMGM, FanDuel
|
| 100 |
+
if bookmaker_id in lines:
|
| 101 |
+
bookmaker = lines[bookmaker_id]
|
| 102 |
+
break
|
| 103 |
+
|
| 104 |
+
if not bookmaker:
|
| 105 |
+
bookmaker = next(iter(lines.values())) if lines else {}
|
| 106 |
|
| 107 |
# Extract odds information
|
| 108 |
+
spread_info = bookmaker.get("spread", {})
|
| 109 |
+
total_info = bookmaker.get("total", {})
|
| 110 |
+
moneyline_info = bookmaker.get("moneyline", {})
|
| 111 |
|
| 112 |
+
# Get venue and broadcast information
|
| 113 |
+
score_info = event.get("score", {})
|
| 114 |
+
venue_name = score_info.get("venue_name", "N/A")
|
| 115 |
+
venue_location = score_info.get("venue_location", "N/A")
|
| 116 |
+
broadcast = score_info.get("broadcast", "N/A")
|
| 117 |
+
|
| 118 |
+
game_info = [
|
| 119 |
+
f"Date: {event_date}",
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
if event_headline:
|
| 123 |
+
game_info.append(f"Event: {event_headline}")
|
| 124 |
+
|
| 125 |
+
game_info.extend([
|
| 126 |
+
f"Game: {away_team.get('name', 'Unknown')} ({away_team.get('record', 'N/A')}) @ "
|
| 127 |
+
f"{home_team.get('name', 'Unknown')} ({home_team.get('record', 'N/A')})",
|
| 128 |
+
f"Venue: {venue_name} ({venue_location})",
|
| 129 |
+
f"Broadcast: {broadcast}",
|
| 130 |
+
])
|
| 131 |
+
|
| 132 |
+
# Only add odds if they are meaningful (not 0.0001)
|
| 133 |
+
if moneyline_info.get("moneyline_away", 0) != 0.0001:
|
| 134 |
+
game_info.append(
|
| 135 |
+
f"Moneyline: {away_team.get('name', 'Away')} {moneyline_info.get('moneyline_away', 'N/A')} | "
|
| 136 |
+
f"{home_team.get('name', 'Home')} {moneyline_info.get('moneyline_home', 'N/A')}"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if spread_info.get("point_spread_away", 0) != 0.0001:
|
| 140 |
+
game_info.append(
|
| 141 |
+
f"Spread: {away_team.get('name', 'Away')} {spread_info.get('point_spread_away', 'N/A')} "
|
| 142 |
+
f"({spread_info.get('point_spread_away_money', 'N/A')})"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
if total_info.get("total_over", 0) != 0.0001:
|
| 146 |
+
game_info.append(
|
| 147 |
+
f"Total: O/U {total_info.get('total_over', 'N/A')} "
|
| 148 |
+
f"(Over: {total_info.get('total_over_money', 'N/A')} | "
|
| 149 |
+
f"Under: {total_info.get('total_under_money', 'N/A')})"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
game_info.append("----------------------------------------")
|
| 153 |
+
|
| 154 |
+
matches.append("\n".join(game_info))
|
| 155 |
|
| 156 |
if matches:
|
| 157 |
return "\n".join(matches)
|
| 158 |
else:
|
| 159 |
+
return f"No NBA matches found with complete information for {target_date}."
|
| 160 |
|
| 161 |
except requests.exceptions.RequestException as e:
|
| 162 |
return f"Error retrieving NBA matches: {str(e)}"
|