-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_all_seasons.py
150 lines (120 loc) · 4.93 KB
/
scrape_all_seasons.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
import json, time, os
import scrape, info, tools, grade
player_stats_path = "data/seasons/{}/players/stats.json"
player_grades_path = "data/seasons/{}/players/grades.json"
teams_path = "data/seasons/{}/teams/{}.json"
team_grades_path = "data/seasons/{}/teams/grades.json"
info_path = "data/seasons/{}/info.json"
standings_path = "data/seasons/{}/league/standings.json"
league_path = "data/seasons/{}/league/league.json"
def get_team_list(data):
teams = []
for player in data:
team = data[player]["Team"]
if isinstance(team, list):
for t in team:
teams.append(t)
else:
teams.append(team)
return set(teams)
def get_standings(year):
pass
def get_teams(teams, year):
pass
def grade_season(season):
print(f"Scraping {season} season")
os.makedirs(f"data/seasons/{str(season)}", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/players", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/teams", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/league", exist_ok=True)
print("Scraping player stats")
player_data = scrape.scrape_stats(season)
tools.dump(player_stats_path.format(season), player_data)
print("Scraping team stats")
teams = get_team_list(player_data)
for team in teams:
team_data = scrape.scrape_team(team, season)
print(season, ":", team)
tools.dump(teams_path.format(season, team), team_data, wait=True)
print("Scraping standings")
standings_data = scrape.scrape_standings(season)
tools.dump(standings_path.format(season), standings_data)
season_info = {
"awards": scrape.scrape_champion_mvp(season),
"all_nba": scrape.scrape_all_nba_teams(season),
}
tools.dump(info_path.format(season), season_info)
# update internal info
data = {}
for team in standings_data:
data[team] = {}
data[team]["standings"] = standings_data[team]
data[team]["img"] = standings_data[team]["img"]
del data[team]["standings"]["img"]
del data[team]["standings"]["link"]
data[team]["roster"] = []
for team in teams:
f = open(teams_path.format(season, team), 'r')
t = json.load(f)
f.close()
data[team]["stats"] = t[team]["team_stats"]
data[team]["info"] = t[team]["info"]
data[team]["last_update"] = t[team]["last_update"]
for player in player_data:
if isinstance(player_data[player]["Team"], list):
team = player_data[player]["Team"][-1]
else:
team = player_data[player]["Team"]
data[team]["roster"].append(player)
tools.dump(league_path.format(season), data)
# grade players
# grade teams
if __name__ == "__main__":
for season in info.seasons:
print(f"Scraping {season} season")
os.makedirs(f"data/seasons/{str(season)}", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/players", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/teams", exist_ok=True)
os.makedirs(f"data/seasons/{str(season)}/league", exist_ok=True)
print("Scraping player stats")
player_data = scrape.scrape_stats(season)
tools.dump(player_stats_path.format(season), player_data)
print("Scraping team stats")
teams = get_team_list(player_data)
for team in teams:
team_data = scrape.scrape_team(team, season)
print(season, ":", team)
tools.dump(teams_path.format(season, team), team_data, wait=True)
print("Scraping standings")
standings_data = scrape.scrape_standings(season)
tools.dump(standings_path.format(season), standings_data)
season_info = {
"awards": scrape.scrape_champion_mvp(season),
"all_nba": scrape.scrape_all_nba_teams(season),
}
tools.dump(info_path.format(season), season_info)
# update internal info
data = {}
for team in standings_data:
data[team] = {}
data[team]["standings"] = standings_data[team]
data[team]["img"] = standings_data[team]["img"]
del data[team]["standings"]["img"]
del data[team]["standings"]["link"]
data[team]["roster"] = []
for team in teams:
f = open(teams_path.format(season, team), 'r')
t = json.load(f)
f.close()
data[team]["stats"] = t[team]["team_stats"]
data[team]["info"] = t[team]["info"]
data[team]["last_update"] = t[team]["last_update"]
for player in player_data:
if isinstance(player_data[player]["Team"], list):
team = player_data[player]["Team"][-1]
else:
team = player_data[player]["Team"]
data[team]["roster"].append(player)
tools.dump(league_path.format(season), data)
# grade players
# grade teams