68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from config import Config as cfg
|
|
from datetime import datetime
|
|
from espn_api.football import League
|
|
from gspread_dataframe import set_with_dataframe
|
|
from huey import crontab, RedisHuey
|
|
from service import gc
|
|
from tabulate import tabulate
|
|
|
|
import pandas as pd
|
|
|
|
|
|
huey = RedisHuey('report-generator', host='redis')
|
|
|
|
|
|
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) -> dict:
|
|
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,
|
|
'AWAY TEAM SCORE': matchup.away_score,
|
|
'AWAY TEAM KICKER': extract_positional_data(matchup.away_lineup, 'K'),
|
|
'AWAY TEAM BENCH': extract_positional_data(matchup.away_lineup, 'BE'),
|
|
'HOME TEAM': matchup.home_team.team_name,
|
|
'HOME TEAM SCORE': matchup.home_score,
|
|
'HOME TEAM KICKER': extract_positional_data(matchup.home_lineup, 'K'),
|
|
'HOME TEAM BENCH': extract_positional_data(matchup.home_lineup, 'BE')
|
|
})
|
|
return result
|
|
|
|
|
|
def write_to_google_spreadsheet(df: pd.DataFrame):
|
|
spreadsheet = gc.open_by_key(cfg.SPREADSHEET_ID)
|
|
worksheet = spreadsheet.worksheet("FantasyData")
|
|
set_with_dataframe(worksheet, df)
|
|
|
|
|
|
@huey.periodic_task(crontab(hour='6'))
|
|
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_matchup_box_scores(league=league)
|
|
df = pd.DataFrame.from_dict(matchup)
|
|
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False))
|
|
write_to_google_spreadsheet(df=df)
|