wffrg/app.py

56 lines
2.1 KiB
Python

from config import cfg
from datetime import datetime
from espn_api.football import League
from gspread_dataframe import set_with_dataframe
from service import gc
from tabulate import tabulate
import pandas as pd
def aggregate_positional_points(lineup: list, position: str) -> int:
response = 0
for player in lineup:
if player.lineupSlot == position:
response += player.points
return response
def extract_weekly_box_scores(league: League) -> list:
result = []
for week in range(1, league.current_week + 1):
matchups = league.box_scores(week=week)
for matchup in matchups:
result.append({
'WEEK #': week,
'AWAY TEAM': matchup.away_team.team_name, # pyright: ignore[reportAttributeAccessIssue]
'AWAY TEAM SCORE': matchup.away_score,
'AWAY TEAM KICKER': aggregate_positional_points(matchup.away_lineup, 'K'),
'AWAY TEAM BENCH': aggregate_positional_points(matchup.away_lineup, 'BE'),
'HOME TEAM': matchup.home_team.team_name, # pyright: ignore[reportAttributeAccessIssue]
'HOME TEAM SCORE': matchup.home_score,
'HOME TEAM KICKER': aggregate_positional_points(matchup.home_lineup, 'K'),
'HOME TEAM BENCH': aggregate_positional_points(matchup.home_lineup, 'BE')
})
return result
def write_to_google_spreadsheet(df: pd.DataFrame):
spreadsheet = gc.open_by_key(str(cfg.SPREADSHEET_ID))
worksheet = spreadsheet.worksheet("FantasyData")
set_with_dataframe(worksheet, df)
def process_daily_report():
league = League(league_id=cfg.LEAGUE_ID,
year=datetime.now().year,
espn_s2=cfg.ESPN_S2,
swid=cfg.SWID,
fetch_league=cfg.FETCH_LEAGUE)
matchup = extract_weekly_box_scores(league=league)
df = pd.DataFrame(matchup)
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False)) # pyright: ignore[reportArgumentType]
write_to_google_spreadsheet(df=df)
process_daily_report()