Add NCAA football logo download script and update team abbreviations
- Added download_espn_ncaa_fb_logos.py script to download logos from ESPN API - Downloaded 50 NCAA football team logos to test/ncaaFBlogos/ - Updated all_team_abbreviations.txt with current ESPN team data - Updated various team logo files (MLB, NBA, NFL, NHL, NCAAF)
192
test/download_espn_ncaa_fb_logos.py
Normal file
@@ -0,0 +1,192 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to download all NCAA Football team logos from ESPN API
|
||||
and update the all_team_abbreviations.txt file with current ESPN abbreviations.
|
||||
"""
|
||||
|
||||
import os
|
||||
import requests
|
||||
import json
|
||||
from pathlib import Path
|
||||
import time
|
||||
|
||||
def create_logo_directory():
|
||||
"""Create the ncaaFBlogos directory if it doesn't exist."""
|
||||
logo_dir = Path("test/ncaaFBlogos")
|
||||
logo_dir.mkdir(parents=True, exist_ok=True)
|
||||
return logo_dir
|
||||
|
||||
def fetch_teams_data():
|
||||
"""Fetch team data from ESPN API."""
|
||||
url = "https://site.api.espn.com/apis/site/v2/sports/football/college-football/teams"
|
||||
|
||||
try:
|
||||
response = requests.get(url, timeout=30)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching teams data: {e}")
|
||||
return None
|
||||
|
||||
def download_logo(url, filepath, team_name):
|
||||
"""Download a logo from URL and save to filepath."""
|
||||
try:
|
||||
response = requests.get(url, timeout=30)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(filepath, 'wb') as f:
|
||||
f.write(response.content)
|
||||
|
||||
print(f"✓ Downloaded: {team_name} -> {filepath.name}")
|
||||
return True
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"✗ Failed to download {team_name}: {e}")
|
||||
return False
|
||||
|
||||
def normalize_abbreviation(abbreviation):
|
||||
"""Normalize team abbreviation to lowercase for filename."""
|
||||
return abbreviation.lower()
|
||||
|
||||
def update_abbreviations_file(teams_data, abbreviations_file_path):
|
||||
"""Update the all_team_abbreviations.txt file with current ESPN abbreviations."""
|
||||
print(f"\nUpdating abbreviations file: {abbreviations_file_path}")
|
||||
|
||||
# Read existing file
|
||||
existing_content = []
|
||||
if os.path.exists(abbreviations_file_path):
|
||||
with open(abbreviations_file_path, 'r', encoding='utf-8') as f:
|
||||
existing_content = f.readlines()
|
||||
|
||||
# Find the NCAAF section
|
||||
ncaaf_start = -1
|
||||
ncaaf_end = -1
|
||||
|
||||
for i, line in enumerate(existing_content):
|
||||
if line.strip() == "NCAAF":
|
||||
ncaaf_start = i
|
||||
elif ncaaf_start != -1 and line.strip() and not line.startswith(" "):
|
||||
ncaaf_end = i
|
||||
break
|
||||
|
||||
if ncaaf_start == -1:
|
||||
print("Warning: Could not find NCAAF section in abbreviations file")
|
||||
return
|
||||
|
||||
if ncaaf_end == -1:
|
||||
ncaaf_end = len(existing_content)
|
||||
|
||||
# Extract teams from ESPN data
|
||||
espn_teams = []
|
||||
for team_data in teams_data:
|
||||
team = team_data.get('team', {})
|
||||
abbreviation = team.get('abbreviation', '')
|
||||
display_name = team.get('displayName', '')
|
||||
|
||||
if abbreviation and display_name:
|
||||
espn_teams.append((abbreviation, display_name))
|
||||
|
||||
# Sort teams by abbreviation
|
||||
espn_teams.sort(key=lambda x: x[0])
|
||||
|
||||
# Create new NCAAF section
|
||||
new_ncaaf_section = ["NCAAF\n"]
|
||||
for abbreviation, display_name in espn_teams:
|
||||
new_ncaaf_section.append(f" {abbreviation} => {display_name}\n")
|
||||
new_ncaaf_section.append("\n")
|
||||
|
||||
# Reconstruct the file
|
||||
new_content = (
|
||||
existing_content[:ncaaf_start] +
|
||||
new_ncaaf_section +
|
||||
existing_content[ncaaf_end:]
|
||||
)
|
||||
|
||||
# Write updated file
|
||||
with open(abbreviations_file_path, 'w', encoding='utf-8') as f:
|
||||
f.writelines(new_content)
|
||||
|
||||
print(f"✓ Updated abbreviations file with {len(espn_teams)} NCAAF teams")
|
||||
|
||||
def main():
|
||||
"""Main function to download all NCAA FB team logos and update abbreviations."""
|
||||
print("Starting NCAA Football logo download and abbreviations update...")
|
||||
|
||||
# Create directory
|
||||
logo_dir = create_logo_directory()
|
||||
print(f"Created/verified directory: {logo_dir}")
|
||||
|
||||
# Fetch teams data
|
||||
print("Fetching teams data from ESPN API...")
|
||||
data = fetch_teams_data()
|
||||
|
||||
if not data:
|
||||
print("Failed to fetch teams data. Exiting.")
|
||||
return
|
||||
|
||||
# Extract teams
|
||||
teams = []
|
||||
try:
|
||||
sports = data.get('sports', [])
|
||||
for sport in sports:
|
||||
leagues = sport.get('leagues', [])
|
||||
for league in leagues:
|
||||
teams = league.get('teams', [])
|
||||
break
|
||||
except (KeyError, IndexError) as e:
|
||||
print(f"Error parsing teams data: {e}")
|
||||
return
|
||||
|
||||
print(f"Found {len(teams)} teams")
|
||||
|
||||
# Download logos
|
||||
downloaded_count = 0
|
||||
failed_count = 0
|
||||
|
||||
for team_data in teams:
|
||||
team = team_data.get('team', {})
|
||||
|
||||
# Extract team information
|
||||
abbreviation = team.get('abbreviation', '')
|
||||
display_name = team.get('displayName', 'Unknown')
|
||||
logos = team.get('logos', [])
|
||||
|
||||
if not abbreviation or not logos:
|
||||
print(f"⚠ Skipping {display_name}: missing abbreviation or logos")
|
||||
continue
|
||||
|
||||
# Get the default logo (first one is usually default)
|
||||
logo_url = logos[0].get('href', '')
|
||||
if not logo_url:
|
||||
print(f"⚠ Skipping {display_name}: no logo URL")
|
||||
continue
|
||||
|
||||
# Create filename
|
||||
filename = f"{normalize_abbreviation(abbreviation)}.png"
|
||||
filepath = logo_dir / filename
|
||||
|
||||
# Skip if already exists
|
||||
if filepath.exists():
|
||||
print(f"⏭ Skipping {display_name}: {filename} already exists")
|
||||
continue
|
||||
|
||||
# Download logo
|
||||
if download_logo(logo_url, filepath, display_name):
|
||||
downloaded_count += 1
|
||||
else:
|
||||
failed_count += 1
|
||||
|
||||
# Small delay to be respectful to the API
|
||||
time.sleep(0.1)
|
||||
|
||||
print(f"\nDownload complete!")
|
||||
print(f"✓ Successfully downloaded: {downloaded_count} logos")
|
||||
print(f"✗ Failed downloads: {failed_count}")
|
||||
print(f"📁 Logos saved in: {logo_dir}")
|
||||
|
||||
# Update abbreviations file
|
||||
abbreviations_file_path = "assets/sports/all_team_abbreviations.txt"
|
||||
update_abbreviations_file(teams, abbreviations_file_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
BIN
test/ncaaFBlogos/amh.png
Normal file
|
After Width: | Height: | Size: 27 KiB |
BIN
test/ncaaFBlogos/ann.png
Normal file
|
After Width: | Height: | Size: 65 KiB |
BIN
test/ncaaFBlogos/ariz.png
Normal file
|
After Width: | Height: | Size: 26 KiB |
BIN
test/ncaaFBlogos/ark.png
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
test/ncaaFBlogos/asu.png
Normal file
|
After Width: | Height: | Size: 27 KiB |
BIN
test/ncaaFBlogos/aub.png
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
test/ncaaFBlogos/bois.png
Normal file
|
After Width: | Height: | Size: 54 KiB |
BIN
test/ncaaFBlogos/brst.png
Normal file
|
After Width: | Height: | Size: 109 KiB |
BIN
test/ncaaFBlogos/buena.png
Normal file
|
After Width: | Height: | Size: 42 KiB |
BIN
test/ncaaFBlogos/cal.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
test/ncaaFBlogos/car.png
Normal file
|
After Width: | Height: | Size: 34 KiB |
BIN
test/ncaaFBlogos/cla.png
Normal file
|
After Width: | Height: | Size: 53 KiB |
BIN
test/ncaaFBlogos/colby.png
Normal file
|
After Width: | Height: | Size: 105 KiB |
BIN
test/ncaaFBlogos/colo.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
test/ncaaFBlogos/conn.png
Normal file
|
After Width: | Height: | Size: 53 KiB |
BIN
test/ncaaFBlogos/cp.png
Normal file
|
After Width: | Height: | Size: 49 KiB |
BIN
test/ncaaFBlogos/csu.png
Normal file
|
After Width: | Height: | Size: 84 KiB |
BIN
test/ncaaFBlogos/cur.png
Normal file
|
After Width: | Height: | Size: 26 KiB |
BIN
test/ncaaFBlogos/del.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
test/ncaaFBlogos/dub.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
test/ncaaFBlogos/elm.png
Normal file
|
After Width: | Height: | Size: 42 KiB |
BIN
test/ncaaFBlogos/famu.png
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
test/ncaaFBlogos/fla.png
Normal file
|
After Width: | Height: | Size: 73 KiB |
BIN
test/ncaaFBlogos/fsu.png
Normal file
|
After Width: | Height: | Size: 56 KiB |
BIN
test/ncaaFBlogos/gri.png
Normal file
|
After Width: | Height: | Size: 34 KiB |
BIN
test/ncaaFBlogos/gt.png
Normal file
|
After Width: | Height: | Size: 23 KiB |
BIN
test/ncaaFBlogos/gtwn.png
Normal file
|
After Width: | Height: | Size: 26 KiB |
BIN
test/ncaaFBlogos/haw.png
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
test/ncaaFBlogos/how.png
Normal file
|
After Width: | Height: | Size: 25 KiB |
BIN
test/ncaaFBlogos/idho.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
BIN
test/ncaaFBlogos/isu.png
Normal file
|
After Width: | Height: | Size: 61 KiB |
BIN
test/ncaaFBlogos/jxst.png
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
test/ncaaFBlogos/lut.png
Normal file
|
After Width: | Height: | Size: 81 KiB |
BIN
test/ncaaFBlogos/mesa.png
Normal file
|
After Width: | Height: | Size: 96 KiB |
BIN
test/ncaaFBlogos/mil.png
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
test/ncaaFBlogos/mor.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
test/ncaaFBlogos/nor.png
Normal file
|
After Width: | Height: | Size: 45 KiB |
BIN
test/ncaaFBlogos/red.png
Normal file
|
After Width: | Height: | Size: 78 KiB |
BIN
test/ncaaFBlogos/sac.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
test/ncaaFBlogos/sdsu.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
test/ncaaFBlogos/sjsu.png
Normal file
|
After Width: | Height: | Size: 48 KiB |
BIN
test/ncaaFBlogos/stan.png
Normal file
|
After Width: | Height: | Size: 34 KiB |
BIN
test/ncaaFBlogos/stet.png
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
test/ncaaFBlogos/uab.png
Normal file
|
After Width: | Height: | Size: 56 KiB |
BIN
test/ncaaFBlogos/ucla.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
test/ncaaFBlogos/uga.png
Normal file
|
After Width: | Height: | Size: 31 KiB |
BIN
test/ncaaFBlogos/usa.png
Normal file
|
After Width: | Height: | Size: 38 KiB |
BIN
test/ncaaFBlogos/usc.png
Normal file
|
After Width: | Height: | Size: 5.0 KiB |
BIN
test/ncaaFBlogos/usf.png
Normal file
|
After Width: | Height: | Size: 40 KiB |
BIN
test/ncaaFBlogos/yale.png
Normal file
|
After Width: | Height: | Size: 23 KiB |