wffrg/app.py
2024-09-26 09:07:51 -07:00

66 lines
2.4 KiB
Python

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) -> dict:
matchups = league.box_scores(week=week_override)
result = []
for matchup in matchups:
result.append({
'Week': week_override,
'Away Team': matchup.away_team.team_name,
'Away Team 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')