commit f07ab5ddd2050965a38716bdfe6a5bfaebe081da Author: carlos Date: Wed Sep 25 23:33:34 2024 -0700 Initial import diff --git a/.env-template b/.env-template new file mode 100644 index 0000000..81fab99 --- /dev/null +++ b/.env-template @@ -0,0 +1,4 @@ +ESPN_S2 = 'S2 key here' +SWID = 'SWID key here' +LEAGUE_ID = 'League ID here' +FETCH_LEAGUE = 'true' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b9192d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +.venv +.env diff --git a/README.md b/README.md new file mode 100644 index 0000000..63610c5 --- /dev/null +++ b/README.md @@ -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` + diff --git a/app.py b/app.py new file mode 100644 index 0000000..2cee5e4 --- /dev/null +++ b/app.py @@ -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') diff --git a/config.py b/config.py new file mode 100644 index 0000000..62e6778 --- /dev/null +++ b/config.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..afe0781 --- /dev/null +++ b/requirements.txt @@ -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