Created
February 25, 2022 01:07
-
-
Save databyjp/242f01288266ba391ebe29170489e689 to your computer and use it in GitHub Desktop.
Fetch data with gameID
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_data_w_gameid(json_dir, gm_id, datatype="boxscore"): | |
""" | |
Download a datafile based on gameID as downloaded from NBA API & saves to file | |
:param json_dir: Directory for saving downloaded JSON | |
:param gm_id: NBA game ID | |
:param datatype: What data types to download - determines endpoint to use | |
:return: | |
""" | |
from nba_api.stats.endpoints import boxscoreadvancedv2 | |
from nba_api.live.nba.endpoints import playbyplay | |
if not os.path.exists(json_dir): | |
os.makedirs(json_dir) | |
if str(gm_id)[:2] != '00': | |
gm_id = '00' + str(gm_id) | |
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}") | |
if datatype == "boxscore": | |
response = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=gm_id) | |
elif datatype == "pbp": | |
response = playbyplay.PlayByPlay(gm_id) | |
else: | |
logger.warning("No data type supplied, downloading box score data by default") | |
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 False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment