Skip to content

Commit c3fbae2

Browse files
authored
Merge pull request #4 from R-Brook/nflreadpy
Use Nflreadpy
2 parents c4d867b + 8f6478f commit c3fbae2

7 files changed

Lines changed: 80 additions & 45 deletions

File tree

.github/workflows/nextjs.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Build and deploy a Next.js site to GitHub Pages
2-
#
32
name: Deploy Next.js site to Pages
43

54
on:
@@ -42,17 +41,19 @@ jobs:
4241
pip install -r requirements.txt
4342
4443
- name: Run Python script
45-
env:
46-
BALLDONTLIEAPI_KEY: ${{ secrets.BALLDONTLIEAPI_KEY }}
47-
run: python scripts/fetch-team-data.py
44+
run: |
45+
python scripts/fetch-team-data.py
46+
python scripts/fetch-qb-data.py
4847
4948
# Upload the file the script produced
5049
- name: Upload Python output
5150
uses: actions/upload-artifact@v4
5251
with:
5352
name: python-output
5453
# File the script produces
55-
path: scripts/team-data.ts
54+
path: |
55+
scripts/team-data.ts
56+
scripts/qb-data.ts
5657
5758
# Build job
5859
build:
@@ -77,6 +78,7 @@ jobs:
7778
- name: Move Python output into project
7879
run: |
7980
cp ./python-output/team-data.ts scripts/team-data.ts
81+
cp ./python-output/qb-data.ts scripts/qb-data.ts
8082
8183
- name: Detect package manager
8284
id: detect-package-manager
@@ -120,6 +122,10 @@ jobs:
120122
- name: Install dependencies
121123
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
122124

125+
- name: Verify generated data files
126+
run: |
127+
ls -l scripts
128+
123129
- name: Build with Next.js
124130
run: ${{ steps.detect-package-manager.outputs.runner }} next build
125131

.gitignore

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
# dependencies
44
/node_modules
5-
/.pnp
6-
.pnp.*
7-
.yarn/*
8-
!.yarn/patches
9-
!.yarn/plugins
10-
!.yarn/releases
11-
!.yarn/versions
125

136
# testing
147
/coverage
@@ -26,9 +19,6 @@
2619

2720
# debug
2821
npm-debug.log*
29-
yarn-debug.log*
30-
yarn-error.log*
31-
.pnpm-debug.log*
3222

3323
# env files (can opt-in for committing if needed)
3424
.env*
@@ -39,3 +29,6 @@ yarn-error.log*
3929
# typescript
4030
*.tsbuildinfo
4131
next-env.d.ts
32+
33+
# script outputs
34+
/scripts/*.ts

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/pages/api-reference/create-next-app).
22

3+
Uses [nflreadpy](https://nflreadpy.nflverse.com/) to fetch data.
4+
35
## Getting Started
46

57
First, run the development server:

pages/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { qbData } from "@/scripts/qb-data"
12
import { teamData } from "@/scripts/team-data"
23
import Head from "next/head"
34

@@ -10,7 +11,12 @@ export default function Home() {
1011
<main>
1112
<div>
1213
<h1>NFL Stats</h1>
13-
{teamData ? "has fetched data" : "has not fetched data"}
14+
<span className="block">
15+
{teamData ? "has fetched team data" : "has not fetched team data"}
16+
</span>
17+
<span className="block">
18+
{qbData ? "has fetched QB data" : "has not fetched QB data"}
19+
</span>
1420
</div>
1521
</main>
1622
</div>

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
balldontlie
2-
python-dotenv
1+
nflreadpy
2+
python-dotenv

scripts/fetch-qb-data.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
import polars as pl
3+
import nflreadpy as nfl
4+
5+
def parse_qbs():
6+
"""
7+
Fetch current-season QB stats and write them to scripts/qb-data.ts
8+
"""
9+
try:
10+
# Fetch player stats (current season by default)
11+
player_stats = nfl.load_player_stats(summary_level="reg")
12+
13+
# Filter to QBs
14+
qb_data = (
15+
player_stats
16+
.filter(pl.col("position") == "QB")
17+
.to_dicts()
18+
)
19+
20+
# Write the data to a TypeScript file
21+
output_path = "scripts/qb-data.ts"
22+
with open(output_path, "w", encoding="utf-8") as f:
23+
f.write("// file generated by fetch-qb-data.py\n\n")
24+
f.write("export const qbData = ")
25+
json.dump(qb_data, f, indent=2)
26+
f.write(";\n")
27+
28+
print(f"Created file {output_path} with {len(qb_data)} QBs")
29+
30+
except Exception as e:
31+
print(f"ERROR: {e}")
32+
raise
33+
34+
parse_qbs()

scripts/fetch-team-data.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
import json
2-
import os
3-
from balldontlie import BalldontlieAPI
4-
from dotenv import load_dotenv
2+
import nflreadpy as nfl
53

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()
912

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

18-
# Initialise the API
19-
api = BalldontlieAPI(api_key=api_key)
21+
print(f"Created file {output_path} with {len(team_data)} teams")
2022

21-
# Fetch NFL teams data
22-
data = api.nfl.teams.list()
23+
except Exception as e:
24+
print(f"ERROR: {e}")
25+
raise
2326

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

Comments
 (0)