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}")