def _json_league_players(league_id, season): path = 'nflleague/espn-league-json/{}/{}/lineup_by_player.json' comb = get_json(path.format(league_id, season), {}) orig = deepcopy(nflleague.players) for pid in orig.keys(): orig[pid]['lineup'] = comb[pid] return orig
def gen_player_stats(season, week, player_id, team, game=None): fp = 'nflleague/espn-league-json/cache/C{}.json' filepath = fp.format(player_id) cache = get_json(filepath, {}) game_eid = nflleague.players[player_id]['schedule'].get( str(season), {}).get(str(week), 'bye') week, season = str(week), str(season) if season not in cache.keys(): cache[season] = {} if week not in cache[season].keys() or (season, week) == (str( nflleague.c_year), str(nflleague.c_week)): if game_eid == 'bye': cache[season][week] = {'BYE': True} save_json(filepath, cache) return cache[season][week] if game == None: game = nflgame.game.Game(game_eid) game_status = get_game_status(game) #Only update if game is currently playing or player stats haven't been cached yet if game_status in ['NOT PLAYED', 'PREGAME']: return {} if game_status in ['PLAYING', 'HALFTIME' ] or week not in cache[season].keys(): player = None if player_id in nflleague.players: player = Player(season, week, player_id) print('Caching Player {}, {} {} (Y:{} W:{})'.format( player.full_name, team, player.position, season, week)) play_stats = nflgame.combine_max_stats([game]) player_stats = list(play_stats.filter(playerid=player_id)) if len(player_stats) != 0: cache[season][week] = player_stats[0].stats else: cache[season][week] = {} #Any specialty stats that need to be broken down by play or some other metric can be added here if player.position == 'K': #Need to break down kicker scoring by play here because most efficient way to find length of indvl field goal. #Adds num of field goals made in 0-39,40-49,50+ ranges to kicker's stats dictionary. play_stats = nflgame.combine_plays([game]) plays = list( filter(lambda p: p.has_player(player_id), play_stats)) cache[season][week] = defaultdict(int, cache[season][week]) for play in plays: if 'kicking_fgm' in play._stats: if play._stats['kicking_fgm_yds'] <= 39: cache[season][week]['kicking_fgm_0_39'] += 1 elif play._stats['kicking_fgm_yds'] <= 49: cache[season][week]['kicking_fgm_40_49'] += 1 elif play._stats['kicking_fgm_yds'] >= 50: cache[season][week]['kicking_fgm_50_100'] += 1 elif 'kicking_fgmissed' in play._stats and int( play._stats['kicking_fgmissed_yds']) <= 39: cache[season][week]['kicking_fgmissed_0_39'] += 1 save_json(filepath, cache) return cache[season][week]
def _create_players(league_id, season, games): path = 'nflleague/espn-league-json/{}/{}/lineup_by_player.json' league_players = get_json(path.format(league_id, season), {}) players = {} for pid, data in nflleague.players.iteritems(): new = PlayerMeta(league_id, season, games, data) new._add_league_data(league_players.get(pid, {})) players[pid] = new return players
def mem_stats(player): fp = 'nflleague/espn-league-json/cache/C{}.json'.format(player.player_id) cache = get_json(fp, {}) season, week = str(player.season), str(player.week) game_status = get_game_status(player.game) if game_status in ['NOT PLAYED', 'PREGAME'] or player.schedule == 'bye': return {} if season not in cache.keys(): cache[season] = {} if week not in cache[season] or game_status in ['PLAYING', 'HALFTIME']: print('Caching {}, (Y:{} W:{})'.format(str(player), season, week)) player_stats = player.game.max_player_stats().playerid( player.player_id) if player_stats != None: cache[season][week] = player_stats.stats else: cache[season][week] = {} if player.position == 'K': #Need to break down kicker scoring by play here because most efficient way to find length of indvl field goal. #Adds num of field goals made in 0-39,40-49,50+ ranges to kicker's stats dictionary. plays = filter(lambda x: x.has_player(player.player_id), list(player.game.drives.plays())) #play_stats=nflgame.combine_plays([game]) #plays=list(filter(lambda p: p.has_player(player_id),play_stats)) cache[season][week] = defaultdict(int, cache[season][week]) for play in plays: if 'kicking_fgm' in play._stats: if play._stats['kicking_fgm_yds'] <= 39: cache[season][week]['kicking_fgm_0_39'] += 1 elif play._stats['kicking_fgm_yds'] <= 49: cache[season][week]['kicking_fgm_40_49'] += 1 elif play._stats['kicking_fgm_yds'] >= 50: cache[season][week]['kicking_fgm_50_100'] += 1 elif 'kicking_fgmissed' in play._stats and int( play._stats['kicking_fgmissed_yds']) <= 39: cache[season][week]['kicking_fgmissed_0_39'] += 1 save_json(fp, cache) return cache[season][week]
def generate_week_players(): s, w = nflgame.live.current_year_and_week() week_players = get_json('nflleague/players.json', identity_player()) nflgame_players = get_json(nflgame.player._player_json_file, {}) if len(week_players) == 1: for pid, plyr in nflgame_players.iteritems(): if plyr.get('team', False): team = plyr.get('team') if plyr.get('team') != 'JAC' else 'JAX' plyr['team'] = {str(s): {str(w): team}} plyr['position'] = { str(s): { str(w): plyr.get('position', 'NONE') } } else: plyr['position'] = {} plyr['team'] = {} plyr['schedule'] = {} plyr['gsis_name'] = '.'.join( [plyr['first_name'][0], plyr['last_name']]) week_players[pid] = plyr for season in range(2014, s + 1): for week in range(1, 18): if (season, week) == (s, w): break print(season, week) if str(season) in week_players['00-0000000']['team'].keys(): if str(week) in week_players['00-0000000']['team'][str( season)].keys(): continue print('S:{} W:{} passed'.format(season, week)) else: week_players['00-0000000']['team'][str(season)][str( week)] = 'NA' week_players['00-0000000']['position'][str(season)][str( week)] = 'NA' else: week_players['00-0000000']['team'][str(season)] = { str(week): 'NA' } week_players['00-0000000']['position'][str(season)] = { str(week): 'NA' } games = nflgame.games(season, week=week) players = nflgame.combine_max_stats(games) for plyr in players: team = nflgame.standard_team(plyr.team) if plyr.name == 'A.Robinson': print(plyr.name, week, team) if plyr.player != None: position = guess_pos(plyr) if str(season) not in week_players[ plyr.playerid]['team'].keys(): week_players[plyr.playerid]['team'][str(season)] = { str(week): team } week_players[plyr.playerid]['position'][str( season)] = { str(week): position } else: week_players[plyr.playerid]['team'][str(season)][str( week)] = team week_players[plyr.playerid]['position'][str(season)][ str(week)] = position #Fill in any unknown team weeks with most likely correct team for pid, plyr in week_players.iteritems(): if type(plyr['team']) == dict and str( season) not in plyr['team'].keys(): continue if plyr.get('position', '') == 'D/ST': continue #Only 1 position per season, so find most commonly guessed position and set to all games. For now count = collections.Counter() for pos in plyr['position'][str(season)].values(): count[pos] += 1 position = sorted(count.items(), key=lambda x: x[1], reverse=True)[0][0] for i in range(1, 18): plyr['position'][str(season)][str(i)] = position #Fill in Teams act = [] for week in range(1, 18): if str(week) in plyr['team'][str(season)].keys(): act.append(plyr['team'][str(season)][str(week)]) else: act.append(False) #Dont forget about the knile davis situation if not act[0]: T, index = False, 0 while (not T): T = act[index] index += 1 act[0] = T plyr['team'][str(season)]['1'] = T for i, team_week in enumerate(act, start=0): if not team_week: plyr['team'][str(season)][str(i + 1)] = plyr['team'][str( season)][str(i)] week_players[pid] = plyr #Build Defenses. Defenses will have constant team and position for did in nflgame.teams: if did[0] not in week_players: week_players[did[0]] = { "first_name": did[1], "full_name": did[3], "gsis_id": did[0], "gsis_name": ' '.join([did[2], 'D/ST']), "last_name": did[2], "position": "D/ST", "profile_url": "http://www.nfl.com/", "team": did[0], "schedule": {}, "status": 'ACT' } length = len(week_players) #Build game_eid dictionary for i, (pid, plyr) in enumerate(week_players.iteritems()): if type(plyr['team']) == dict and str( season) not in plyr['team'].keys(): #skip players who weren't statistically active continue week_players[pid]['schedule'][str(season)] = {} print(pid, plyr['full_name'], '{}%'.format(round((float(i) / length) * 100, 2))) for week in range(1, 18): if plyr.get('position') != 'D/ST': team = week_players[pid]['team'][str(season)][str(week)] else: team = week_players[pid]['team'] week_players[pid]['schedule'][str(season)][str( week)] = load_schedule_info(season, week, team) save_json('nflleague/players.json', week_players)
def _json_load_schedule(league_id, season): schedule = get_json( 'nflleague/espn-league-json/{}/{}/schedule.json'.format( league_id, season), {}) assert schedule != {}, 'Schedule Not Created.' return schedule
def _load_settings(league_id, season, category): settings = get_json( 'nflleague/espn-league-json/{}/{}/settings.json'.format( league_id, season), {}) assert settings != {}, 'Settings Not Created.' return settings.get(category, {})
def _json_load_owners(league_id, season): return get_json( 'nflleague/espn-league-json/{}/{}/owner_info.json'.format( league_id, season), {})
def gen_defense_stats(season, week, team, game=None): fp = 'nflleague/espn-league-json/cache/C{}.json' filepath = fp.format(team) cache = get_json(filepath, {}) game_eid = nflleague.players[team]['schedule'][str(season)][str(week)] week, season = str(week), str(season) if season not in cache.keys(): cache[season] = {} if week not in cache[season].keys() or (season, week) == (str( nflleague.c_year), str(nflleague.c_week)): if game_eid == 'bye': cache[season][week] = {'BYE': True} save_json(filepath, cache) return cache[season][week] if game == None: game = nflgame.game.Game(game_eid) game_status = get_game_status(game) if game_status in ['NOT PLAYED', 'PREGAME']: return {} if game_status in ['PLAYING', 'HALFTIME' ] or week not in cache[season].keys(): print('Caching {} Defense (Y:{} W:{})'.format(team, season, week)) players = nflgame.combine_max_stats([game]) #individual players dst = filter( lambda p: p.team == team and (p.has_cat('defense') or p.has_cat('kickret') or p.has_cat( 'puntret')), players) cache[season][week] = {} if len(dst) != 0: for dst_plyr in dst: cache[season][week][dst_plyr.playerid] = dst_plyr._stats else: cache[season][week] = {} #combines all individual player stats dicts into one team stats category for access w/o initing player objs if not cache[season][week] == {}: val = cache[season][week].values() cache[season][week]['defense'] = reduce( lambda x, y: x + y, [Counter(dstats) for dstats in val]) else: cache[season][week]['defense'] = {} #team stats if game.home == team: cache[season][week]['defense']['defense_PA'] = game.score_away opponent = game.away if game.away == team: cache[season][week]['defense']['defense_PA'] = game.score_home opponent = game.home cache[season][week]['defense']['defense_rush'], cache[season][ week]['defense']['defense_pass'] = 0, 0 dst_team = filter(lambda t: t.team == opponent, players) for off_p in dst_team: try: #Find better way DEF = [ 'DE', 'DT', 'CB', 'SS', 'FS', 'MLB', 'OLB', 'ILB', 'DB', 'T', 'RT', 'LT', 'S', 'LB' ] if off_p.player.position not in DEF: cache[season][week]['defense'][ 'defense_rush'] += off_p.rushing_yds cache[season][week]['defense'][ 'defense_pass'] += off_p.passing_yds + off_p.passing_sk_yds except Exception as err: print(err) TYDA = cache[season][week]['defense']['defense_rush'] + cache[ season][week]['defense']['defense_pass'] cache[season][week]['defense']['defense_TYDA'] = TYDA save_json(filepath, cache) return cache[season][week]
def _json_lineup_by_team(league_id, season, week, team_id): path = 'nflleague/espn-league-json/{}/{}/lineup_by_week.json' data = get_json(path.format(league_id, season), {}) return data.get(str(team_id), {}).get(str(week), {})
def _json_lineup_by_player(league_id, season, week, player_id): path = 'nflleague/espn-league-json/{}/{}/lineup_by_player.json' data = get_json(path.format(league_id, season), {}) return data.get(player_id, {}).get(str(season), {}).get(str(week), {})
def _json_week_players(): return get_json('nflleague/players.json', {})