Update api_scraper.py
Browse files- api_scraper.py +10 -8
api_scraper.py
CHANGED
|
@@ -141,7 +141,7 @@ 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.
|
| 145 |
|
| 146 |
Parameters:
|
| 147 |
- game_list_input (list): A list of game IDs for which to retrieve live data.
|
|
@@ -152,16 +152,18 @@ class MLB_Scrape:
|
|
| 152 |
data_total = []
|
| 153 |
print('This May Take a While. Progress Bar shows Completion of Data Retrieval.')
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
return data_total
|
| 163 |
|
| 164 |
-
|
| 165 |
def get_data_df(self, data_list):
|
| 166 |
"""
|
| 167 |
Converts a list of game data JSON objects into a Polars DataFrame.
|
|
|
|
| 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 |
+
time.sleep(1)
|
| 164 |
|
| 165 |
return data_total
|
| 166 |
|
|
|
|
| 167 |
def get_data_df(self, data_list):
|
| 168 |
"""
|
| 169 |
Converts a list of game data JSON objects into a Polars DataFrame.
|