From ac1ac19f468303a544adb518a128dc467d8c1470 Mon Sep 17 00:00:00 2001 From: Justin Hsieh <41461549+justin-hsieh@users.noreply.github.com> Date: Thu, 25 Jan 2024 09:23:20 -0700 Subject: [PATCH 1/2] Update scores.py --- fantasy_app/scores.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/fantasy_app/scores.py b/fantasy_app/scores.py index 82defdc..48538b4 100644 --- a/fantasy_app/scores.py +++ b/fantasy_app/scores.py @@ -4,7 +4,6 @@ from dotenv import load_dotenv from .espn_api_submodule.espn_api.football.league import League - # load environment variables load_dotenv() LEAGUE_ID = os.getenv('LEAGU2') @@ -31,7 +30,6 @@ def get_year(): def current_week(): return league.current_week - def get_week(): today = datetime.date.today() weekday_number = today.weekday() @@ -44,12 +42,10 @@ def get_week(): def get_team_list(): return league.teams - def get_current_matchups(): matchups = league.box_scores(current_week()) return matchups - def get_highest(position, stat, currentweek=0): if currentweek > 0: matchups = league.box_scores(currentweek) From 3e7ed0a60d045bb9e961186e13a5c760fa699c9b Mon Sep 17 00:00:00 2001 From: Justin Hsieh <41461549+justin-hsieh@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:14:37 -0700 Subject: [PATCH 2/2] Update functions to calculate specific stats --- .gitignore | 1 + app.py | 10 +++- fantasy_app/contests.py | 44 ++++++++++----- fantasy_app/scores.py | 121 +++++++++++----------------------------- 4 files changed, 71 insertions(+), 105 deletions(-) diff --git a/.gitignore b/.gitignore index 1292bfd..b02433f 100644 --- a/.gitignore +++ b/.gitignore @@ -112,3 +112,4 @@ key.json .ipynb db.ipynb +CREDS.json \ No newline at end of file diff --git a/app.py b/app.py index b800d57..8bcfebd 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ from firebase_admin import credentials, firestore # file imports -from fantasy_app.scores import current_week, get_most_position_points, order_positions_by_points +from fantasy_app.scores import current_week, get_most_position_points, order_positions_by_points, get_highest_points from fantasy_app.contests import contest_list @@ -38,10 +38,14 @@ def calculate_most_points_post(): result_dict = {} point_request = request.get_json() contest = point_request['contest'] - week = int(point_request['week']) + week = point_request['week'] year = point_request['year'] + points = get_most_position_points( - contest_list[contest]['position'], week) + contest_list[contest]['position'], contest_list[contest]['stat'], year, week) + if contest_list[contest].get('single'): + points = get_highest_points(points) + result_dict['contest_results'] = order_positions_by_points(points) result_dict['contest'] = contest result_dict['week'] = week diff --git a/fantasy_app/contests.py b/fantasy_app/contests.py index 8fa11f5..1456571 100644 --- a/fantasy_app/contests.py +++ b/fantasy_app/contests.py @@ -1,16 +1,32 @@ contest_list = { - 'highest_qb_points' : {'position': ['QB']}, - 'total_rb_points' : {'position': ['RB']}, - 'total_wr_points' : {'position': ['WR']}, - 'highest_te_points' : {'position': ['TE']}, - 'highest_flex_points' : {'position': ['RB/WR/TE']}, - 'total_de_points' : {'position': ['DE']}, - 'total_lb_points' : {'position': ['LB']}, - 'total_punter_points' : {'position': ['P']}, - 'highest_hc_points' : {'position': ['HC']}, - 'highest_single_starter_points' : {'position' : ['QB', 'RB', 'WR','TE', 'RB/WR/TE','DE', 'LB', 'P', 'HC']}, - 'highest_bench_player_points' : {'position': ['BE']}, - 'total_defensive_points' : {'position': ['DE','LB']}, - 'total_offensive_points_flex' : {'position': ['QB', 'RB', 'WR','TE', 'RB/WR/TE']}, - 'total_offensive_points_no_flex' : {'position': ['QB', 'RB', 'WR','TE']} + 'highest_qb_points' : {'position': ['QB'], 'stat': ['']}, + 'total_rb_points' : {'position': ['RB'], 'stat': ['']}, + 'total_wr_points' : {'position': ['WR'], 'stat': ['']}, + 'highest_te_points' : {'position': ['TE'], 'stat': ['']}, + 'highest_flex_points' : {'position': ['RB/WR/TE'], 'stat': ['']}, + 'total_de_points' : {'position': ['DE'], 'stat': ['']}, + 'total_lb_points' : {'position': ['LB'], 'stat': ['']}, + 'total_punter_points' : {'position': ['P'], 'stat': ['']}, + 'highest_hc_points' : {'position': ['HC'], 'stat': ['']}, + 'highest_single_starter_points' : {'position' : ['QB', 'RB', 'WR','TE', 'RB/WR/TE','DE', 'LB', 'P', 'HC'], 'stat': [''], 'single': True}, + 'highest_bench_player_points' : {'position': ['BE'], 'stat': [''], 'single': True}, + 'total_defensive_points' : {'position': ['DE','LB'], 'stat': ['']}, + 'total_offensive_points_flex' : {'position': ['QB', 'RB', 'WR','TE', 'RB/WR/TE'], 'stat': ['']}, + 'total_offensive_points_no_flex' : {'position': ['QB', 'RB', 'WR','TE'], 'stat': ['']}, + 'total_points' : {'position' : ['QB', 'RB', 'WR','TE', 'RB/WR/TE','DE', 'LB', 'P', 'HC'], 'stat': ['']}, + 'most_qb_passing_yards' : {'position': ['QB'], 'stat': ['passingYards']}, + 'most_receptions_by_wr' : {'position': ['WR'], 'stat': ['receivingReceptions']}, + 'most_receptions_by_rb' : {'position': ['RB'], 'stat': ['receivingReceptions']}, + 'most_receptions_by_wr_and_rb' : {'position': ['WR', 'RB'], 'stat': ['receivingReceptions']}, + 'most_punts_downed_within_20' : {'position': ['P'], 'stat': ['puntsInsideThe20']}, + 'total_punter_yards' : {'position': ['P'], 'stat': ['puntYards']}, + 'total_lb_tackles' : {'position': ['LB'], 'stat': ['defensiveTotalTackles']}, + 'total_de_tackles' : {'position': ['DE'], 'stat': ['defensiveTotalTackles']}, + 'total_lb_and_de_tackles' : {'position': ['LB', 'DE'], 'stat': ['defensiveTotalTackles']}, + 'total_sacks' : {'position': ['LB', 'DE'], 'stat': ['defensiveSacks']}, + 'total_rb_and_wr_yards' : {'position': ['RB', 'WR'], 'stat': ['rushingYards', 'receivingYards']}, + 'most_te_receiving_yards' : {'position': ['TE'], 'stat': ['receivingYards']}, + 'most_wr_receiving_yards' : {'position': ['WR'], 'stat': ['receivingYards']}, + 'most_rb_receiving_yards' : {'position': ['RB'], 'stat': ['receivingYards']}, + 'total_rb_and_wr_tds' : {'position': ['RB', 'WR'], 'stat': ['rushingTouchdowns', 'receivingTouchdowns']} } \ No newline at end of file diff --git a/fantasy_app/scores.py b/fantasy_app/scores.py index 48538b4..589f66f 100644 --- a/fantasy_app/scores.py +++ b/fantasy_app/scores.py @@ -20,14 +20,11 @@ def get_year(): currentYear = datetime.now().year - 1 return currentYear -league = League( - league_id=LEAGUE_ID, - year=get_year(), - espn_s2=ESPN_S2, - swid=SWID -) +def league_instance(input_year): + return League(league_id=LEAGUE_ID,year=input_year,espn_s2=ESPN_S2,swid=SWID) def current_week(): + league = league_instance(get_year()) return league.current_week def get_week(): @@ -40,100 +37,42 @@ def get_week(): return week def get_team_list(): + league = league_instance(get_year()) return league.teams def get_current_matchups(): + league = league_instance(get_year()) matchups = league.box_scores(current_week()) return matchups -def get_highest(position, stat, currentweek=0): - if currentweek > 0: - matchups = league.box_scores(currentweek) - else: - matchups = get_current_matchups() - currentweek = get_week() - player_dict = {} - for matchup_index, matchup in enumerate(matchups): - away = matchup.away_lineup - home = matchup.home_lineup - create_dictionary(away) - create_dictionary(home) - return player_dict - - -def create_dictionary(team_name, lineup, position, stat, player_dict): - - currentweek = get_week() - for player_index, player in enumerate(lineup): - if player.slot_position in position: - if team_name in player_dict: - if player.stats[currentweek].get('breakdown'): - if player.stats[currentweek]['breakdown'].get(stat): - player_dict[team_name]['player'].update( - {(player.name): player.stats[currentweek]['breakdown'][stat]}) - else: - player_dict[team_name]['player'].update( - {(player.name): 0}) - else: - player_dict[team_name]['player'].update({(player.name): 0}) - else: - if player.stats[currentweek].get('breakdown'): - if player.stats[currentweek]['breakdown'].get(stat): - player_dict[team_name] = {'player': { - (player.name): player.stats[currentweek]['breakdown'][stat]}} - else: - player_dict[team_name] = {'player': {(player.name): 0}} - else: - player_dict[team_name] = {'player': {(player.name): 0}} - return - ''' - for player_index, player in enumerate(home): - if player.slot_position in position: - team = matchup.home_team.team_name - if team in player_dict: - if player.stats[currentweek].get('breakdown'): - if player.stats[currentweek]['breakdown'].get(stat): - player_dict[team]['player'].update( - {(player.name): player.stats[currentweek]['breakdown'][stat]}) - else: - player_dict[team]['player'].update( - {(player.name): 0}) - else: - player_dict[team]['player'].update({(player.name): 0}) - else: - if player.stats[currentweek].get('breakdown'): - if player.stats[currentweek]['breakdown'].get(stat): - player_dict[team] = {'player': { - (player.name): player.stats[currentweek]['breakdown'][stat]}} - else: - player_dict[team] = {'player': {(player.name): 0}} - else: - player_dict[team] = {'player': {(player.name): 0}} - - return player_dict - ''' -def new_dict(position, current_dict, lineup): - count = 0 - total_score = 0 +def new_dict(position, current_dict, lineup, stat, week): + total = 0 player_list = [] for player in lineup: if player.slot_position in position: - - count += 1 temp_dict = { - 'name': player.name, - 'score': player.points, - 'game_played': player.game_played + 'name': player.name, + 'game_played': player.game_played } - - total_score += player.points + if stat[0] != '': + for i in stat: + if player.stats[week].get('breakdown'): + if player.stats[week]['breakdown'].get(i): + temp_dict['score'] = player.stats[week]['breakdown'][i] + total += player.stats[week]['breakdown'][i] + else: + temp_dict['score'] = 0 + else: + temp_dict['score'] = player.points + total += player.points + player_list.append(temp_dict) - - current_dict['total_score'] = total_score - current_dict['players'] = player_list + current_dict['total_score'] = total + current_dict['players'] = player_list return current_dict -def get_most_position_points(position, currentweek=0): +def get_most_position_points(position, stat, year, currentweek=0): + league = league_instance(int(year)) matchups = league.box_scores(currentweek) matchups_list = [] @@ -148,13 +87,19 @@ def get_most_position_points(position, currentweek=0): player_dict1['team'] = matchup.home_team.team_name away_dict = new_dict( - position, player_dict, away) + position, player_dict, away, stat, currentweek) home_dict = new_dict( - position, player_dict1, home) + position, player_dict1, home, stat, currentweek) matchups_list.append(away_dict) matchups_list.append(home_dict) return matchups_list +def get_highest_points(points_dict): + for entry in points_dict: + entry['players'] = [max(entry['players'], key=(lambda x: x['score']))] + entry['total_score'] = entry['players'][0]['score'] + return points_dict + def order_positions_by_points(score_list): sorted_list = sorted(score_list, key=operator.itemgetter( 'total_score'), reverse=True)