コード例 #1
0
def find_summary(game_id):
    # 0041400161
    summary = 'http://stats.nba.com/stats/boxscoresummaryv2?GameID=' \
              + str(game_id)

    summary_resp = requests.get(summary)
    if summary_resp.status_code >= 400:
        print('no such game ' + str(summary_resp.status_code))
        return False

    try:
        home_team_id = summary_resp.json()['resultSets'][0]['rowSet'][0][6]
        line_score = summary_resp.json()['resultSets'][5]
        line_score['headers'].append('IS_HOME')
        is_home = (line_score['rowSet'][0][3] == home_team_id)

        line_score['rowSet'][0].append(is_home)
        line_score['rowSet'][1].append(not is_home)

        # CsvHelper.list2d_to_csv(
        #     dict_to_list2d(game_summary), 'summary_' + str(game_id)
        # )
        CsvHelper.list2d_to_csv(
            dict_to_list2d(line_score), PATH + 'line_score_' + str(game_id)
        )
    except (KeyError, IndexError, TypeError, ValueError):
        print(summary)

    return True
コード例 #2
0
ファイル: Live.py プロジェクト: 570468837/Daily-pracitce
def find_live(game_id, period):
    # http://china.nba.com/wap/static/data/game/playbyplay_0041400311_4.json
    aim = 'http://china.nba.com/wap/static/data/game/playbyplay_' \
          + str(game_id) + \
          '_' \
          + str(period) + \
          '.json'

    game = requests.get(aim)
    game.encoding = 'utf-8'
    if game.status_code >= 400:
        print('no suck game ' + str(game.status_code))
        return False
    try:
        events = game.json()['payload']['playByPlays'][0]['events']
        file_id = PATH + 'live' + str(game_id) + '_' + str(period)

        # CsvHelper.dict_to_csv_stream(events[0])
        # for x in range(1, len(events)):
        #     CsvHelper.dict_to_csv_stream(
        #         events[x], False
        #     )
        CsvHelper.dict_to_csv(events[0], file_id)
        for x in range(1, len(events)):
            CsvHelper.dict_to_csv(
                events[x], file_id, False, mode='a'
            )

    except (ValueError, IndexError):
        print(aim)

    return True
コード例 #3
0
ファイル: Live.py プロジェクト: prshnt/Daily-pracitce
def find_live(game_id, period):
    # http://china.nba.com/wap/static/data/game/playbyplay_0041400311_4.json
    aim = 'http://china.nba.com/wap/static/data/game/playbyplay_' \
          + str(game_id) + \
          '_' \
          + str(period) + \
          '.json'

    game = requests.get(aim)
    game.encoding = 'utf-8'
    if game.status_code >= 400:
        print('no suck game ' + str(game.status_code))
        return False
    try:
        events = game.json()['payload']['playByPlays'][0]['events']
        file_id = PATH + 'live' + str(game_id) + '_' + str(period)

        # CsvHelper.dict_to_csv_stream(events[0])
        # for x in range(1, len(events)):
        #     CsvHelper.dict_to_csv_stream(
        #         events[x], False
        #     )
        CsvHelper.dict_to_csv(events[0], file_id)
        for x in range(1, len(events)):
            CsvHelper.dict_to_csv(events[x], file_id, False, mode='a')

    except (ValueError, IndexError):
        print(aim)

    return True
コード例 #4
0
def player_profile(player_id, season=SeasonType.regular):
    url = 'http://stats.nba.com/stats/commonplayerinfo?LeagueID=00&PlayerID=' \
          + str(player_id) + \
          '&SeasonType=' \
          + season.value

    response = requests.get(url)
    if not valid_response(response):
        return

    stats = response.json()['resultSets'][0]
    CsvHelper.list2d_to_csv(dict_to_list2d(stats),
                            'player_profile_' + str(player_id))
コード例 #5
0
def player_profile(player_id, season=SeasonType.regular):
    url = 'http://stats.nba.com/stats/commonplayerinfo?LeagueID=00&PlayerID=' \
          + str(player_id) + \
          '&SeasonType=' \
          + season.value

    response = requests.get(url)
    if not valid_response(response):
        return

    stats = response.json()['resultSets'][0]
    CsvHelper.list2d_to_csv(
        dict_to_list2d(stats), 'player_profile_' + str(player_id)
    )
コード例 #6
0
def find_detail(season, game_id, detail_type):
    # http://stats.nba.com/stats/boxscoreadvancedv2?EndPeriod=10&EndRange=28800&GameID=
    # 0020000654
    # &RangeType=2&Season=
    # 2000-01
    # &SeasonType=Regular+Season&StartPeriod=1&StartRange=0
    # '2014-15', 0021400160
    detail = 'http://stats.nba.com/stats/boxscore' \
             + str(detail_type.name) + \
             'v2?EndPeriod=10&EndRange=28800&' \
             'GameID=' \
             + str(game_id) + \
             '&RangeType=2&Season=' \
             + str(season) + \
             '&SeasonType=Regular+Season&StartPeriod=1&StartRange=0'

    detail_resp = requests.get(detail)
    if detail_resp.status_code >= 400:
        print('no such game ' + str(detail_resp.status_code))
        return False

    try:
        player_stats = detail_resp.json()['resultSets'][0]
        team_stats = detail_resp.json()['resultSets'][1]

        from hw_scrape import CsvHelper

        CsvHelper.list2d_to_csv(
            dict_to_list2d(player_stats), PATH + 'player_' + str(game_id) + UNDER + str(detail_type.name)
        )
        CsvHelper.list2d_to_csv(
            dict_to_list2d(team_stats), PATH + 'team_' + str(game_id) + UNDER + str(detail_type.name)
        )

    except (KeyError, IndexError, TypeError):
        print(detail)

    return True