-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget-gameIds.py
70 lines (57 loc) · 1.33 KB
/
get-gameIds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sys
import urllib2
import json
from pprint import pprint
from operator import itemgetter
#
#
# Get user argument and create the json location
#
#
dateArg = sys.argv[1]
if len(dateArg) != 8:
print "Enter a date in yyyymmdd format"
sys.exit()
yearArg = dateArg[0:4]
monthArg = dateArg[4:6]
dayArg = dateArg[6:]
if int(yearArg) > 2020 or int(yearArg) < 2010 or int(monthArg) < 1 or int(monthArg) > 12 or int(dayArg) < 1 or int(dayArg) > 31:
print "Enter a date in yyyymmdd format"
sys.exit()
requestStr = yearArg + "-" + monthArg + "-" + dayArg
jsonLoc = "https://statsapi.web.nhl.com/api/v1/schedule?startDate=" + requestStr + "&endDate=" + requestStr
print jsonLoc
#
#
# Get json
#
#
response = urllib2.urlopen(jsonLoc)
html = response.read()
jsonDict = json.loads(html)
dates = jsonDict["dates"]
#
#
# Get gameIds from json
#
#
outGames = []
for date in dates:
for game in date["games"]:
gameDict = dict()
gameDict["season"] = game["season"]
gameDict["gameId"] = str(game["gamePk"])[5:]
gameDict["state"] = game["status"]["detailedState"]
outGames.append(gameDict)
outGames = sorted(outGames, key=itemgetter("gameId"))
#
#
# Print gameIds
#
#
print " "
print requestStr
print "SEASON GAMEID STATE"
for game in outGames:
print str(game["season"]) + " " + str(game["gameId"]) + " " + game["state"]
print " "