From 04dd74600cfad092e5026f27360706b8ced2f33b Mon Sep 17 00:00:00 2001 From: ikeyoda <73535614+ikeyoda@users.noreply.github.com> Date: Sat, 29 Jan 2022 13:26:43 -0500 Subject: [PATCH 1/2] Fixed https://github.com/panzarino/mlbgame/issues/148 Checks if raw box score exists, and if not, avoids accessing additional stats to avoid error --- mlbgame/stats.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/mlbgame/stats.py b/mlbgame/stats.py index 5080414..ede6eba 100644 --- a/mlbgame/stats.py +++ b/mlbgame/stats.py @@ -3,7 +3,7 @@ """Module that controls getting stats and creating objects to hold that information.""" -from lxml import etree +import lxml.etree as etree import mlbgame.data import mlbgame.object @@ -69,41 +69,49 @@ def player_stats(game_id): """ # get data from data module box_score = mlbgame.data.get_box_score(game_id) + if mlbgame.data.does_raw_box_score_exist(game_id): + raw_box_score = mlbgame.data.get_raw_box_score(game_id) + # parse XML box_score_tree = etree.parse(box_score).getroot() + if mlbgame.data.does_raw_box_score_exist(game_id): + raw_box_score_tree = etree.parse(raw_box_score).getroot() # get pitching and batting info pitching = box_score_tree.findall('pitching') batting = box_score_tree.findall('batting') # get parsed stats pitching_info = __player_stats_info(pitching, 'pitcher') batting_info = __player_stats_info(batting, 'batter') - # rawboxscore not available after 2018 - try: - raw_box_score = mlbgame.data.get_raw_box_score(game_id) - raw_box_score_tree = etree.parse(raw_box_score).getroot() - additional_stats = __raw_player_stats_info(raw_box_score_tree) + # get parsed additional stats + #additional_stats = __raw_player_stats_info(raw_box_score_tree) + if mlbgame.data.does_raw_box_score_exist(game_id): addl_home_pitching = additional_stats[0]['pitchers'] addl_home_batting = additional_stats[0]['batters'] addl_away_pitching = additional_stats[1]['pitchers'] addl_away_batting = additional_stats[1]['batters'] - + if mlbgame.data.does_raw_box_score_exist(game_id): output = { 'home_pitching': pitching_info[0], 'away_pitching': pitching_info[1], 'home_batting': batting_info[0], 'away_batting': batting_info[1], + + + # Section of person that + 'home_additional_pitching': addl_home_pitching, 'away_additional_pitching': addl_away_pitching, 'home_additional_batting': addl_home_batting, 'away_additional_batting': addl_away_batting } - except etree.XMLSyntaxError: + return output + else: output = { 'home_pitching': pitching_info[0], 'away_pitching': pitching_info[1], 'home_batting': batting_info[0], 'away_batting': batting_info[1], } - return output + return output def __team_stats_info(data, output, output_key): @@ -152,10 +160,12 @@ def team_stats(game_id): """ # get data from data module box_score = mlbgame.data.get_box_score(game_id) - raw_box_score = mlbgame.data.get_raw_box_score(game_id) + if mlbgame.data.does_raw_box_score_exist(game_id): + raw_box_score = mlbgame.data.get_raw_box_score(game_id) # parse XML box_score_tree = etree.parse(box_score).getroot() - raw_box_score_tree = etree.parse(raw_box_score).getroot() + if mlbgame.data.does_raw_box_score_exist(game_id): + raw_box_score_tree = etree.parse(raw_box_score).getroot() # get pitching and batting ingo pitching = box_score_tree.findall('pitching') batting = box_score_tree.findall('batting') @@ -163,7 +173,8 @@ def team_stats(game_id): output = {} output = __team_stats_info(pitching, output, 'pitching') output = __team_stats_info(batting, output, 'batting') - output = __raw_team_stats_info(raw_box_score_tree, output) + if mlbgame.data.does_raw_box_score_exist(game_id): + output = __raw_team_stats_info(raw_box_score_tree, output) return output @@ -206,6 +217,7 @@ def __init__(self, data, game_id, player): self.away_pitching = output['away_pitching'] self.home_batting = output['home_batting'] self.away_batting = output['away_batting'] + self.home_additional_pitching = output['home_additional_pitching'] self.away_additional_pitching = output['away_additional_pitching'] self.home_additional_batting = output['home_additional_batting'] From 73589ef6f3829ab5697bf31d1a0088eafa94c944 Mon Sep 17 00:00:00 2001 From: ikeyoda <73535614+ikeyoda@users.noreply.github.com> Date: Sat, 29 Jan 2022 13:28:04 -0500 Subject: [PATCH 2/2] Update data.py --- mlbgame/data.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/mlbgame/data.py b/mlbgame/data.py index fe03c49..f9c4ae5 100644 --- a/mlbgame/data.py +++ b/mlbgame/data.py @@ -32,20 +32,10 @@ 'version=48') IMPORTANT_DATES = ('http://lookup-service-prod.mlb.com/named.org_history.bam?' 'org_id=1&season={0}') -BCAST_INFO = ('http://mlb.mlb.com/lookup/json/named.mlb_broadcast_info.bam?' - 'team_id={}&season={}') -INNINGS_URL = BASE_URL + 'gid_{3}/inning/inning_all.xml' # Local Directory PWD = os.path.join(os.path.dirname(__file__)) -def get_broadcast_info(team_id, year): - try: - return urlopen(BCAST_INFO.format(team_id, year)) - except HTTPError: - raise ValueError('Failed to retrieve MLB broadcast information.') - - def get_important_dates(year): try: return urlopen(IMPORTANT_DATES.format(year)) @@ -81,22 +71,26 @@ def get_raw_box_score(game_id): except HTTPError: raise ValueError('Could not find a game with that id.') - -def get_game_events(game_id): - """Return the game events file of a game with matching id.""" +def does_raw_box_score_exist(game_id) : + """Checks if box score exists for game id""" year, month, day = get_date_from_game_id(game_id) try: - return urlopen(GAME_URL.format(year, month, day, game_id, - 'game_events.xml')) + urlopen(GAME_URL.format(year, month, day, game_id, + 'rawboxscore.xml')) + return True + except HTTPError: - raise ValueError('Could not find a game with that id.') + return False + + -def get_innings(game_id): - """Return the innings file of a game with matching id.""" +def get_game_events(game_id): + """Return the game events file of a game with matching id.""" year, month, day = get_date_from_game_id(game_id) try: - return urlopen(INNINGS_URL.format(year, month, day, game_id)) + return urlopen(GAME_URL.format(year, month, day, game_id, + 'game_events.xml')) except HTTPError: raise ValueError('Could not find a game with that id.')