def get_stats_projections_and_diff_for_player(player_id, scoring_settings): stats = Stats() player_pts_by_week = {} for week in all_weeks_stats: player_week_stats = stats.get_player_week_stats(week, player_id) player_pts_by_week[week[0]['week']] = {"stats": {}, "projections": {}} if player_week_stats is not None: pts = utils.calculate_points(scoring_settings, player_week_stats['stats']) player_pts_by_week[week[0]['week']]['stats'] = round(pts, 2) else: player_pts_by_week[week[0]['week']]['stats'] = 'BYE' for week in all_weeks_projections: player_week_projections = stats.get_player_week_projections( week, player_id) if player_week_projections is not None: pts = utils.calculate_points(scoring_settings, player_week_projections['stats']) player_pts_by_week[week[0]['week']]['projections'] = round(pts, 2) else: player_pts_by_week[week[0]['week']]['projections'] = 'BYE' for week, stats in player_pts_by_week.items(): if stats['stats'] != 'BYE': player_pts_by_week[week]["difference"] = round( float(player_pts_by_week[week]['stats']) - float(player_pts_by_week[week]['projections']), 2) return player_pts_by_week
def update_stats(path, week): print("Updating stats.") s = Stats() curr_stats = s.get_week_stats('regular', config['seasonYear'], week) with open(path, 'w') as outfile: json.dump(curr_stats, outfile, indent=2) outfile.close() return curr_stats
def test_get_player_week_stats(): stats = Stats() week_stats = stats.get_week_stats("regular", 2018, 5) player_week_stats = stats.get_player_week_stats(week_stats, "1262") assert isinstance(player_week_stats, dict) player_week_stats = stats.get_player_week_stats(week_stats, "300000000") assert player_week_stats is None
def make_roster_dict(starters_list, bench_list): """ Takes in a teams starter list and bench list and makes a dictionary with positions. :param starters_list: List of a teams starters :param bench_list: List of a teams bench players :return: {starters:{position: []} , bench:{ position: []} } """ week = get_current_week() players = Players().get_all_players() stats = Stats() week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) roster_dict = {"starters": {}, "bench": {}} for player_id in starters_list: player = players[player_id] player_position = player["position"] player_name = player["first_name"] + " " + player["last_name"] try: player_std_score = week_stats[player_id]["pts_std"] except KeyError: player_std_score = None player_and_score_tup = (player_name, player_std_score) if player_position not in roster_dict["starters"]: roster_dict["starters"][player_position] = [player_and_score_tup] else: roster_dict["starters"][player_position].append( player_and_score_tup) for player_id in bench_list: player = players[player_id] player_position = player["position"] player_name = player["first_name"] + " " + player["last_name"] try: player_std_score = week_stats[player_id]["pts_std"] except KeyError: player_std_score = None player_and_score_tup = (player_name, player_std_score) if player_position not in roster_dict["bench"]: roster_dict["bench"][player_position] = [player_and_score_tup] else: roster_dict["bench"][player_position].append(player_and_score_tup) return roster_dict
def get_negative_starters(league_id): """ Finds all of the players that scores negative points in standard and :param league_id: Int league_id :return: Dict {"owner_name":[("player_name", std_score), ...], "owner_name":...} """ week = get_current_week() league = League(league_id) users = league.get_users() matchups = league.get_matchups(week) stats = Stats() # WEEK STATS NEED TO BE FIXED week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) players = Players() players_dict = players.get_all_players() owner_id_to_team_dict = map_users_to_team_name(users) roster_id_to_owner_id_dict = map_roster_id_to_owner_id(league_id) result_dict = {} for i, matchup in enumerate(matchups): starters = matchup["starters"] negative_players = [] for starter_id in starters: try: std_pts = week_stats[str(starter_id)]["pts_std"] except KeyError: std_pts = 0 if std_pts < 0: player_info = players_dict[starter_id] player_name = "{} {}".format(player_info["first_name"], player_info["last_name"]) negative_players.append((player_name, std_pts)) if len(negative_players) > 0: owner_id = roster_id_to_owner_id_dict[matchup["roster_id"]] if owner_id is None: team_name = "Team name not available" + str(i) else: team_name = owner_id_to_team_dict[owner_id] result_dict[team_name] = negative_players return result_dict
def get_bench_points(league_id): """ :param league_id: Int league_id :return: List [(team_name, score), ...] """ week = get_current_week() league = League(league_id) users = league.get_users() matchups = league.get_matchups(week) stats = Stats() # WEEK STATS NEED TO BE FIXED week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) owner_id_to_team_dict = map_users_to_team_name(users) roster_id_to_owner_id_dict = map_roster_id_to_owner_id(league_id) result_list = [] for matchup in matchups: starters = matchup["starters"] all_players = matchup["players"] bench = set(all_players) - set(starters) std_points = 0 for player in bench: try: std_points += week_stats[str(player)]["pts_std"] except: continue owner_id = roster_id_to_owner_id_dict[matchup["roster_id"]] if owner_id is None: team_name = "Team name not available" else: team_name = owner_id_to_team_dict[owner_id] result_list.append((team_name, std_points)) return result_list
def test_get_player_week_score(capsys): stats = Stats() week_stats = stats.get_week_stats("regular", 2018, 5) score = stats.get_player_week_score(week_stats, "GB") assert isinstance(score, dict) assert score["pts_ppr"] == None score = stats.get_player_week_score(week_stats, "1262") assert isinstance(score, dict) assert score["pts_ppr"] == None score = stats.get_player_week_score(week_stats, "5170") assert isinstance(score, dict) assert score["pts_ppr"] != None score = stats.get_player_week_score(week_stats, "30000000000") assert score is None
def get_player_stats(search_object): today = pendulum.today() starting_date = pendulum.datetime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY) if starting_date >= today: year = STARTING_YEAR else: year = int(STARTING_YEAR) - 1 stats = Stats(). get_all_stats("regular",year) bot_type = os.environ["BOT_TYPE"] if bot_type == "groupme": bot_id = os.environ["BOT_ID"] bot = GroupMe(bot_id) elif bot_type == "slack": webhook = os.environ["SLACK_WEBHOOK"] bot = Slack(webhook) elif bot_type == "discord": webhook = os.environ["DISCORD_WEBHOOK"] bot = Discord(webhook) stats_run = True player_id = search_object[0] player_name = search_object[1] position = search_object[2] team = search_object[3] if search_object[4] == None: injury_status = 'Active' else: injury_status = search_object[4] try: player = stats[player_id] except: stats_run = False pass if position not in ["QB","RB","WR","TE","DEF"]: stats_run = False if stats_run: if position is not "DEF": final_string = "{} ({} - {})\n{}\n\n".format(player_name, position, team, injury_status) else: final_string = "{} ({} - {})\n\n".format(player_name, position, team) if position is not "DEF": try: ga = int(player["gms_active"]) except: ga = 0 pass try: gp = int(player["gp"]) except: gp = 0 pass try: gs = int(player["gs"]) pass except: gs = 0 pass try: pts_half_ppr = player["pts_half_ppr"] except: pts_half_ppr = 0 pass final_string += "Fantasy Points: {}\n\nGames Active: {}\nGames Played: {}\nGames Started: {}\n\n".format(pts_half_ppr, ga, gp, gs) try: team_snaps = player["tm_off_snp"] player_snaps = player["off_snp"] snap_perc = round((player_snaps / team_snaps)*100,2) final_string += "Snap Share: {}%\n".format(snap_perc) except: pass if "QB" in position: #try: #rating = player["pass_rtg"] #final_string += "Passer Rating: {}\n".format(rating) #except: #pass try: pyards = int(player["pass_yd"]) final_string += "Passing Yards: {}\n".format(pyards) except: pass try: ptd = int(player["pass_td"]) final_string += "Passing TDs: {}\n".format(ptd) except: pass try: ryards = int(player["rush_yd"]) final_string += "Rushing Yards: {}\n".format(ryards) except: pass try: rtd = int(player["rush_td"]) final_string += "Rushing TDs: {}\n".format(rtd) except: pass try: pass_int = int(player["pass_int"]) final_string += "Interceptions {}\n".format(pass_int) except: pass try: fum = int(player["fum"]) final_string += "Fumbles: {}\n".format(fum) except: pass if "RB" in position: try: ryards = int(player["rush_yd"]) final_string += "Rushing Yards: {}\n".format(ryards) except: pass try: rtd = int(player["rush_td"]) final_string += "Rushing TDs: {}\n".format(rtd) except: pass try: fum = int(player["fum"]) final_string += "Fumbles: {}\n".format(fum) except: pass try: catch_perc = round((player["rec"]/player["rec_tgt"])*100,2) final_string += "Catch Rate: {}%\n".format(catch_perc) except: pass try: rcyards = int(player["rec_yd"]) final_string += "Receiving Yards: {}\n".format(rcyards) except: pass try: rctd = int(player["rec_td"]) final_string += "Receiving TDs: {}\n".format(rctd) except: pass if "WR" in position: try: rcyards = int(player["rec_yd"]) final_string += "Receiving Yards: {}\n".format(rcyards) except: pass try: rctd = int(player["rec_td"]) final_string += "Receiving TDs: {}\n".format(rctd) except: pass try: drop_perc = round((player["rec"]/player["rec_tgt"])*100,2) final_string += "Catch Rate: {}%\n".format(drop_perc) except: pass try: ryards = int(player["rush_yd"]) final_string += "Rushing Yards: {}\n".format(ryards) except: pass try: rtd = int(player["rush_td"]) final_string += "Rushing TDs: {}\n".format(rtd) except: pass try: fum = int(player["fum"]) final_string += "Fumbles: {}\n".format(fum) except: pass if "TE" in position: try: rcyards = int(player["rec_yd"]) final_string += "Receiving Yards: {}\n".format(rcyards) except: pass try: rctd = int(player["rec_td"]) final_string += "Receiving TDs: {}\n".format(rctd) except: pass try: drop_perc = round((player["rec"]/player["rec_tgt"])*100,2) final_string += "Catch Rate: {}%\n".format(drop_perc) except: pass try: ryards = int(player["rush_yd"]) final_string += "Rushing Yards: {}\n".format(ryards) except: pass try: rtd = int(player["rush_td"]) final_string += "Rushing TDs: {}\n".format(rtd) except: pass try: fum = int(player["fum"]) final_string += "Fumbles: {}\n".format(fum) except: pass if "K" in position: try: fga = int(player["fga"]) fgm = int(player["fgm"]) fgperc = round((fgm/fga)*100,2) final_string += "FG%: {}\n\nField Goals Attempted: {}\nField Goals Made: {}\n".format(fgperc, fga, fgm) except: pass try: fgm = int(player["fgm"]) final_string += "Field Goals Made: {}\n".format(fgm) except: pass try: fgm1 = int(player["fgm_0_19"]) final_string += "0-19: {}\n".format(fgm1) except: pass try: fgm2 = int(player["fgm_20_29"]) final_string += "20-29: {}\n".format(fgm2) except: pass try: fgm3 = int(player["fgm_30_39"]) final_string += "30-39: {}\n".format(fgm3) except: pass try: fgm4 = int(player["fgm_40_49"]) final_string += "40-49: {}\n".format(fgm4) except: pass try: fgm5 = int(player["fgm_50p"]) final_string += "50+: {}\n".format(fgm5) except: pass try: xpa = int(player["xpa"]) xpm = int(player["xpm"]) xpperc = round((xpm/xpa)*100,2) final_string += "XP%: {}\n\nXP Attempted: {}\nXP Made: {}\n".format(xpperc, xpa, xpm) except: pass if "DEF" in position: try: td = int(player["td"]) final_string += "Touchdowns: {}\n".format(td) except: pass try: ff = int(player["ff"]) final_string += "Forced Fumbles: {}\n".format(ff) except: pass try: fum_rec = int(player["fum_rec"]) final_string += "Fumbles Recoved: {}\n".format(fum_rec) except: pass try: tkl = int(player["tkl_loss"]) final_string += "Tackles For Loss: {}\n".format(tkl) except: pass try: qbh = int(player["qb_hit"]) final_string += "QB Hits: {}\n".format(qbh) except: pass try: sck = int(player["sack"]) final_string += "Sacks: {}\n".format(sck) except: pass else: if player_name == "Aaron Hernandez": final_string = "{} hung himself. Gone Forever! Aaron Hernandez.".format(player_name) elif position not in ["QB","RB","WR","TE"]: final_string = "I do not do IDP stats" else: final_string = "No {} stats found for {}".format(year, player_name) bot.send(send_any_string, final_string)
from sleeper_wrapper import League from sleeper_wrapper import Players from sleeper_wrapper import Stats import pandas as pd import numpy as np import matplotlib.pyplot as plt import six ##### # Get the player data ##### league_id = 438058453903077376 league = League(league_id) stats = Stats() players = Players() all_rules = { 'pass_2pt': 2.0, # Passing 2 point conversions 'pass_int': -2.0, # Passing interceptions 'pass_lng': 0, # Long pass 'off_snp': 68.0, # Total snaps 'gs': 1.0, # Game started 'cmp_pct': 66.7, # Completion percentage 'pass_fd': 6.0, # Passing first downs 'gp': 1.0, # Game played 'pass_sack': 1.0, # Times sacked passing 'tm_off_snp': 68.0, # Teams total offensive snaps 'wind_speed': 6.0, # Wind speed 'gms_active': 1.0, # Active during game
def week_stats(week_number, year=None): if not year: year = get_current_year() week_stats = Stats().get_week_stats('regular', year, week_number) return week_stats
def test_get_week_stats(): stats = Stats() week_stats = stats.get_week_stats('regular', 2018, '2') assert isinstance(week_stats, list)
def test_get_all_stats(capsys): stats = Stats() all_stats = stats.get_all_stats('regular', 2018) assert isinstance(all_stats, list)
def test_get_week_projections(): stats = Stats() week_projections = stats.get_week_projections("regular", 2018, "4") assert isinstance(week_projections, list)
def test_get_all_projections(): stats = Stats() projections = stats.get_all_projections("regular", "2019") assert isinstance(projections, list)
from sleeper_wrapper import League from sleeper_wrapper import Stats league = League(601091358869999616) league.get_league() league.get_users() stats = Stats() stats.get_all_stats("regular", "2020") stats.get_week_stats("regular", "2020", "1")