Spaces:
Running
Running
File size: 3,358 Bytes
6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c 699526c a5f1d3c 699526c a5f1d3c 6764e7c a5f1d3c 6764e7c a5f1d3c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | from nba_api.stats.endpoints import scoreboardv2
from nba_api.stats.static import teams
from datetime import datetime
import pytz
# --- Configuration ---
# Set your target timezone
target_timezone = pytz.timezone('US/Arizona')
eastern_timezone = pytz.timezone('US/Eastern')
# --- Helper: Get Today's Date in AZ ---
now_az = datetime.now(target_timezone)
game_date_str = now_az.strftime('%Y-%m-%d')
print(f"Fetching games for {now_az.strftime('%A, %b %d')} (Arizona Time)...")
print("-" * 30)
try:
# 1. Fetch Data
board = scoreboardv2.ScoreboardV2(game_date=game_date_str)
games_df = board.game_header.get_data_frame()
linescore_df = board.line_score.get_data_frame()
if games_df.empty:
print("No games scheduled today.")
else:
# 2. Map Team IDs to Names
nba_teams = teams.get_teams()
team_map = {t['id']: t['full_name'] for t in nba_teams}
# 3. Iterate Games
for _, game in games_df.iterrows():
game_id = game['GAME_ID']
host_name = team_map.get(game['HOME_TEAM_ID'], "Unknown")
visitor_name = team_map.get(game['VISITOR_TEAM_ID'], "Unknown")
status_text = game['GAME_STATUS_TEXT'] # e.g. "7:00 pm ET", "Final"
# Get Scores if available
home_score = 0
visitor_score = 0
# Filter linescore for this game
game_scores = linescore_df[linescore_df['GAME_ID'] == game_id]
if not game_scores.empty:
# Home Score
h_rows = game_scores[game_scores['TEAM_ID'] == game['HOME_TEAM_ID']]
if not h_rows.empty and h_rows['PTS'].values[0]:
home_score = int(h_rows['PTS'].values[0])
# Visitor Score
v_rows = game_scores[game_scores['TEAM_ID'] == game['VISITOR_TEAM_ID']]
if not v_rows.empty and v_rows['PTS'].values[0]:
visitor_score = int(v_rows['PTS'].values[0])
# Time Conversion
display_time = ""
if "ET" in status_text:
try:
time_part = status_text.replace(" ET", "").strip()
dt_str = f"{game_date_str} {time_part}"
dt_est = datetime.strptime(dt_str, '%Y-%m-%d %I:%M %p')
dt_est = eastern_timezone.localize(dt_est)
dt_az = dt_est.astimezone(target_timezone)
display_time = dt_az.strftime('%I:%M %p MST')
except:
display_time = status_text
else:
display_time = status_text
# --- OUTPUT FOR UI PARSER ---
# The JS regex looks for "Visitor @ Home"
print(f"\n{visitor_name} @ {host_name}")
# The JS regex looks for scores "100 - 90"
# Only print scores if game has started
if "Final" in status_text or "Q" in status_text or ":" in str(status_text) and "ET" not in str(status_text):
print(f"{visitor_score} - {home_score}")
print(f"Status: {status_text}") # "Final" or "Live" triggers
else:
print(f"Time: {display_time}")
print("Status: Upcoming")
except Exception as e:
print(f"Error: {e}") |