-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_milb_teams.py
223 lines (182 loc) · 6.22 KB
/
get_milb_teams.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
import json
import time
from datetime import datetime
from urllib.request import urlopen
import pandas as pd
from tqdm import tqdm
def get_milb_team_list(season: int, save=True):
"""
"""
row_df = pd.DataFrame()
teams_df = pd.DataFrame()
now = datetime.now()
if season > (now.year+1):
raise ValueError(f'`season` cannot be greater than {now.year+1}.')
teams_url = f"https://statsapi.mlb.com/api/v1/teams?season={season}"
response = urlopen(teams_url)
time.sleep(2)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
'The MiLB API is actively refusing your connection.' +
'\nHTTP Error Code:\t403'
)
else:
raise ConnectionError(
'Could not establish a connection to the MiLB API.' +
f'\nHTTP Error Code:\t{response.code}'
)
json_data = json.loads(response.read())
for team in tqdm(json_data['teams']):
team_id = team['id']
try:
team_full_name = team['name']
except Exception:
team_full_name = None
team_link = team['link']
try:
team_venue_id = team['venue']['id']
except Exception:
team_venue_id = None
try:
team_venue_name = team['venue']['name']
except Exception:
team_venue_name = None
try:
team_venue_link = team['venue']['link']
except Exception:
team_venue_link = None
try:
team_code = team['teamCode']
except Exception:
team_code = None
try:
file_code = team['fileCode']
except Exception:
file_code = None
try:
team_abbreviation = team['abbreviation']
except Exception:
team_abbreviation = None
try:
team_short_name = team['shortName']
except Exception:
team_short_name = None
try:
team_nickname = team['teamName']
except Exception:
team_nickname = None
try:
team_location = team['locationName']
except Exception:
team_location = None
try:
first_year_of_play = int(team['firstYearOfPlay'])
except Exception:
first_year_of_play = None
try:
league_id = team['league']['id']
except Exception:
league_id = None
try:
league_name = team['league']['name']
except Exception:
league_name = None
try:
league_link = team['league']['link']
except Exception:
league_link = None
try:
division_id = team['division']['id']
except Exception:
division_id = None
try:
division_name = team['division']['name']
except Exception:
division_name = None
try:
division_link = team['division']['link']
except Exception:
division_link = None
try:
sport_id = team['sport']['id']
except Exception:
sport_id = None
try:
sport_name = team['sport']['name']
except Exception:
sport_name = None
try:
sport_link = team['sport']['link']
except Exception:
sport_link = None
# College teams are returned in this API endpoint.
# Those teams do not have a parent org.
try:
parent_org_name = team['parentOrgName']
except Exception:
parent_org_name = None
try:
parent_org_id = team['parentOrgId']
except Exception:
parent_org_id = None
try:
franchise_name = team['franchiseName']
except Exception:
franchise_name = None
try:
club_name = team['clubName']
except Exception:
club_name = None
is_active_team = team['active']
row_df = pd.DataFrame(
{
'team_id': team_id,
'team_full_name': team_full_name,
'team_link': team_link,
'season': season,
'team_venue_id': team_venue_id,
'team_venue_name': team_venue_name,
'team_venue_link': team_venue_link,
'team_code': team_code,
'file_code': file_code,
'team_abbreviation': team_abbreviation,
'team_nickname': team_nickname,
'team_location': team_location,
'first_year_of_play': first_year_of_play,
'league_id': league_id,
'league_name': league_name,
'league_link': league_link,
'division_id': division_id,
'division_name': division_name,
'division_link': division_link,
'sport_id': sport_id,
'sport_name': sport_name,
'sport_link': sport_link,
'team_short_name': team_short_name,
'parent_org_name': parent_org_name,
'parent_org_id': parent_org_id,
'franchise_name': franchise_name,
'club_name': club_name,
'is_active_team': is_active_team
}, index=[0]
)
del team_id, team_full_name, team_link, team_venue_id, \
team_venue_name, team_venue_link, team_code, \
team_abbreviation, team_nickname, team_location, \
first_year_of_play, league_id, league_name, \
league_link, division_id, division_name, division_link, \
sport_id, sport_name, sport_link, team_short_name, \
parent_org_name, parent_org_id, franchise_name, \
club_name, is_active_team
teams_df = pd.concat([teams_df, row_df], ignore_index=True)
if save is True:
teams_df.to_csv(f'teams/{season}_teams.csv', index=False)
return teams_df
if __name__ == "__main__":
now = datetime.now()
# for i in range(now.year, now.year+1):
for i in range(2015, now.year+1):
print(f'Getting a list of teams in the MLB API for the {i} season.')
get_milb_team_list(i)