Compare commits
3 commits
d3532386fd
...
cdaba75591
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdaba75591 | ||
|
|
ce36987863 | ||
|
|
17e1f551ec |
45
app.py
45
app.py
|
|
@ -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()
|
||||||
27
config.py
27
config.py
|
|
@ -6,16 +6,27 @@ load_dotenv()
|
||||||
|
|
||||||
|
|
||||||
def str2bool(v):
|
def str2bool(v):
|
||||||
return v.lower() in ("yes", "true", "t", "1")
|
return v.lower() in ("yes", "true", "t", "1", "on")
|
||||||
|
|
||||||
|
|
||||||
class Config():
|
class Config():
|
||||||
ESPN_S2 = getenv("ESPN_S2")
|
|
||||||
LEAGUE_ID = getenv("LEAGUE_ID")
|
def __init__(self) -> None:
|
||||||
SWID = getenv("SWID")
|
self.ESPN_S2 = getenv("ESPN_S2", 0)
|
||||||
FETCH_LEAGUE = str2bool(getenv("FETCH_LEAGUE"))
|
self.LEAGUE_ID = int(getenv("LEAGUE_ID", 0))
|
||||||
SPREADSHEET_ID = getenv("SPREADSHEET_ID")
|
self.SWID = getenv("SWID", 0)
|
||||||
WORKSHEET_NAME = getenv("WORKSHEET_NAME")
|
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)
|
||||||
|
|
@ -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
|
google-auth-oauthlib==1.2.1
|
||||||
gspread==6.1.2
|
gspread==6.1.2
|
||||||
gspread-dataframe==4.0.0
|
gspread-dataframe==4.0.0
|
||||||
huey==2.5.2
|
|
||||||
pandas==2.2.3
|
pandas==2.2.3
|
||||||
python-dotenv==1.0.1
|
python-dotenv==1.0.1
|
||||||
redis==5.1.0
|
|
||||||
tabulate==0.9.0
|
tabulate==0.9.0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue