Skip to content

Commit

Permalink
Merge pull request #27 from justin-hsieh/highest_stat
Browse files Browse the repository at this point in the history
Highest stat
  • Loading branch information
justin-hsieh authored Feb 1, 2024
2 parents 7ac102d + 3e7ed0a commit b811148
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ key.json
.ipynb

db.ipynb
CREDS.json
10 changes: 7 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
44 changes: 30 additions & 14 deletions fantasy_app/contests.py
Original file line number Diff line number Diff line change
@@ -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']}
}
125 changes: 33 additions & 92 deletions fantasy_app/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -21,17 +20,13 @@ 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():
today = datetime.date.today()
weekday_number = today.weekday()
Expand All @@ -42,102 +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 = []
Expand All @@ -152,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)
Expand Down

0 comments on commit b811148

Please sign in to comment.