nesticot commited on
Commit
5b5aa93
·
verified ·
1 Parent(s): 6f6f5ff

Update api_scraper.py

Browse files
Files changed (1) hide show
  1. api_scraper.py +33 -9
api_scraper.py CHANGED
@@ -154,9 +154,34 @@ class MLB_Scrape:
154
  return game_df
155
 
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  def get_data(self, game_list_input: list):
158
  """
159
- Retrieves live game data for a list of game IDs in parallel.
160
 
161
  Parameters:
162
  - game_list_input (list): A list of game IDs for which to retrieve live data.
@@ -167,17 +192,16 @@ class MLB_Scrape:
167
  data_total = []
168
  print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
169
 
170
- def fetch_data(game_id):
171
- r = requests.get(f'https://statsapi.mlb.com/api/v1.1/game/{game_id}/feed/live')
172
- return r.json()
173
-
174
- with ThreadPoolExecutor() as executor:
175
- futures = {executor.submit(fetch_data, game_id): game_id for game_id in game_list_input}
176
- for future in tqdm(as_completed(futures), total=len(futures), desc="Processing", unit="iteration"):
177
- data_total.append(future.result())
178
 
179
  return data_total
180
 
 
181
  def get_data_df(self, data_list):
182
  """
183
  Converts a list of game data JSON objects into a Polars DataFrame.
 
154
  return game_df
155
 
156
 
157
+ # def get_data(self, game_list_input: list):
158
+ # """
159
+ # Retrieves live game data for a list of game IDs in parallel.
160
+
161
+ # Parameters:
162
+ # - game_list_input (list): A list of game IDs for which to retrieve live data.
163
+
164
+ # Returns:
165
+ # - data_total (list): A list of JSON responses containing live game data for each game ID.
166
+ # """
167
+ # data_total = []
168
+ # print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
169
+
170
+ # def fetch_data(game_id):
171
+ # r = requests.get(f'https://statsapi.mlb.com/api/v1.1/game/{game_id}/feed/live')
172
+ # return r.json()
173
+
174
+ # with ThreadPoolExecutor() as executor:
175
+ # futures = {executor.submit(fetch_data, game_id): game_id for game_id in game_list_input}
176
+ # for future in tqdm(as_completed(futures), total=len(futures), desc="Processing", unit="iteration"):
177
+ # data_total.append(future.result())
178
+
179
+ # return data_total
180
+
181
+
182
  def get_data(self, game_list_input: list):
183
  """
184
+ Retrieves live game data for a list of game IDs.
185
 
186
  Parameters:
187
  - game_list_input (list): A list of game IDs for which to retrieve live data.
 
192
  data_total = []
193
  print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
194
 
195
+ # Iterate over the list of game IDs with a progress bar
196
+ for i in tqdm(range(len(game_list_input)), desc="Processing", unit="iteration"):
197
+ # Make a GET request to the MLB API for each game ID
198
+ r = requests.get(f'https://statsapi.mlb.com/api/v1.1/game/{game_list_input[i]}/feed/live')
199
+ # Append the JSON response to the data_total list
200
+ data_total.append(r.json())
 
 
201
 
202
  return data_total
203
 
204
+
205
  def get_data_df(self, data_list):
206
  """
207
  Converts a list of game data JSON objects into a Polars DataFrame.