-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
86 lines (66 loc) · 2.69 KB
/
utils.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
# --------------------------------------------------------------------------- #
# D. Rodriguez, 2021-04-06, File created.
# --------------------------------------------------------------------------- #
import requests
import json
HEADERS = {
'Host': 'stats.nba.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) '
'Gecko/20100101 Firefox/72.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Connection': 'keep-alive',
'Referer': 'https://stats.nba.com/',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
}
# def get_http_response(request_url, headers, parameters):
# errors = []
#
# try:
# response = requests.get(request_url, headers=headers, params=parameters)
# response_data = response.json()
# return response
# except:
# errors.append('Unable to get URL.')
# return {'error': errors}
# TODO (D. Rodriguez 2021-04-09): Implement background task (Redis?)
# queue to avoid timeout error.
def get_player_common_info(player_id):
"""Get player details"""
print(f'\nStarting get_player_common_info() task for player id: {player_id}')
parameters = {
'PlayerID': player_id
}
endpoint = 'commonplayerinfo'
request_url = f'https://stats.nba.com/stats/{endpoint}?'
response = requests.get(request_url, headers=HEADERS, params=parameters)
# response = get_http_response(request_url, HEADERS, parameters)
player_common_info = json.loads(response.content.decode())['resultSets'][0]
player_headline_stats = json.loads(response.content.decode())['resultSets'][1]
print('\n')
print(player_common_info)
print('\n')
print(player_headline_stats)
print('\n')
return player_common_info, player_headline_stats
def get_player_seasons(player_id):
"""Get player season's played per player ID"""
# print(f'\nStarting get_player_seasons() task for player id: {player_id}')
parameters = {
'LeagueID': '00',
'PerMode': 'PerGame',
'PlayerID': player_id
}
endpoint = 'playerprofilev2'
request_url = f'https://stats.nba.com/stats/{endpoint}?'
response = requests.get(request_url, headers=HEADERS, params=parameters)
# response = get_http_response(request_url, HEADERS, parameters)
player_available_season_stats = json.loads(response.content.decode())['resultSets'][0]
# print('\n')
# print(player_available_season_stats)
# print('\n')
return player_available_season_stats