nesticot commited on
Commit
1d21300
·
verified ·
1 Parent(s): 490f010

Update api_scraper.py

Browse files
Files changed (1) hide show
  1. api_scraper.py +10 -7
api_scraper.py CHANGED
@@ -5,6 +5,7 @@ from datetime import datetime
5
  from tqdm import tqdm
6
  from pytz import timezone
7
  import re
 
8
 
9
 
10
  class MLB_Scrape:
@@ -140,7 +141,7 @@ class MLB_Scrape:
140
 
141
  def get_data(self, game_list_input: list):
142
  """
143
- Retrieves live game data for a list of game IDs.
144
 
145
  Parameters:
146
  - game_list_input (list): A list of game IDs for which to retrieve live data.
@@ -151,12 +152,14 @@ class MLB_Scrape:
151
  data_total = []
152
  print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
153
 
154
- # Iterate over the list of game IDs with a progress bar
155
- for i in tqdm(range(len(game_list_input)), desc="Processing", unit="iteration"):
156
- # Make a GET request to the MLB API for each game ID
157
- r = requests.get(f'https://statsapi.mlb.com/api/v1.1/game/{game_list_input[i]}/feed/live')
158
- # Append the JSON response to the data_total list
159
- data_total.append(r.json())
 
 
160
 
161
  return data_total
162
 
 
5
  from tqdm import tqdm
6
  from pytz import timezone
7
  import re
8
+ from concurrent.futures import ThreadPoolExecutor, as_completed
9
 
10
 
11
  class MLB_Scrape:
 
141
 
142
  def get_data(self, game_list_input: list):
143
  """
144
+ Retrieves live game data for a list of game IDs in parallel.
145
 
146
  Parameters:
147
  - game_list_input (list): A list of game IDs for which to retrieve live data.
 
152
  data_total = []
153
  print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
154
 
155
+ def fetch_data(game_id):
156
+ r = requests.get(f'https://statsapi.mlb.com/api/v1.1/game/{game_id}/feed/live')
157
+ return r.json()
158
+
159
+ with ThreadPoolExecutor() as executor:
160
+ futures = {executor.submit(fetch_data, game_id): game_id for game_id in game_list_input}
161
+ for future in tqdm(as_completed(futures), total=len(futures), desc="Processing", unit="iteration"):
162
+ data_total.append(future.result())
163
 
164
  return data_total
165