def get_tracking_stats(stat_measure, seasons, season_types, entity_type, **kwargs): """ stat_measure - string, options: 'Drives', 'Defense', 'CatchShoot', 'Passing', 'Possessions', 'PullUpShot', 'Rebounding', 'Efficiency', 'SpeedDistance', 'ElbowTouch', 'PostTouch', 'PaintTouch' seasons - list, ex season '2019-20' season_types - list, season types are 'Regular Season' or 'Playoffs' entity_type - string, 'player' or 'team' possible kwargs: date_from - string, optional, format - MM/DD/YYYY date_to - string, optional, format - MM/DD/YYYY opponent_team_id - int, optional, default is 0, which gets all teams returns list of dicts """ all_season_stats = [] for season in seasons: for season_type in season_types: time.sleep(2) response_json = get_tracking_response_json_for_stat_measure( stat_measure, season, season_type, entity_type, **kwargs) stats = utils.make_array_of_dicts_from_response_json( response_json, 0) for stat in stats: stat['SEASON'] = f'{season} {season_type}' all_season_stats += stats return all_season_stats
def test_make_array_of_dicts_from_response_json(): response_json = { 'resultSets': [{'headers': ['header1', 'header2'], 'rowSet': [[1, 2], [3, 4], [4, 5], [2, 1]]}] } index = 0 array_of_dicts = utils.make_array_of_dicts_from_response_json(response_json, index) assert array_of_dicts == [{'header1': 1, 'header2': 2}, {'header1': 3, 'header2': 4}, {'header1': 4, 'header2': 5}, {'header1': 2, 'header2': 1}]
def get_hustle_stats(seasons, season_types, **kwargs): """ seasons - list, ex season '2019-20' season_types - list, season types are 'Regular Season' or 'Playoffs' possible kwargs: date_from - string, optional, format - MM/DD/YYYY date_to - string, optional, format - MM/DD/YYYY returns list of dicts """ all_season_stats = [] for season in seasons: for season_type in season_types: time.sleep(2) response_json = get_hustle_response_json_for_stat_measure( season, season_type, **kwargs) stats = utils.make_array_of_dicts_from_response_json( response_json, 0) for stat in stats: stat['SEASON'] = f'{season} {season_type}' all_season_stats += stats return all_season_stats
def get_tracking_shot_stats(entity_type, seasons, season_types, **kwargs): """ entity_type - string, player, team, opponent seasons - list, ex season '2019-20' season_types - list, season types are 'Regular Season' or 'Playoffs' possible kwargs: close_def_dists - list, options are: '', '0-2 Feet - Very Tight','2-4 Feet - Tight','4-6 Feet - Open','6+ Feet - Wide Open' shot_clocks - list, options are: '', '24-22', '22-18 Very Early', '18-15 Early', '15-7 Average', '7-4 Late', '4-0 Very Late', 'ShotClock Off' shot_dists - list, options are: '', '>=10.0' touch_times - list, options are: '', 'Touch < 2 Seconds', 'Touch 2-6 Seconds', 'Touch 6+ Seconds' dribble_ranges - list, options are: '', '0 Dribbles', '1 Dribble', '2 Dribbles', '3-6 Dribbles', '7+ Dribbles' general_ranges - list, options are: 'Overall', 'Catch and Shoot', 'Pullups', 'Less Than 10 ft' date_from - string, format - MM/DD/YYYY date_to - string, format - MM/DD/YYYY periods - list of ints location - string, 'Home' or 'Road' returns list of dicts """ all_season_stats = [] for season in seasons: for season_type in season_types: season_stats = [] for close_def in kwargs.get('close_def_dists', ['']): for clock in kwargs.get('shot_clocks', ['']): for dist in kwargs.get('shot_dists', ['']): for touch in kwargs.get('touch_times', ['']): for dribbles in kwargs.get('dribble_ranges', ['']): for general in kwargs.get( 'general_ranges', ['Overall']): for period in kwargs.get('periods', ['']): time.sleep(2) response_json = get_tracking_shots_response( entity_type, season, season_type, close_def_dist=close_def, shot_clock=clock, shot_dist=dist, touch_time=touch, dribbles=dribbles, general_range=general, date_from=kwargs.get( 'date_from', ''), date_to=kwargs.get('date_to', ''), period=period, location=kwargs.get( 'location', ''), ) filter_stats = utils.make_array_of_dicts_from_response_json( response_json, 0) season_stats.append(filter_stats) stats = sum_tracking_shot_totals(entity_type, *season_stats) entity_id_key = 'PLAYER_ID' if entity_type == 'player' else 'TEAM_ID' overall_response_json = get_tracking_shots_response( entity_type, season, season_type, general_range='Overall', date_from=kwargs.get('date_from', ''), date_to=kwargs.get('date_to', '')) overall_stats = utils.make_array_of_dicts_from_response_json( overall_response_json, 0) overall_stats_by_entity = { stat[entity_id_key]: { 'FGA': stat['FGA'], 'FG2A': stat['FG2A'], 'FG3A': stat['FG3A'] } for stat in overall_stats } for stat in stats: entity_id = stat[entity_id_key] stat['SEASON'] = f'{season} {season_type}' stat['OVERALL_FGA'] = overall_stats_by_entity[entity_id]['FGA'] stat['OVERALL_FG2A'] = overall_stats_by_entity[entity_id][ 'FG2A'] stat['OVERALL_FG3A'] = overall_stats_by_entity[entity_id][ 'FG3A'] stat['FGA_FREQUENCY'] = stat['FGA'] / stat[ 'OVERALL_FGA'] if stat['OVERALL_FGA'] != 0 else 0 stat['FG2A_FREQUENCY'] = stat['FG2A'] / stat[ 'OVERALL_FGA'] if stat['OVERALL_FGA'] != 0 else 0 stat['FG3A_FREQUENCY'] = stat['FG3A'] / stat[ 'OVERALL_FGA'] if stat['OVERALL_FGA'] != 0 else 0 stat['FREQUENCY_OF_FG2A'] = stat['FG2A'] / stat[ 'OVERALL_FG2A'] if stat['OVERALL_FG2A'] != 0 else 0 stat['FREQUENCY_OF_FG3A'] = stat['FG3A'] / stat[ 'OVERALL_FG3A'] if stat['OVERALL_FG3A'] != 0 else 0 all_season_stats += stats return all_season_stats