Example #1
0
def generate_boxscore_game_logs(measure_type, date_from, date_to):
    """
    date_from - string, format - MM/DD/YYYY
    date_to - string, format - MM/DD/YYYY

    returns list of dicts
    """
    start_date = datetime.strptime(date_from, '%m/%d/%Y')
    end_date = datetime.strptime(date_to, '%m/%d/%Y')
    game_logs = []
    for dt in rrule(DAILY, dtstart=start_date, until=end_date):
        date = dt.strftime('%m/%d/%Y')
        team_id_game_id_map, team_id_opponent_team_id_map = utils.get_team_id_maps_for_date(
            date)
        if len(team_id_game_id_map.values()) == 0:
            return game_logs

        date_game_id = list(team_id_game_id_map.values())[0]

        season = utils.get_season_from_game_id(date_game_id)
        season_type = utils.get_season_type_from_game_id(date_game_id)

        boxscore_game_logs = get_boxscore_stats(measure_type, [season],
                                                [season_type],
                                                date_from=date,
                                                date_to=date)

        player_id_team_id_map = utils.get_player_team_map_for_date(date)
        for game_log in boxscore_game_logs:
            game_log['TEAM_ID'] = player_id_team_id_map[game_log['PLAYER_ID']]
        game_logs += boxscore_game_logs
    return game_logs
Example #2
0
def test_get_player_team_map_for_date():
    with open('tests/data/scoreboard_response.json') as f:
        scoreboard_response = json.loads(f.read())
    scoreboard_response['resultSets'][0]['rowSet'] = [['2020-02-02T00:00:00', 4, '0021900740', 3, 'Final', '20200202/CHITOR', 1610612761, 1610612741, '2019', 4, '     ', None, 'TSN1/4', 'NBCSCH', 'Q4       - ', 'Scotiabank Arena', 1]]
    scoreboard_response_url = 'https://stats.nba.com/stats/scoreboardV2?DayOffset=0&LeagueID=00&gameDate=02/02/2020'

    responses.add(responses.GET, scoreboard_response_url, json=scoreboard_response, status=200)

    with open('tests/data/game_boxscore_response.json') as f:
        game_response_json = json.loads(f.read())

    game_id = '0021900740'

    base_url = 'https://stats.nba.com/stats/boxscoretraditionalv2'
    query_params = {
        'GameId': game_id,
        'StartPeriod': 0,
        'EndPeriod': 10,
        'RangeType': 2,
        'StartRange': 0,
        'EndRange': 55800
    }
    url = furl(base_url).add(query_params).url

    responses.add(responses.GET, url, json=game_response_json, status=200)

    date = '02/02/2020'
    player_team_map = utils.get_player_team_map_for_date(date)
    assert player_team_map == {
        1628990: 1610612741,
        201152: 1610612741,
        1628436: 1610612741,
        203897: 1610612741,
        203107: 1610612741,
        1629655: 1610612741,
        1629632: 1610612741,
        1627853: 1610612741,
        1627756: 1610612741,
        1626245: 1610612741,
        1629690: 1610612741,
        1627885: 1610612741,
        1628384: 1610612761,
        1627783: 1610612761,
        201586: 1610612761,
        200768: 1610612761,
        1627832: 1610612761,
        1627775: 1610612761,
        1628449: 1610612761,
        1629056: 1610612761,
        1629744: 1610612761,
        1626169: 1610612761,
        1629052: 1610612761,
        1628778: 1610612761,
        1626259: 1610612761
    }
def generate_tracking_shot_game_logs(entity_type, date_from, date_to,
                                     **kwargs):
    """
    entity_type - string, player, team, opponent
    date_from - string, format - MM/DD/YYYY
    date_to - string, format - MM/DD/YYYY

    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'
    periods - list of ints
    location - string, 'Home' or 'Road'

    returns list of dicts
    """
    start_date = datetime.strptime(date_from, '%m/%d/%Y')
    end_date = datetime.strptime(date_to, '%m/%d/%Y')
    game_logs = []
    for dt in rrule(DAILY, dtstart=start_date, until=end_date):
        date = dt.strftime('%m/%d/%Y')
        team_id_game_id_map, team_id_opponent_team_id_map = utils.get_team_id_maps_for_date(
            date)
        if len(team_id_game_id_map.values()) == 0:
            return game_logs

        date_game_id = list(team_id_game_id_map.values())[0]

        season = utils.get_season_from_game_id(date_game_id)
        season_type = utils.get_season_type_from_game_id(date_game_id)

        tracking_shots_data = get_tracking_shot_stats(entity_type, [season],
                                                      [season_type],
                                                      date_from=date,
                                                      date_to=date,
                                                      **kwargs)
        tracking_shots_game_logs = sum_tracking_shot_totals(
            entity_type, tracking_shots_data)
        if entity_type == 'player':
            # need to add team id for player because results only have PLAYER_LAST_TEAM_ID, which may not be the team for which they played the game
            player_id_team_id_map = utils.get_player_team_map_for_date(date)
            for game_log in tracking_shots_game_logs:
                game_log['TEAM_ID'] = player_id_team_id_map[
                    game_log['PLAYER_ID']]
        for game_log in tracking_shots_game_logs:
            game_log['GAME_ID'] = team_id_game_id_map[game_log['TEAM_ID']]
            game_log['OPPONENT_TEAM_ID'] = team_id_opponent_team_id_map[
                game_log['TEAM_ID']]
        game_logs += tracking_shots_game_logs
    return game_logs
Example #4
0
def generate_tracking_game_logs(stat_measure, entity_type, date_from, date_to):
    """
    stat_measure - string, options: 'Drives', 'Defense', 'CatchShoot', 'Passing', 'Possessions', 'PullUpShot', 'Rebounding', 'Efficiency', 'SpeedDistance', 'ElbowTouch', 'PostTouch', 'PaintTouch'
    entity_type - string, player, team
    date_from - string, format - MM/DD/YYYY
    date_to - string, format - MM/DD/YYYY

    returns list of dicts
    """
    start_date = datetime.strptime(date_from, '%m/%d/%Y')
    end_date = datetime.strptime(date_to, '%m/%d/%Y')
    game_logs = []
    for dt in rrule(DAILY, dtstart=start_date, until=end_date):
        date = dt.strftime('%m/%d/%Y')
        team_id_game_id_map, team_id_opponent_team_id_map = utils.get_team_id_maps_for_date(
            date)
        if len(team_id_game_id_map.values()) == 0:
            return game_logs

        date_game_id = list(team_id_game_id_map.values())[0]

        season = utils.get_season_from_game_id(date_game_id)
        season_type = utils.get_season_type_from_game_id(date_game_id)

        tracking_game_logs = get_tracking_stats(stat_measure, [season],
                                                [season_type],
                                                entity_type,
                                                date_from=date,
                                                date_to=date)
        if entity_type == 'player':
            # need to add team id for player because results only have last team id, which may not be the team for which they played the game
            player_id_team_id_map = utils.get_player_team_map_for_date(date)
            for game_log in tracking_game_logs:
                game_log['TEAM_ID'] = player_id_team_id_map[
                    game_log['PLAYER_ID']]
        for game_log in tracking_game_logs:
            game_log['GAME_ID'] = team_id_game_id_map[game_log['TEAM_ID']]
            game_log['OPPONENT_TEAM_ID'] = team_id_opponent_team_id_map[
                game_log['TEAM_ID']]
        game_logs += tracking_game_logs
    return game_logs