Skip to content

Instantly share code, notes, and snippets.

@sspboyd
Last active April 30, 2021 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspboyd/68ec8f5c5cd15ee7467d4326e3b74111 to your computer and use it in GitHub Desktop.
Save sspboyd/68ec8f5c5cd15ee7467d4326e3b74111 to your computer and use it in GitHub Desktop.
Python + NHL API to gather stats on every skater since 1917
import json
import time
import random
import requests
from requests.exceptions import HTTPError
api_request_limit = 100
# total_num_players = 7274 # skaters
total_num_players = 201 # only using 201 for now so that we aren't hammering the api while testing
start_index = 0
base_api_url_skaters = "https://api.nhle.com/stats/rest/en/skater/bios?isAggregate=true&isGame=false&sort=[{%22property%22:%22playerId%22,%22direction%22:%22ASC%22}]&limit=100&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameTypeId=2%20and%20seasonId%3C=20202021%20and%20seasonId%3E=19171918&start="
player_data = []
curr_data = {}
for curr_start_index in range(start_index, total_num_players, api_request_limit):
api_url = base_api_url_skaters + str(curr_start_index)
try:
response = requests.get(api_url)
# if successful, no exceptions
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
else:
# print('Success!')
curr_data = response.json()
player_data = [*player_data, *curr_data['data']]
set_delay = (random.random() * 3) + 1
time.sleep(set_delay)
print(f'Counter: {curr_start_index}. Delay: {set_delay}. Record Count: {len(player_data)}.')
with open('nhl_skaters_bios_1917-2021.json', 'w') as f:
json.dump(player_data,f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment