Simplifying logic and updating names of functions for better description

This commit is contained in:
carlos 2025-09-04 22:36:04 -07:00
parent ce36987863
commit cdaba75591

45
app.py
View file

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