forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnba_stats_sync.py
More file actions
85 lines (63 loc) · 2.15 KB
/
nba_stats_sync.py
File metadata and controls
85 lines (63 loc) · 2.15 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
Gathering statistics on NBA players with the regular old
synchronous requests library.
It took: 214.62 seconds (3.6 minutes) on my machine at home on May 29th
Borrowed from:
http://terriblecode.com/blog/asynchronous-http-requests-in-python/
"""
import requests
import json
import time
base_url = 'http://stats.nba.com/stats'
HEADERS = {
'user-agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/45.0.2454.101 Safari/537.36'),
}
def get_players(player_args):
"""
get the names of all the players we are interested in
This request will get JSON of the players for the 2016-17 season:
http://stats.nba.com/stats/commonallplayers?LeagueID=00&season=2016-17&isonlycurrentseason=1
"""
endpoint = '/commonallplayers'
params = {'leagueid': '00',
'season': '2016-17',
'isonlycurrentseason': '1'}
url = base_url + endpoint
print('Getting all players...')
resp = requests.get(url,
headers=HEADERS,
params=params)
data = resp.json()
player_args.extend(
[(item[0], item[2]) for item in data['resultSets'][0]['rowSet']])
def get_player(player_id, player_name):
"""
The request for a player's stats.
Should be a request like:
http://stats.nba.com/stats/commonplayerinfo?playerid=203112
"""
endpoint = '/commonplayerinfo'
params = {'playerid': player_id}
url = base_url + endpoint
print("Getting player", player_name, player_id)
resp = requests.get(url,
headers=HEADERS,
params=params)
print(resp)
data = resp.json()
all_players[player_name] = data
all_players = {}
players = []
start = time.time()
get_players(players)
print("there are {} players".format(len(players)))
for id, name in players:
get_player(id, name)
print("Done getting data: it took {:.2F} seconds".format(time.time() - start))
# write it out to a file
with open("NBA_stats.json", 'w') as outfile:
json.dump(all_players, outfile, indent=2)
print("File written out")