|
| 1 | +name: Load Weekly NFL Data |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + season: |
| 7 | + description: "Season year (e.g., 2024)" |
| 8 | + required: false |
| 9 | + default: "2024" |
| 10 | + week: |
| 11 | + description: "Week number (leave blank for all)" |
| 12 | + required: false |
| 13 | + default: "" |
| 14 | + schedule: |
| 15 | + - cron: '0 10 * * TUE' |
| 16 | + |
| 17 | +jobs: |
| 18 | + load: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v5 |
| 26 | + with: |
| 27 | + python-version: '3.11' |
| 28 | + cache: pip |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: | |
| 32 | + python -m pip install --upgrade pip |
| 33 | + pip install -r requirements.txt -r requirements-dev.txt |
| 34 | +
|
| 35 | + - name: Run weekly loader (nflreadpy/nfl_data_py) |
| 36 | + shell: bash |
| 37 | + run: | |
| 38 | + ARGS=( ) |
| 39 | + if [ -n "${{ inputs.season }}" ]; then ARGS+=(--season "${{ inputs.season }}"); fi |
| 40 | + if [ -n "${{ inputs.week }}" ]; then ARGS+=(--week "${{ inputs.week }}"); fi |
| 41 | + python scripts/load_weekly_data.py "${ARGS[@]}" --output nfl_weekly_stats.csv |
| 42 | +
|
| 43 | + - name: Upload CSV artifact |
| 44 | + if: success() |
| 45 | + uses: actions/upload-artifact@v4 |
| 46 | + with: |
| 47 | + name: nfl-weekly-stats |
| 48 | + path: nfl_weekly_stats.csv |
| 49 | + |
| 50 | + - name: Load CSV into Postgres (optional) |
| 51 | + if: ${{ success() && secrets.PGHOST && secrets.PGUSER && secrets.PGDATABASE }} |
| 52 | + env: |
| 53 | + PGHOST: ${{ secrets.PGHOST }} |
| 54 | + PGPORT: ${{ secrets.PGPORT }} |
| 55 | + PGUSER: ${{ secrets.PGUSER }} |
| 56 | + PGPASSWORD: ${{ secrets.PGPASSWORD }} |
| 57 | + PGDATABASE: ${{ secrets.PGDATABASE }} |
| 58 | + run: | |
| 59 | + python - << 'PY' |
| 60 | + import os, sys |
| 61 | + import pandas as pd |
| 62 | + from sqlalchemy import create_engine |
| 63 | +
|
| 64 | + csv_path = 'nfl_weekly_stats.csv' |
| 65 | + if not os.path.exists(csv_path): |
| 66 | + print(f"CSV not found: {csv_path}") |
| 67 | + sys.exit(1) |
| 68 | +
|
| 69 | + user = os.environ['PGUSER'] |
| 70 | + pwd = os.environ['PGPASSWORD'] |
| 71 | + host = os.environ['PGHOST'] |
| 72 | + port = os.environ.get('PGPORT', '5432') |
| 73 | + db = os.environ['PGDATABASE'] |
| 74 | +
|
| 75 | + url = f"postgresql+psycopg2://{user}:{pwd}@{host}:{port}/{db}" |
| 76 | + engine = create_engine(url) |
| 77 | +
|
| 78 | + df = pd.read_csv(csv_path) |
| 79 | + # Replace 'nfl_weekly_stats' with your target table if different |
| 80 | + df.to_sql('nfl_weekly_stats', engine, if_exists='append', index=False) |
| 81 | + print('Loaded CSV into Postgres table nfl_weekly_stats') |
| 82 | + PY |
0 commit comments