Fix NBA leaderboard team ID field for logo fetching (#116)

* Fix NBA leaderboard team ID field for logo fetching

- Add missing 'id' field to NBA team standings data structure
- Enables proper logo fetching from assets/sports/nba_logos/
- Fixes 'id' KeyError when creating NBA leaderboard images
- Includes diagnostic and test scripts for verification

* Add NBA logo downloader script and documentation

- download_nba_logos.py: Script to download all 30 NBA team logos from ESPN API
- README_NBA_LOGOS.md: Comprehensive documentation for the logo downloader
- Supports force re-download and quiet modes
- Downloads to assets/sports/nba_logos/ for leaderboard integration

* replace NBA Logos

* return NBA logo
This commit is contained in:
Chuck
2025-10-10 18:27:36 -04:00
committed by GitHub
parent 584a4f258e
commit 98d3ed7d91
39 changed files with 1181 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
#!/usr/bin/env python3
"""
Simple test script to verify NBA data structure includes team ID fields.
"""
import sys
import os
import requests
import logging
import json
from typing import Dict, Any
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def test_nba_data_structure():
"""Test that NBA data includes team ID fields."""
try:
# Test fetching NBA teams data directly
logger.info("Testing NBA teams API...")
teams_url = "https://site.api.espn.com/apis/site/v2/sports/basketball/nba/teams"
response = requests.get(teams_url, timeout=30)
response.raise_for_status()
teams_data = response.json()
# Extract team information
sports = teams_data.get('sports', [])
if not sports:
logger.error("No sports data found!")
return False
leagues = sports[0].get('leagues', [])
if not leagues:
logger.error("No leagues data found!")
return False
teams = leagues[0].get('teams', [])
if not teams:
logger.error("No teams data found!")
return False
logger.info(f"Found {len(teams)} NBA teams")
# Check first few teams for ID fields
teams_with_ids = 0
for i, team_data in enumerate(teams[:5]):
team = team_data.get('team', {})
team_id = team.get('id')
team_abbr = team.get('abbreviation', 'Unknown')
team_name = team.get('name', 'Unknown')
logger.info(f"Team {i+1}: ID={team_id}, ABBR={team_abbr}, NAME={team_name}")
if team_id is not None:
teams_with_ids += 1
if teams_with_ids == 0:
logger.error("No teams have ID fields!")
return False
logger.info(f"{teams_with_ids} out of 5 tested teams have ID fields")
# Test fetching NBA standings data directly
logger.info("Testing NBA standings API...")
standings_url = "https://site.api.espn.com/apis/v2/sports/basketball/nba/standings"
response = requests.get(standings_url, timeout=30)
response.raise_for_status()
standings_data = response.json()
# Check standings structure
children = standings_data.get('children', [])
logger.info(f"Found {len(children)} conference/division groups")
standings_teams_with_ids = 0
total_standings_teams = 0
for child in children:
if 'standings' in child and 'entries' in child['standings']:
entries = child['standings']['entries']
total_standings_teams += len(entries)
for entry in entries[:3]: # Check first 3 teams per conference
team = entry.get('team', {})
team_id = team.get('id')
team_abbr = team.get('abbreviation', 'Unknown')
team_name = team.get('displayName', 'Unknown')
logger.info(f"Standings team: ID={team_id}, ABBR={team_abbr}, NAME={team_name}")
if team_id is not None:
standings_teams_with_ids += 1
if standings_teams_with_ids == 0:
logger.error("No standings teams have ID fields!")
return False
logger.info(f"{standings_teams_with_ids} standings teams have ID fields out of {total_standings_teams} total teams")
# Simulate the fixed leaderboard manager logic
logger.info("Simulating fixed leaderboard manager logic...")
# Simulate the team data structure that would be created by the fixed code
simulated_teams = []
for team_data in teams[:3]: # Test with first 3 teams
team = team_data.get('team', {})
simulated_teams.append({
'name': team.get('name', 'Unknown'),
'id': team.get('id'), # This is the fix - including the ID field
'abbreviation': team.get('abbreviation', 'Unknown'),
'wins': 10, # Mock data
'losses': 5, # Mock data
'ties': 0, # Mock data
'win_percentage': 0.667 # Mock data
})
# Verify that our simulated teams have ID fields
teams_with_ids_in_simulation = 0
for team in simulated_teams:
if team.get('id') is not None:
teams_with_ids_in_simulation += 1
logger.info(f"Simulated team: {team['abbreviation']} (ID: {team['id']})")
if teams_with_ids_in_simulation == len(simulated_teams):
logger.info("✅ All simulated teams have ID fields - fix is working!")
return True
else:
logger.error(f"{len(simulated_teams) - teams_with_ids_in_simulation} simulated teams missing ID fields!")
return False
except Exception as e:
logger.error(f"Error testing NBA data structure: {e}")
return False
def main():
"""Main test function."""
logger.info("Testing NBA data structure and fix...")
success = test_nba_data_structure()
if success:
logger.info("✅ NBA data structure test PASSED!")
logger.info("The NBA leaderboard fix should work correctly")
else:
logger.error("❌ NBA data structure test FAILED!")
return success
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)