-
Notifications
You must be signed in to change notification settings - Fork 3
/
updateRoster.py
72 lines (58 loc) · 2.07 KB
/
updateRoster.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
import requests, json, time, operator, pickle, random
labels = ['ast','blk','dreb','fg3_pct','fg3a','fg3m','fga','fgm','fta','ftm','oreb','pf','pts','reb','stl', 'turnover', 'min']
season = '2022'
roster = {}
for i in range(0,31):
roster.update({str(i):[]})
def main():
playerIdByTeamID = load_obj('2022PlayerIdByTeamID')
seasonAverages = load_obj('2022SeasonAverages')
#avg = getSeaonAverage('61',season,labels)
#print(avg)
#iterate team in teams
for team in playerIdByTeamID:
#iterate player in teams
for playerid in playerIdByTeamID[team]:
#get current team
teamid = getCurrentTeam(playerid,season)
#add player to roster
roster[str(teamid)].append(playerid)
print(roster)
#save roster
save_obj(roster,'roster')
def getSeaonAverage(playerId,season,labels):
url = 'https://www.balldontlie.io/api/v1/season_averages?season='+season
url+='&player_ids[]='+str(playerId)
r = req(url)
if len(r['data'])==0:
print('no season average-----------------')
return []
r = r['data'][0]
print(r)
seasonAverage = []
for label in labels:
seasonAverage.append(r[label])
return seasonAverage
def getCurrentTeam(playerId,season):
url = 'https://www.balldontlie.io/api/v1/players/'+str(playerId)
r = req(url)
return r['team']['id']
def req(url):
proxy = load_obj('proxy')
dict = {}
p = random.randint(0,len(proxy)-1)
dict.update({'http' : proxy[p]})
r = requests.get(url)
print('proxy: ', proxy[p], 'url: ', url, 'response: ', r)
if str(r) != '<Response [200]>':#means we request too fast..fast af boi so like anything under 1 r/sec cause error at 60 seconds in....
time.sleep(30)
req(url)
time.sleep(2)
return r.json()
def save_obj(obj, name):
with open('updatedObj/'+ name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open('updatedObj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
main()