forked from lijin-THU/notes-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Player.py
453 lines (449 loc) · 25 KB
/
_Player.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import requests as _requests
from goldsberry._apiFunc import *
my_headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)'\
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 '\
'Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9'\
',image/webp,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
}
class demographics:
def __init__(self, playerid):
self._url = 'http://stats.nba.com/stats/commonplayerinfo?'
self._api_param = {'PlayerID':playerid}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def player_info(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def headline_stats(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class career_stats:
def __init__(self, playerid, league='NBA',permode=1):
self._url = "http://stats.nba.com/stats/playerprofilev2?"
self._api_param = {'PlayerID':playerid,
'LeagueID':_nbaLeague(league),
'PerMode':_PerModeSmall36(permode)}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def season_totals_regular(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def career_totals_regular(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_totals_post(self):
_headers = self._pull.json()['resultSets'][2]['headers']
_values = self._pull.json()['resultSets'][2]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def career_totals_post(self):
_headers = self._pull.json()['resultSets'][3]['headers']
_values = self._pull.json()['resultSets'][3]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_totals_allstar(self):
_headers = self._pull.json()['resultSets'][4]['headers']
_values = self._pull.json()['resultSets'][4]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def career_totals_allstar(self):
_headers = self._pull.json()['resultSets'][5]['headers']
_values = self._pull.json()['resultSets'][5]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_totals_college(self):
_headers = self._pull.json()['resultSets'][6]['headers']
_values = self._pull.json()['resultSets'][6]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def career_totals_college(self):
_headers = self._pull.json()['resultSets'][7]['headers']
_values = self._pull.json()['resultSets'][7]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_rankings_regular(self):
_headers = self._pull.json()['resultSets'][8]['headers']
_values = self._pull.json()['resultSets'][8]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_rankings_post(self):
_headers = self._pull.json()['resultSets'][9]['headers']
_values = self._pull.json()['resultSets'][9]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def season_high(self):
_headers = self._pull.json()['resultSets'][10]['headers']
_values = self._pull.json()['resultSets'][10]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def career_high(self):
_headers = self._pull.json()['resultSets'][11]['headers']
_values = self._pull.json()['resultSets'][11]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def next_game(self):
_headers = self._pull.json()['resultSets'][12]['headers']
_values = self._pull.json()['resultSets'][12]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class general_splits:
def __init__(self, playerid, season='2015',seasontype=1, league='NBA',
dateto='', datefrom='', gamesegment=1, lastngames=0, location=1, measuretype=1,
month=0, opponentteamid=0, outcome=1, paceadjust=1, permode=1, period=0,
plusminus=1, rank=1, seasonsegment=1, vsconf=1, vsdiv=1):
self._url = "http://stats.nba.com/stats/playerdashboardbygeneralsplits?"
self._api_param = {'PlayerID':playerid,
'SeasonType': _SeasonType(seasontype),
'Season': _nbaSeason(season),
'LeagueID': _nbaLeague(league),
'DateTo':_valiDate(dateto),
'DateFrom':_valiDate(datefrom),
'GameSegment':_GameSegment(gamesegment),
'LastNGames':lastngames,
'Location':_Location(location),
'MeasureType':_measureType(measuretype),
'Month':month,
'OpponentTeamID':opponentteamid,
'Outcome':_Outcome(outcome),
'PaceAdjust':_PaceAdjust(paceadjust),
'PerMode':_PerModeLarge(permode),
'Period':period,
'PlusMinus':_PlusMinus(plusminus),
'Rank':_Rank(rank),
'SeasonSegment':_SeasonSegment(seasonsegment),
'VsConference':_VsConference(vsconf),
'VsDivision':_VsDivision(vsdiv)}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def overall(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def location(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def wins_losses(self):
_headers = self._pull.json()['resultSets'][2]['headers']
_values = self._pull.json()['resultSets'][2]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def month(self):
_headers = self._pull.json()['resultSets'][3]['headers']
_values = self._pull.json()['resultSets'][3]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def pre_post_allstar(self):
_headers = self._pull.json()['resultSets'][4]['headers']
_values = self._pull.json()['resultSets'][4]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def starting_position(self):
_headers = self._pull.json()['resultSets'][5]['headers']
_values = self._pull.json()['resultSets'][5]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def days_rest(self):
_headers = self._pull.json()['resultSets'][6]['headers']
_values = self._pull.json()['resultSets'][6]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class game_logs:
def __init__(self, playerid, season='2015',seasontype=1, league='NBA'):
self._url = "http://stats.nba.com/stats/playergamelog?"
self._api_param = {'PlayerID':playerid,
'SeasonType': _SeasonType(seasontype),
'Season': _nbaSeason(season),
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def logs(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class shot_dashboard:
def __init__(self,playerid,league='NBA',season='2015', seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0, permode=1):
self._url = "http://stats.nba.com/stats/playerdashptshots?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames,
'PerMode' : _PerModeMini(permode)
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def overall(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def general(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def shot_clock(self):
_headers = self._pull.json()['resultSets'][2]['headers']
_values = self._pull.json()['resultSets'][2]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def dribble(self):
_headers = self._pull.json()['resultSets'][3]['headers']
_values = self._pull.json()['resultSets'][3]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def closest_defender(self):
_headers = self._pull.json()['resultSets'][4]['headers']
_values = self._pull.json()['resultSets'][4]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def closest_defender_10ft(self):
_headers = self._pull.json()['resultSets'][5]['headers']
_values = self._pull.json()['resultSets'][5]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def touch_time(self):
_headers = self._pull.json()['resultSets'][6]['headers']
_values = self._pull.json()['resultSets'][6]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class rebound_dashboard:
def __init__(self,playerid,league='NBA',season='2015', seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0,permode=1):
self._url = "http://stats.nba.com/stats/playerdashptreb?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames,
'PerMode' : _PerModeMini(permode)
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def overall(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def shot_type(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def contesting_rebounders(self):
_headers = self._pull.json()['resultSets'][2]['headers']
_values = self._pull.json()['resultSets'][2]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def shot_distance(self):
_headers = self._pull.json()['resultSets'][3]['headers']
_values = self._pull.json()['resultSets'][3]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def rebound_distance(self):
_headers = self._pull.json()['resultSets'][4]['headers']
_values = self._pull.json()['resultSets'][4]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class passing_dashboard:
def __init__(self,playerid,league='NBA',season='2015', seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0,permode=1):
self._url = "http://stats.nba.com/stats/playerdashptpass?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames,
'PerMode' : _PerModeMini(permode)
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def passes_made(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def passes_received(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class defense_dashboard:
def __init__(self,playerid,league='NBA',season='2015', seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0,permode=1):
self._url = "http://stats.nba.com/stats/playerdashptreb?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames,
'PerMode' : _PerModeMini(permode)
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def defending_shot(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class shot_log:
def __init__(self,playerid,league='NBA',season='2015',seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0):
self._url = "http://stats.nba.com/stats/playerdashptshotlog?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def log(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class rebound_log:
def __init__(self,playerid,league='NBA',season='2015',seasontype=1,teamid=0,
outcome=1,location=1,month=0,seasonsegment=1,datefrom='',
dateto='',opponentteamid=0,vsconf=1,vsdiv=1,gamesegment=1,
period=0,lastngames=0):
self._url = "http://stats.nba.com/stats/playerdashptreboundlogs?"
self._api_param = {'PlayerID' : playerid,
'LeagueID': _nbaLeague(league),
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def log(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
class shot_chart:
def __init__(self,playerid,leagueid='',season='2015', seasontype=1,teamid=0,
gameid='',outcome=1,location=1,month=0,seasonsegment=1,
datefrom='',dateto='',opponentteamid=0,vsconf=1,vsdiv=1,
position=1,period=0,lastngames=0,aheadbehind=1,
contextmeasure=1,clutchtime=7,rookieyear='',
contextfilter='',startperiod='1',endperiod='10',startrange='0',
endrange='28800', gamesegment=1, rangetype='2'):
if not rookieyear == '':
rookieyear = _nbaSeason(rookieyear)
self._url = "http://stats.nba.com/stats/shotchartdetail?"
self._api_param = {'LeagueID': leagueid,
'Season' : _nbaSeason(season),
'SeasonType' : _SeasonType(seasontype),
'TeamID' : teamid,
'PlayerID' : playerid,
'GameID' : gameid,
'Outcome' : _Outcome(outcome),
'Location' : _Location(location),
'Month' : month,
'SeasonSegment' : _SeasonSegment(seasonsegment),
'DateFrom' : _valiDate(datefrom),
'DateTo' : _valiDate(dateto),
'OpponentTeamID' : opponentteamid,
'VsConference' : _VsConference(vsconf),
'VsDivision' : _VsDivision(vsdiv),
'Position' : _Position(position),
'GameSegment' : _GameSegment(gamesegment),
'Period' : period,
'LastNGames' : lastngames,
'AheadBehind' : _AheadBehind(aheadbehind),
'ContextMeasure' : _ContextMeasure(contextmeasure),
'ClutchTime' : _ClutchTime(clutchtime),
'RookieYear' : rookieyear,
'ContextFilter':contextfilter,
'StartPeriod':startperiod,
'EndPeriod':endperiod,
'StartRange':startrange,
'EndRange':endrange,
'RangeType':rangetype,
}
self._pull = _requests.get(self._url, params=self._api_param, headers=my_headers)
def chart(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def leagueaverage(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def PlayerList(season='2015', AllTime=False, league='NBA'):
if AllTime:
_url = "http://stats.nba.com/stats/commonallplayers?"
_api_param = {'IsOnlyCurrentSeason':"0",
'LeagueID':_nbaLeague(league),
'Season': "2015-16"}
_pull = _requests.get(_url, params=_api_param, headers=my_headers)
_headers = _pull.json()['resultSets'][0]['headers']
_values = _pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
else:
_url = "http://stats.nba.com/stats/commonallplayers?"
_api_param = {'IsOnlyCurrentSeason':"0",
'LeagueID': _nbaLeague(league),
'Season': _nbaSeason(season)}
_pull = _requests.get(_url, params=_api_param, headers=my_headers)
_headers = _pull.json()['resultSets'][0]['headers']
_values = _pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
__all__ = ["demographics", "career_stats", "general_splits",
"game_logs", "shot_dashboard", "rebound_dashboard",
"passing_dashboard", "defense_dashboard", "shot_log",
"rebound_log", "shot_chart"]