Initial import

This commit is contained in:
Carlos 2024-09-25 23:33:34 -07:00
commit f07ab5ddd2
6 changed files with 136 additions and 0 deletions

4
.env-template Normal file
View file

@ -0,0 +1,4 @@
ESPN_S2 = 'S2 key here'
SWID = 'SWID key here'
LEAGUE_ID = 'League ID here'
FETCH_LEAGUE = 'true'

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
__pycache__
.venv
.env

40
README.md Normal file
View file

@ -0,0 +1,40 @@
# Weekly Fantasy Football Report Generator
## Install
Required: Python 3.8 or greater
- Clone this repository: `git clone https://forgejo.digitalhippo.tech/wffrg`
- Create a virtual environment
- `cd wffrg`
- `python -m venv .venv` or `python3 -m venv .venv`
- Activvate virtual environment
- MacOS/Linux: `. .venv/bin/active`
- Windows
- Command Prompt: `.venv\Scripts\activate.bat`
- Powershell: `.venv\Scripts\activate.ps1`
- Install requirements
- `pip install -r requirements.txt`
## Configure
Copy `.env-template` as `.env` file and enter the following items:
- `ESPN_S2`: S2 key from ESPN
- `SWID`: SWID key from ESPN
- `LEAGUE_ID`: League ID for your Fantasy League from ESPN
- `FETCH_LEAGE`: Generally, leave as `true` unless you know what you're doing
For more information on how to retrieve these details, head on over to GitHub and [review this discussion](https://github.com/cwendt94/espn-api/discussions/150).
## Execute
The following arguments are available and optional
- `-wo` or `--weekly-override`: Provide a week (as an `int`) to override data generation, default is current week.
For example, if the current week is week 10 but I want data from week 8:
- `python app.py -wo 8` or `python app.py --weekly-override 8`
Otherwise, for current week:
- `python app.py`

67
app.py Normal file
View file

@ -0,0 +1,67 @@
from argparse import ArgumentParser
from config import Config as cfg
from datetime import datetime
from espn_api.football import League
from time import strftime
import pandas as pd
def check_int(s):
if s is None:
return s
if s[0] in ('-', '+'):
return s[1:].isdigit()
return s.isdigit()
def extract_positional_data(lineup: list, position: str) -> int:
response = 0
for player in lineup:
if player.lineupSlot == position:
response = response + player.points
return response
def extract_matchup_box_scores(league: League, week_override: int = None) -> dict:
week_to_process = league.current_week
if week_override:
week_to_process = week_override
matchups = league.box_scores(week=week_to_process)
result = []
for matchup in matchups:
result.append({
'away_team': matchup.away_team.team_name,
'away_score': matchup.away_score,
'away_team_kicker_score': extract_positional_data(matchup.away_lineup, 'K'),
'away_team_bench_score': extract_positional_data(matchup.away_lineup, 'BE'),
'home_team': matchup.home_team.team_name,
'home_team_score': matchup.home_score,
'home_team_kicker_score': extract_positional_data(matchup.home_lineup, 'K'),
'home_team_bench_score': extract_positional_data(matchup.home_lineup, 'BE')
})
return result
if __name__ == "__main__":
parser = ArgumentParser(description='Weekly Report Generator for Fantasy Football')
parser.add_argument('-wo','--weekly-override', help='Numeric value that overrides which fantasy week to generate data from', required=False)
args = vars(parser.parse_args())
timestamp = strftime("%Y%m%d_%H%M%S")
weekly_value = args['weekly_override']
league = League(league_id=cfg.LEAGUE_ID,
year=datetime.now().year,
espn_s2=cfg.ESPN_S2,
swid=cfg.SWID,
fetch_league=cfg.FETCH_LEAGUE)
if check_int(weekly_value):
print(f"Override applied, using week {weekly_value}")
else:
weekly_value = league.current_week
print(f'Using current week: {weekly_value}')
matchup = extract_matchup_box_scores(league=league, week_override=int(weekly_value))
df = pd.DataFrame.from_dict(matchup)
df.to_csv(f'espn_fantasy_football_week_{weekly_value}_{timestamp}.csv', index=False, float_format='%.2f')

19
config.py Normal file
View file

@ -0,0 +1,19 @@
from dotenv import load_dotenv
from os import getenv
load_dotenv()
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
class Config():
ESPN_S2 = getenv("ESPN_S2")
LEAGUE_ID = getenv("LEAGUE_ID")
SWID = getenv("SWID")
FETCH_LEAGUE = str2bool(getenv("FETCH_LEAGUE"))
cfg = Config()

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
-e git+https://github.com/cwendt94/espn-api@8e131e7ccaa843abac539948c44f9d45bfcee764#egg=espn_api
pandas==2.2.3
python-dotenv==1.0.1