Compare commits

..

11 commits

Author SHA1 Message Date
carlos 98db8d28a7 Fix misspelling 2025-09-04 22:36:55 -07:00
carlos cdaba75591 Simplifying logic and updating names of functions for better description 2025-09-04 22:36:04 -07:00
carlos ce36987863 Updated requirements, removing redis and huey dependencies 2025-09-04 22:35:14 -07:00
carlos 17e1f551ec Added a smarter configuration file 2025-09-04 22:34:52 -07:00
Carlos d3532386fd Update template 2024-09-30 23:59:38 -07:00
Carlos 082f905a25 Delete wip.ipynb 2024-09-30 23:58:17 -07:00
Carlos f8ef55628c Delete docker-compose.yml 2024-09-30 23:57:03 -07:00
Carlos 39e6e71da1 Updated hour to accomodate TZ 2024-09-30 23:40:34 -07:00
Carlos 7115648754 Added minutes 2024-09-30 23:39:10 -07:00
Carlos 2cbdbed180 asdfasdf 2024-09-30 23:38:22 -07:00
Carlos 6342be9def Merge pull request 'feature-huey' (#1) from feature-huey into main
Reviewed-on: #1
2024-09-30 23:30:07 -07:00
6 changed files with 40 additions and 212 deletions

View file

@ -2,3 +2,5 @@ ESPN_S2 = 'S2 key here'
SWID = 'SWID key here' SWID = 'SWID key here'
LEAGUE_ID = 'League ID here' LEAGUE_ID = 'League ID here'
FETCH_LEAGUE = 'true' FETCH_LEAGUE = 'true'
SPREADSHEET_ID = 'Google Sheets ID here'
WORKSHEET_NAME = 'Worksheet name here'

View file

@ -24,7 +24,7 @@ Copy `.env-template` as `.env` file and enter the following items:
- `ESPN_S2`: S2 key from ESPN - `ESPN_S2`: S2 key from ESPN
- `SWID`: SWID key from ESPN - `SWID`: SWID key from ESPN
- `LEAGUE_ID`: League ID for your Fantasy League from ESPN - `LEAGUE_ID`: League ID for your Fantasy League from ESPN
- `FETCH_LEAGE`: Generally, leave as `true` unless you know what you're doing - `FETCH_LEAGUE`: Generally, leave as `true` unless you know what you're doing
For more information on how to retrieve these details, head on over to GitHub and [review this discussion](https://github.com/cwendt94/espn-api/discussions/150). For more information on how to retrieve these details, head on over to GitHub and [review this discussion](https://github.com/cwendt94/espn-api/discussions/150).

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='6'))
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()

View file

@ -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)

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 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

172
wip.ipynb
View file

@ -1,172 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"from argparse import ArgumentParser\n",
"from config import Config as cfg\n",
"from datetime import datetime\n",
"from espn_api.football import League\n",
"from gspread_dataframe import get_as_dataframe, set_with_dataframe\n",
"from service import gc\n",
"from time import strftime\n",
"\n",
"import pandas as pd\n",
"\n",
"\n",
"def check_int(s):\n",
" if s is None:\n",
" return s\n",
" if s[0] in ('-', '+'):\n",
" return s[1:].isdigit()\n",
" return s.isdigit()\n",
"\n",
"\n",
"def extract_positional_data(lineup: list, position: str) -> int:\n",
" response = 0\n",
" for player in lineup:\n",
" if player.lineupSlot == position:\n",
" response = response + player.points\n",
" return response\n",
"\n",
"\n",
"def extract_matchup_box_scores(league: League) -> dict:\n",
" result = []\n",
" for week in range(1, league.current_week + 1):\n",
" matchups = league.box_scores(week=week)\n",
" for matchup in matchups:\n",
" result.append({\n",
" 'WEEK #': week,\n",
" 'AWAY TEAM': matchup.away_team.team_name,\n",
" 'AWAY TEAM SCORE': matchup.away_score,\n",
" 'AWAY TEAM KICKER': extract_positional_data(matchup.away_lineup, 'K'),\n",
" 'AWAY TEAM BENCH': extract_positional_data(matchup.away_lineup, 'BE'),\n",
" 'HOME TEAM': matchup.home_team.team_name,\n",
" 'HOME TEAM SCORE': matchup.home_score,\n",
" 'HOME TEAM KICKER': extract_positional_data(matchup.home_lineup, 'K'),\n",
" 'HOME TEAM BENCH': extract_positional_data(matchup.home_lineup, 'BE')\n",
" })\n",
" return result\n",
"\n",
"\n",
"def write_to_google_spreadsheet(df: pd.DataFrame):\n",
" spreadsheet = gc.open_by_key(cfg.SPREADSHEET_ID)\n",
" worksheet = spreadsheet.worksheet(\"FantasyData\")\n",
" set_with_dataframe(worksheet, df)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"league = League(league_id=cfg.LEAGUE_ID, \n",
" year=datetime.now().year, \n",
" espn_s2=cfg.ESPN_S2, \n",
" swid=cfg.SWID, \n",
" fetch_league=cfg.FETCH_LEAGUE)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"matchup = extract_matchup_box_scores(league=league)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame.from_dict(matchup) "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"#spreadsheet = gc.open_by_key(cfg.SPREADSHEET_ID)\n",
"spreadsheet = gc.open_by_key('1vBtceabKqc0QAs7MOgeg-q0FSHXIbNf31B8XEgzy6s8')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"worksheet = spreadsheet.worksheet(\"FantasyData\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"set_with_dataframe(worksheet, df)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def next_available_row(worksheet):\n",
" str_list = list(filter(None, worksheet.col_values(1)))\n",
" return str(len(str_list)+1)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'7'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next_available_row(worksheet=worksheet)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}