Last active
May 2, 2022 15:41
-
-
Save nicklozon/32420c6f28d5562d3a0404709616545b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import requests | |
import unicodecsv | |
from datetime import date | |
dt = date.today().isoformat() | |
########### | |
# Skaters # | |
########### | |
filename = "/srv/http/maximumstandard.com/playoffstats/playoff_stats_%s.csv" % dt | |
f = open(filename, 'wb') | |
writer = unicodecsv.writer(f) | |
keys = ['skaterFullName', | |
'teamAbbrevs', | |
'positionCode', | |
'gamesPlayed', | |
'goals', | |
'assists', | |
'points'] | |
start = 0 | |
limit = 10 | |
params = { | |
'isAggregate': False, | |
'isGame': False, | |
'factCayenneExp': 'gamesPlayed>=1', | |
'cayenneExp': 'gameTypeId=3 and seasonId<=20212022 and seasonId>=20212022', | |
'sort': '[{"property":"points","direction":"DESC"},{"property":"goals","direction":"DESC"},{"property":"assists","direction":"DESC"},{"property":"skaterFullName","direction":"ASC"}]', | |
'start': start, | |
'limit': limit | |
} | |
# Write headers | |
result = requests.get('https://api.nhle.com/stats/rest/en/skater/summary', | |
params=params).json() | |
data = result['data'] | |
values = [[rec[key] for key in keys] for rec in data] | |
writer.writerow(keys) | |
total = result['total'] | |
print total | |
# Write rows | |
while start < total: | |
data = requests.get('https://api.nhle.com/stats/rest/en/skater/summary', | |
params=params).json()['data'] | |
values = [[rec[key] for key in keys] for rec in data] | |
writer.writerows(values) | |
start += limit | |
params['start'] = start | |
f.close() | |
########### | |
# Goalies # | |
########### | |
filename = "/srv/http/maximumstandard.com/playoffstats/playoff_goaliestats_%s.csv" % dt | |
f = open(filename, 'wb') | |
writer = unicodecsv.writer(f) | |
keys = ['goalieFullName', | |
'teamAbbrevs', | |
'gamesPlayed', # In lieu of position in order to not throw of Dallas' spreadsheet | |
'gamesPlayed', | |
'wins', | |
'losses', | |
'otLosses', | |
'saves', | |
'goalsAgainst'] | |
start = 0 | |
limit = 10 | |
params = { | |
'isAggregate': False, | |
'isGame': False, | |
'factCayenneExp': 'gamesPlayed>=1', | |
'cayenneExp': 'gameTypeId=3 and seasonId<=20212022 and seasonId>=20212022', | |
'sort': '[{"property":"wins","direction":"DESC"},{"property":"savePct","direction":"DESC"}]', | |
'start': start, | |
'limit': limit | |
} | |
# Write headers | |
result = requests.get('https://api.nhle.com/stats/rest/en/goalie/summary', | |
params=params).json() | |
data = result['data'] | |
values = [[rec[key] for key in keys] for rec in data] | |
writer.writerow(keys) | |
total = result['total'] | |
print total | |
# Write rows | |
while start < total: | |
data = requests.get('https://api.nhle.com/stats/rest/en/goalie/summary', | |
params=params).json()['data'] | |
values = [[rec[key] for key in keys] for rec in data] | |
writer.writerows(values) | |
start += limit | |
params['start'] = start | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment