Compare commits

...

3 commits

3 changed files with 37 additions and 39 deletions

45
app.py
View file

@ -1,67 +1,56 @@
from config import Config as cfg
from config import 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:
def aggregate_positional_points(lineup: list, position: str) -> int:
response = 0
for player in lineup:
if player.lineupSlot == position:
response = response + player.points
response += player.points
return response
def extract_matchup_box_scores(league: League) -> dict:
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,
'AWAY TEAM': matchup.away_team.team_name, # pyright: ignore[reportAttributeAccessIssue]
'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,
'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': extract_positional_data(matchup.home_lineup, 'K'),
'HOME TEAM BENCH': extract_positional_data(matchup.home_lineup, 'BE')
'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(cfg.SPREADSHEET_ID)
spreadsheet = gc.open_by_key(str(cfg.SPREADSHEET_ID))
worksheet = spreadsheet.worksheet("FantasyData")
set_with_dataframe(worksheet, df)
@huey.periodic_task(crontab(hour='13', minute='0'))
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))
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()

View file

@ -6,16 +6,27 @@ load_dotenv()
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
return v.lower() in ("yes", "true", "t", "1", "on")
class Config():
ESPN_S2 = getenv("ESPN_S2")
LEAGUE_ID = getenv("LEAGUE_ID")
SWID = getenv("SWID")
FETCH_LEAGUE = str2bool(getenv("FETCH_LEAGUE"))
SPREADSHEET_ID = getenv("SPREADSHEET_ID")
WORKSHEET_NAME = getenv("WORKSHEET_NAME")
def __init__(self) -> None:
self.ESPN_S2 = getenv("ESPN_S2", 0)
self.LEAGUE_ID = int(getenv("LEAGUE_ID", 0))
self.SWID = getenv("SWID", 0)
self.FETCH_LEAGUE = str2bool(getenv("FETCH_LEAGUE", 0))
self.SPREADSHEET_ID = getenv("SPREADSHEET_ID", 0)
self.SHEET_NAME = getenv("WORKSHEET_NAME", 0)
self.check_values()
def check_values(self):
for attribute, value in self.__dict__.items():
if value == 0:
raise Exception("ERROR: Unproperly set environment variable {attribute}, please review".format(attribute=attribute))
cfg = Config()
try:
cfg = Config()
except Exception as e:
print(e)

View file

@ -1,9 +1,7 @@
-e git+https://github.com/cwendt94/espn-api@8e131e7ccaa843abac539948c44f9d45bfcee764#egg=espn_api
-e git+https://github.com/cwendt94/espn-api#egg=espn_api
google-auth-oauthlib==1.2.1
gspread==6.1.2
gspread-dataframe==4.0.0
huey==2.5.2
pandas==2.2.3
python-dotenv==1.0.1
redis==5.1.0
tabulate==0.9.0