Created
February 16, 2022 14:19
-
-
Save databyjp/dc3460b3b438b84439233b0cdc680c1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fetch_box_score(json_dir, gm_id): | |
""" | |
Download one box score from NBA API | |
:param json_dir: Directory for saving downloaded JSON | |
:param gm_id: NBA game ID | |
:return: | |
""" | |
from nba_api.stats.endpoints import boxscoreadvancedv2 | |
dl_path = os.path.join(json_dir, gm_id + ".json") | |
if os.path.exists(dl_path): | |
logger.info(f"JSON found for game {gm_id}, skipping download.") | |
else: | |
try: | |
logger.info(f"Downloading data for game {gm_id}") | |
try: | |
response = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=gm_id) | |
except: | |
logger.info(f"That didn't work - trying again with '00' prepended to game_id: {gm_id}") | |
gm_id = "00" + gm_id | |
response = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=gm_id) | |
content = json.loads(response.get_json()) | |
if type(content) == dict: | |
with open(dl_path, 'w') as f: | |
json.dump(content, f) | |
logger.info(f"Got data for game {gm_id}") | |
else: | |
logger.info(f"Saved data for game {gm_id} at {dl_path}") | |
except: | |
logger.error(f"Error getting data for game {gm_id}") | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment