|
1 | 1 | import json |
2 | | -import os |
3 | | -from balldontlie import BalldontlieAPI |
4 | | -from dotenv import load_dotenv |
| 2 | +import nflreadpy as nfl |
5 | 3 |
|
6 | | -# Only load .env if it exists |
7 | | -if os.path.exists(".env"): |
8 | | - load_dotenv() |
| 4 | +def parse_teams(): |
| 5 | + """ |
| 6 | + Fetch NFL teams via the API, convert them to dictionaries, |
| 7 | + and write to a TypeScript file (scripts/team-data.ts). |
| 8 | + """ |
| 9 | + try: |
| 10 | + # Fetch data (Polars DataFrame) |
| 11 | + team_data = nfl.load_teams().to_dicts() |
9 | 12 |
|
10 | | -# Fetch the API key from environment |
11 | | -api_key = os.getenv("BALLDONTLIEAPI_KEY") |
12 | | -if not api_key: |
13 | | - raise ValueError( |
14 | | - "BALLDONTLIEAPI_KEY not set. " |
15 | | - "Set it in a local .env file or as a GitHub Actions secret." |
16 | | - ) |
| 13 | + # Write the data to a TypeScript file |
| 14 | + output_path = "scripts/team-data.ts" |
| 15 | + with open(output_path, "w", encoding="utf-8") as f: |
| 16 | + f.write("// file generated by fetch-team-data.py\n\n") |
| 17 | + f.write("export const teamData = ") |
| 18 | + json.dump(team_data, f, indent=2) |
| 19 | + f.write(";\n") |
17 | 20 |
|
18 | | -# Initialise the API |
19 | | -api = BalldontlieAPI(api_key=api_key) |
| 21 | + print(f"Created file {output_path} with {len(team_data)} teams") |
20 | 22 |
|
21 | | -# Fetch NFL teams data |
22 | | -data = api.nfl.teams.list() |
| 23 | + except Exception as e: |
| 24 | + print(f"ERROR: {e}") |
| 25 | + raise |
23 | 26 |
|
24 | | -# Convert each team to a dictionary using Pydantic v2+ `.model_dump()` |
25 | | -team_data = [team.model_dump() for team in data.data] |
26 | | - |
27 | | -# Write the data to a TypeScript file |
28 | | -with open("scripts/team-data.ts", "w") as f: |
29 | | - f.write("// file generated by fetch-qb-data.py\n") |
30 | | - f.write("\n") |
31 | | - f.write("export const teamData = ") |
32 | | - json.dump(team_data, f, indent=2) |
33 | | - f.write("\n") |
| 27 | +parse_teams() |
0 commit comments