def get_game_events(team_name): game = get_todays_game(team_name) if game != None: events = mlbgame.game_events(game.game_id) return events else: return ("Looks like there's no " + team_name + " game today")
def real_time_game(team_query): team_plural = get_team_plural(team_query) #print(team_plural) # debug now = datetime.datetime.now() year = now.year month = now.month day = now.day try: game_id = mlb.day(year, month, day, home=team_plural, away=team_plural)[0].game_id except: game_is_null_msg() return #game_id = "2019_05_13_oakmlb_seamlb_1" # For debugging # Get team abbreviations correctly formatted game_id_split = game_id.split("_") away_abbreviation = game_id_split[3][0:3].upper() away_score_str = "| " + away_abbreviation + " |" home_abbreviation = game_id_split[4][0:3].upper() home_score_str = "| " + home_abbreviation + " |" inningIndex = 0 prev_box_score_str = None is_top = True last_event = "NONE" # Continuously loop until game is over, user exits, or error while True: try: # API can be buggy innings_list = mlb.box_score(game_id).__dict__[ "innings"] # Get list of innings. Each inning is a dict e.g. `{'inning': 1, 'home': 1, 'away': 0}` except Exception as e: print(e) game_is_null_msg() return cur_box_score_str = get_box_score_str(innings_list, away_score_str, home_score_str) if cur_box_score_str != prev_box_score_str: print(cur_box_score_str) prev_box_score_str = cur_box_score_str game_events = mlb.game_events( game_id) # List of lists, each indice is a full inning's events cur_inning_num = len(game_events) # Get current inning num for inning_events in game_events: # Loop through all innings because API gives them out of order if inning_events.num == cur_inning_num: # If current inning inning if len(inning_events.bottom) > 0: # if bottom inning # for ev in inning_events.bottom: # print(ev.des) cur_event = inning_events.bottom[len(inning_events.bottom) - 1].des if cur_event != last_event and cur_event != "": print(cur_event) last_event = cur_event elif len(inning_events.top) > 0: # if top inning # for ev in inning_events.bottom: # print(ev.des) cur_event = inning_events.top[len(inning_events.top) - 1].des if cur_event != last_event and cur_event != "": print(cur_event) last_event = cur_event
def printCurrentGame(game): print("\nCURRENT GAME INFO\n=================\n") current_inning = mlbgame.game_events(game.game_id)[-1] players = mlbgame.players(game.game_id) playerlist = players.home_players + players.away_players if len(current_inning.bottom) > 0: print("Bottom " + str(current_inning) + "\n") last_ab = current_inning.bottom[-1] elif len(current_inning.top) > 0: print("Top " + str(current_inning) + "\n") last_ab = current_inning.top[-1] else: print("Upcoming " + str(current_inning) + "\n") current_inning = mlbgame.game_events(game.game_id)[-2] last_ab = current_inning.bottom[-1] pitcher = getPlayer(last_ab.pitcher, playerlist) batter = getPlayer(last_ab.batter, playerlist) pitcherStatus = pitcher.team_abbrev + " " + "Pitching: " + pitcher.first + " " + pitcher.last + ", ERA: " + ( "%.2f" % pitcher.era) batterStatus = batter.team_abbrev + " " + "Batting: " + batter.first + " " + batter.last + ", AVG: " + ( "%.3f" % batter.avg)[1:] print(pitcherStatus) print(batterStatus) if (last_ab.b1 or last_ab.b2 or last_ab.b3): base_status = "Runners on: " else: base_status = "No Runners On" if last_ab.b1: base_status += "First " if last_ab.b2: base_status += "Second " if last_ab.b3: base_status += "Third" print("\n" + str(last_ab.o) + " Out(s), " + base_status + "\n") print("SCORE\n=====\n" + game.home_team + ": " + str(game.home_team_runs) + "\n" + game.away_team + ": " + str(game.away_team_runs))
def __current_inning(self, game_id): innings = mlbgame.game_events(game_id) inning_status = {} inning_status['number'] = len(innings) current_inning = innings[-1] is_bottom = bool(len(current_inning.bottom)) at_bats = current_inning.bottom if is_bottom else current_inning.top inning_status['bottom'] = is_bottom inning_status['at_bat'] = self.__current_at_bat(at_bats[-1]) return inning_status
def printCurrentGame(game): print("\nCURRENT GAME INFO\n=================\n") current_inning = mlbgame.game_events(game.game_id)[-1] players = mlbgame.players(game.game_id) playerlist = players.home_players + players.away_players if len(current_inning.bottom) > 0: print("Bottom " + str(current_inning) + "\n") last_ab = current_inning.bottom[-1] elif len(current_inning.top) > 0: print("Top " + str(current_inning) + "\n") last_ab = current_inning.top[-1] else : print("Upcoming " + str(current_inning) + "\n") current_inning = mlbgame.game_events(game.game_id)[-2] last_ab = current_inning.bottom[-1] pitcher = getPlayer(last_ab.pitcher, playerlist) batter = getPlayer(last_ab.batter, playerlist) pitcherStatus = pitcher.team_abbrev + " " + "Pitching: " + pitcher.first + " " + pitcher.last + ", ERA: " + ("%.2f"%pitcher.era) batterStatus = batter.team_abbrev + " " + "Batting: " + batter.first + " " + batter.last + ", AVG: " + ("%.3f"%batter.avg)[1:] print(pitcherStatus) print(batterStatus) if(last_ab.b1 or last_ab.b2 or last_ab.b3): base_status = "Runners on: " else : base_status = "No Runners On" if last_ab.b1: base_status += "First " if last_ab.b2: base_status += "Second " if last_ab.b3: base_status += "Third" print("\n" + str(last_ab.o) + " Out(s), " + base_status + "\n") print("SCORE\n=====\n" + game.home_team+ ": " + str(game.home_team_runs) + "\n" + game.away_team + ": " + str(game.away_team_runs))
def test_game_events(self): """Test that game events properties are of the correct type and that the first atbat event and first action event of the match http://gd2.mlb.com/components/game/mlb/year_2016/month_08/day_02/gid_2016_08_02_nyamlb_nynmlb_1/game_events.xml are exactly correct.""" events = mlbgame.game_events('2016_08_02_nyamlb_nynmlb_1') for inning in events: self.assertIsInstance(inning.num, int) self.assertIsInstance(inning.top, list) self.assertIsInstance(inning.bottom, list) atbats_actions = inning.top + inning.bottom for atbat_action in atbats_actions: if isinstance(atbat_action, AtBat) and atbat_action.num == 1: # test an atbat self.assertEqual(inning.num, 1) self.assertEqual(inning.__str__(), 'Inning 1') self.assertEqual(atbat_action.away_team_runs, 0) self.assertEqual(atbat_action.b, 1) self.assertEqual(atbat_action.b1, '') self.assertEqual(atbat_action.b2, '') self.assertEqual(atbat_action.b3, '') self.assertEqual(atbat_action.batter, 458731) self.assertEqual( atbat_action.des, 'Brett Gardner flies out to center fielder Alejandro De Aza. ' ) self.assertEqual( atbat_action.des_es, 'Brett Gardner batea elevado de out a jardinero central Alejandro De Aza. ' ) self.assertEqual(atbat_action.event, 'Flyout') self.assertEqual(atbat_action.event_es, 'Elevado de Out') self.assertEqual(atbat_action.event_num, 6) self.assertEqual(atbat_action.home_team_runs, 0) self.assertEqual(atbat_action.num, 1) self.assertEqual(atbat_action.o, 1) self.assertEqual(atbat_action.pitcher, 594798) self.assertEqual(atbat_action.play_guid, 'e91fe0bf-6e1e-40a3-953c-47a943b37638') self.assertEqual(atbat_action.s, 0) self.assertEqual(atbat_action.start_tfs, 231105) self.assertEqual(atbat_action.start_tfs_zulu, '2016-08-02T23:11:05Z') self.assertEqual( atbat_action.__str__(), 'Brett Gardner flies out to center fielder Alejandro De Aza. ' ) pitch = atbat_action.pitches[0] self.assertEqual(pitch.des, 'Ball') self.assertEqual(pitch.des_es, 'Bola mala') self.assertEqual(pitch.pitch_type, 'FT') self.assertEqual(pitch.start_speed, 95.2) self.assertEqual(pitch.type, 'B') self.assertEqual(pitch.__str__(), 'Pitch: FT at 95.2: Ball') if atbat_action.event_num == 229 and isinstance( atbat_action, Action): # test an action self.assertEqual(inning.num, 4) self.assertEqual(inning.__str__(), 'Inning 4') self.assertEqual(atbat_action.away_team_runs, 0) self.assertEqual(atbat_action.b, 1) self.assertEqual( atbat_action.des, 'With Michael Conforto batting, wild pitch by Masahiro Tanaka, James Loney to 2nd. ' ) self.assertEqual( atbat_action.des_es, 'Con Michael Conforto bateando, lanzamiento desviado de Masahiro Tanaka, James Loney a 2da. ' ) self.assertEqual(atbat_action.event, 'Wild Pitch') self.assertEqual(atbat_action.event_es, 'Lanzamiento Descontrolado') self.assertEqual(atbat_action.event_num, 229) self.assertEqual(atbat_action.home_team_runs, 2) self.assertEqual(atbat_action.o, 2) self.assertEqual(atbat_action.pitch, 4) self.assertEqual(atbat_action.player, 425766) self.assertEqual(atbat_action.play_guid, '79d308c3-585d-4473-82ac-9e1a311f26a1') self.assertEqual(atbat_action.s, 2) self.assertEqual(atbat_action.tfs, 2130) self.assertEqual(atbat_action.tfs_zulu, '2016-08-03T00:21:30Z') self.assertEqual( atbat_action.__str__(), 'With Michael Conforto batting, wild pitch by Masahiro Tanaka, James Loney to 2nd. ' ) # atbat specific tests if isinstance(atbat_action, AtBat): self.assertIsInstance(atbat_action.b1, (int, str)) self.assertIsInstance(atbat_action.b2, (int, str)) self.assertIsInstance(atbat_action.b3, (int, str)) self.assertIsInstance(atbat_action.batter, int) self.assertIsInstance(atbat_action.num, int) self.assertIsInstance(atbat_action.pitcher, int) self.assertIsInstance(atbat_action.pitches, list) self.assertIsInstance(atbat_action.start_tfs, int) self.assertIsInstance(atbat_action.start_tfs_zulu, str) for pitch in atbat_action.pitches: self.assertIsInstance(pitch.des, str) try: self.assertIsInstance(pitch.des_es, (unicode, str)) except NameError: self.assertIsInstance(pitch.des_es, str) self.assertIsInstance(pitch.pitch_type, str) self.assertIsInstance(pitch.start_speed, float) self.assertIsInstance(pitch.sv_id, (str, int)) self.assertIsInstance(pitch.type, str) # action specific tests if isinstance(atbat_action, Action): self.assertIsInstance(atbat_action.pitch, int) self.assertIsInstance(atbat_action.player, int) self.assertIsInstance(atbat_action.tfs, int) self.assertIsInstance(atbat_action.tfs_zulu, str) # mutual tests self.assertIsInstance(atbat_action.away_team_runs, int) self.assertIsInstance(atbat_action.b, int) self.assertIsInstance(atbat_action.des, str) try: self.assertIsInstance(atbat_action.des_es, (unicode, str)) except NameError: self.assertIsInstance(atbat_action.des_es, str) self.assertIsInstance(atbat_action.event, str) try: self.assertIsInstance(atbat_action.event_es, (unicode, str)) except NameError: self.assertIsInstance(atbat_action.event_es, str) self.assertIsInstance(atbat_action.event_num, int) self.assertIsInstance(atbat_action.home_team_runs, int) self.assertIsInstance(atbat_action.o, int) self.assertIsInstance(atbat_action.play_guid, str) self.assertIsInstance(atbat_action.s, int)
def test_game_events_empty(self): self.assertRaises(ValueError, lambda: mlbgame.game_events('game_id')) self.assertRaises( ValueError, lambda: mlbgame.game_events('2016_08_02_nymlb_nymlb_1'))
def parse_events(ig): ge = mlbgame.game_events(ig) for i in ge: for j in ge[i]: for k in ge[i][j]: print k
def test_game_innings(self): innings = mlbgame.game_events('2016_08_02_nyamlb_nynmlb_1', True) for inning in innings: self.assertIsInstance(inning.num, int) if inning.num == 1: i = inning self.assertIsInstance(inning.top, list) self.assertIsInstance(inning.bottom, list) atbats = inning.top + inning.bottom for atbat in atbats: if inning.num == 1 and atbat.num == 1: ab = atbat # TODO: distinguish assertions on AtBat and Action objects # some of the assertions test attributes specific to AtBats, # so we need to skip Actions. if not isinstance(atbat, mlbgame.events.AtBat): continue self.assertIsInstance(atbat.away_team_runs, int) self.assertIsInstance(atbat.b, int) self.assertIsInstance(atbat.batter, int) self.assertIsInstance(atbat.stand, str) self.assertIsInstance(atbat.b_height,(str,int)) self.assertIsInstance(atbat.des, str) self._assert_stringlike(atbat.des_es) self.assertIsInstance(atbat.event, str) self._assert_stringlike(atbat.event_es) self.assertIsInstance(atbat.event_num, int) self.assertIsInstance(atbat.home_team_runs, int) self.assertIsInstance(atbat.num, int) self.assertIsInstance(atbat.o, int) self.assertIsInstance(atbat.pitcher, int) self.assertIsInstance(atbat.p_throws, str) self.assertIsInstance(atbat.pitches, list) self.assertIsInstance(atbat.play_guid, str) self.assertIsInstance(atbat.s, int) self.assertIsInstance(atbat.start_tfs, int) self.assertIsInstance(atbat.start_tfs_zulu, str) for pitch in atbat.pitches: self.assertIsInstance(pitch.des, str) self._assert_stringlike(pitch.des_es) self.assertIsInstance(pitch.id, int) self.assertIsInstance(pitch.type, str) self.assertIsInstance(pitch.code, str) self.assertIsInstance(pitch.tfs, int) self.assertIsInstance(pitch.tfs_zulu, str) self.assertIsInstance(pitch.x, (int, float)) self.assertIsInstance(pitch.y, (int, float)) self.assertIsInstance(pitch.event_num, int) self.assertIsInstance(pitch.sv_id, (str, int)) self.assertIsInstance(pitch.play_guid, str) self.assertIsInstance(pitch.start_speed, (int, float)) self.assertIsInstance(pitch.end_speed, (int, float)) self.assertIsInstance(pitch.sz_top, (int, float)) self.assertIsInstance(pitch.sz_bot, (int, float)) self.assertIsInstance(pitch.pfx_x, (int, float)) self.assertIsInstance(pitch.pfx_z, (int, float)) self.assertIsInstance(pitch.px, (int, float)) self.assertIsInstance(pitch.pz, (int, float)) self.assertIsInstance(pitch.x0, (int, float)) self.assertIsInstance(pitch.y0, (int, float)) self.assertIsInstance(pitch.z0, (int, float)) self.assertIsInstance(pitch.vx0, (int, float)) self.assertIsInstance(pitch.vy0, (int, float)) self.assertIsInstance(pitch.vz0, (int, float)) self.assertIsInstance(pitch.ax, (int, float)) self.assertIsInstance(pitch.ay, (int, float)) self.assertIsInstance(pitch.az, (int, float)) self.assertIsInstance(pitch.break_y, float) self.assertIsInstance(pitch.break_angle, float) self.assertIsInstance(pitch.break_length, float) self.assertIsInstance(pitch.pitch_type, str) self.assertIsInstance(pitch.type_confidence, float) self.assertIsInstance(pitch.zone, int) self.assertIsInstance(pitch.nasty, int) self.assertIsInstance(pitch.spin_dir, float) self.assertIsInstance(pitch.spin_rate, float) self.assertIsInstance(pitch.cc, str) self.assertIsInstance(pitch.mt, str) inning = i self.assertEqual(inning.num, 1) self.assertEqual(inning.__str__(), 'Inning 1') atbat = ab self.assertEqual(atbat.away_team_runs, 0) self.assertEqual(atbat.b, 1) self.assertEqual(atbat.batter, 458731) self.assertEqual(atbat.stand, 'L') self.assertEqual(atbat.b_height, '5-11') self.assertEqual(atbat.des, 'Brett Gardner flies out to center fielder Alejandro De Aza. ') self.assertEqual(atbat.des_es, 'Brett Gardner batea elevado de out a jardinero central Alejandro De Aza. ') self.assertEqual(atbat.event, 'Flyout') self.assertEqual(atbat.event_es, 'Elevado de Out') self.assertEqual(atbat.event_num, 6) self.assertEqual(atbat.home_team_runs, 0) self.assertEqual(atbat.num, 1) self.assertEqual(atbat.o, 1) self.assertEqual(atbat.pitcher, 594798) self.assertEqual(atbat.p_throws, 'R') self.assertEqual(atbat.play_guid, 'e91fe0bf-6e1e-40a3-953c-47a943b37638') self.assertEqual(atbat.s, 0) self.assertEqual(atbat.start_tfs, 231105) self.assertEqual(atbat.start_tfs_zulu, '2016-08-02T23:11:05Z') self.assertEqual(atbat.__str__(), 'Brett Gardner flies out to center fielder Alejandro De Aza. ') pitch = atbat.pitches[0] self.assertEqual(pitch.des, 'Ball') self.assertEqual(pitch.des_es, 'Bola mala') self.assertEqual(pitch.id, 3) self.assertEqual(pitch.type, 'B') self.assertEqual(pitch.code, 'B') self.assertEqual(pitch.tfs, 231122) self.assertEqual(pitch.tfs_zulu, "2016-08-02T23:11:22Z") self.assertEqual(pitch.x, 161.25) self.assertEqual(pitch.y, 143.42) self.assertEqual(pitch.event_num, 3) # test below reflects differing behavior of mlbgame.object.setobjattr # returns the string in Python < 3.6 and the int in 3.6+ self.assertIn(pitch.sv_id, (160802191259, "160802_191259")) self.assertEqual(pitch.play_guid, "daeb229c-f106-4360-a7ea-08d4da117424") self.assertEqual(pitch.start_speed, 95.2) self.assertEqual(pitch.end_speed, 86.8) self.assertEqual(pitch.sz_top, 3.08) self.assertEqual(pitch.sz_bot, 1.46) self.assertEqual(pitch.pfx_x, -7.67) self.assertEqual(pitch.pfx_z, 7.29) self.assertEqual(pitch.px, -1.161) self.assertEqual(pitch.pz, 3.532) self.assertEqual(pitch.x0, -1.053) self.assertEqual(pitch.y0, 50) self.assertEqual(pitch.z0, 5.601) self.assertEqual(pitch.vx0, 2.432) self.assertEqual(pitch.vy0, -139.644) self.assertEqual(pitch.vz0, -2.417) self.assertEqual(pitch.ax, -14.963) self.assertEqual(pitch.ay, 34.94) self.assertEqual(pitch.az, -17.886) self.assertEqual(pitch.break_y, 23.7) self.assertEqual(pitch.break_angle, 37.8) self.assertEqual(pitch.break_length, 4.9) self.assertEqual(pitch.pitch_type, "FT") self.assertEqual(pitch.type_confidence, .913) self.assertEqual(pitch.zone, 11) self.assertEqual(pitch.nasty, 56) self.assertEqual(pitch.spin_dir, 226.321) self.assertEqual(pitch.spin_rate, 2149.420) self.assertEqual(pitch.cc, "") self.assertEqual(pitch.mt, "") self.assertEqual(pitch.__str__(), 'Pitch: FT at 95.2: Ball')
def writeGames(game_dictionary): game_live = False current_date = datetime.now() current_date = current_date + timedelta(hours=3) # current_date = date.fromisoformat('2019-05-02') # current_date = datetime.combine(current_date, datetime.min.time()) for game in game_dictionary: if game_dictionary[game].game_status == 'IN_PROGRESS': game_live = True for game in game_dictionary: diff = game_dictionary[game].date - current_date diff_days = diff.days if game_dictionary[game].game_status == 'IN_PROGRESS': game_link = game_dictionary[game].game_id current_game = mlbgame.game_events(game_link) current_in = len(current_game) current_inning = current_game[current_in - 1] try: bottom_of_inning = current_inning.bottom[0] except Exception: top_or_bottom = 'T' # Top of inning current_event_num = len(current_inning.top) current_event = current_inning.top[current_event_num - 1] else: top_or_bottom = 'B' # Bottom of inning current_event_num = len(current_inning.bottom) current_event = current_inning.bottom[current_event_num - 1] current_outs = current_event.o if current_in >= 9: if current_outs == 3: if current_event.away_team_runs < current_event.home_team_runs: if top_or_bottom == 'T': Static.end_game = True elif current_event.away_team_runs != current_event.home_team_runs: if top_or_bottom == 'B': Static.end_game = True if game_dictionary[game].home_team == Static.teamname: print( '{0:10}{1:2} {6:10} {2:2} {3:1}{4:2} {5:1} Out/s'.format( game_dictionary[game].away_team, current_event.away_team_runs, current_event.home_team_runs, top_or_bottom, str(current_in), current_outs, game_dictionary[game].home_team)) else: print( '{6:10}{0:2} {1:10} {2:2} {3:1}{4:2} {5:1} Out/s'.format( current_event.away_team_runs, game_dictionary[game].home_team, current_event.home_team_runs, top_or_bottom, str(current_in), current_outs, game_dictionary[game].away_team)) elif game_dictionary[game].game_status == 'FINAL': if not game_live: final_score = game_dictionary[ game].home_team_runs - game_dictionary[game].away_team_runs if final_score > 0: home_won = True else: home_won = False if game_dictionary[game].home_team == Static.teamname: if home_won: print('{0:10}{1:2} {3:10} {2:2} W'.format( game_dictionary[game].away_team, game_dictionary[game].away_team_runs, game_dictionary[game].home_team_runs, game_dictionary[game].home_team)) else: print('{0:10}{1:2} {3:10} {2:2} L'.format( game_dictionary[game].away_team, game_dictionary[game].away_team_runs, game_dictionary[game].home_team_runs, game_dictionary[game].home_team)) else: if home_won: print('{3:10} {0:2} {1:10} {2:2} L'.format( game_dictionary[game].away_team_runs, game_dictionary[game].home_team, game_dictionary[game].home_team_runs, game_dictionary[game].home_team)) else: print('{3:10}{0:2} {1:10} {2:2} W'.format( game_dictionary[game].away_team_runs, game_dictionary[game].home_team, game_dictionary[game].home_team_runs, game_dictionary[game].home_team)) elif game_dictionary[game].game_status == 'PRE_GAME': if game_dictionary[game].home_team == Static.teamname: print('{0:10} P - {1:17} T-{2:2} {3} EDT'.format( game_dictionary[game].away_team, game_dictionary[game].p_pitcher_away, diff_days, game_dictionary[game].game_start_time)) else: print('@{0:9} P - {1:17} T-{2:2} {3} EDT'.format( game_dictionary[game].home_team, game_dictionary[game].p_pitcher_home, diff_days, game_dictionary[game].game_start_time)) return game_live
else: athome = False otherteam = game.home_team print("Today, the %s are visiting the %s" % (myteam, otherteam)) print("The game start time is %s" % (game.game_start_time)) # print the score print(game) status = game.game_status print("The status of this game is %s" % (status)) if (status != 'FINAL' and status != 'PRE_GAME'): events = mlbgame.game_events(game.game_id) this_inning = len(events) # Point event to the current inning for event in events: if (event.num == this_inning): break # Figure out if we're in the top or the bottom if (len(event.bottom) == 0): this_half = 'top' this_atbat = event.top[-1] else:
def update(self): self.__init__() # reset everything to False now = datetime.datetime.now(pytz.timezone('America/New_York')) day = mlbgame.day(now.year, now.month, now.day, home=myteam, away=myteam) if (len(day) == 0): return False game = day[0] self.game_today = True status = game.game_status self.status = status self.score_home = game.home_team_runs self.score_away = game.away_team_runs if (status == 'IN_PROGRESS'): self.game_on = True elif (status == 'FINAL'): self.game_over = True elif (status == 'PRE_GAME'): self.pre_game = True start_time = game.game_start_time status = game.game_status start_date = game.date start_date_aware = newyork.localize(start_date) minutes_to_go = (start_date_aware - now).total_seconds() / 60.0 self.minutes_to_go = minutes_to_go if (myteam == game.home_team): athome = True otherteam = game.away_team else: athome = False otherteam = game.home_team self.score_string = game.nice_score() if (status == 'FINAL'): self.game_won = (athome and (self.score_home > self.score_away) or (not athome and (self.score_home < self.score_away))) if (status == 'IN_PROGRESS'): events = mlbgame.game_events(game.game_id) this_inning = len(events) # Point event to the current inning for event in events: if (event.num == this_inning): break # Figure out if we're in the top or the bottom if (len(event.bottom) == 0): this_half = 'top' if (len(event.top) == 0): this_atbat = False else: this_atbat = event.top[-1] else: this_half = 'bottom' this_atbat = event.bottom[-1] # Print the last thing the happened. atbat = this_atbat if (not atbat): atbat_string = 'Changing Innings' else: # print(atbat.b,atbat.s,atbat.o,atbat.away_team_runs,atbat.home_team_runs) atbat_string = atbat.nice_output() self.event = atbat_string if ((athome and this_half == 'bottom') or not athome and this_half == 'top'): if ('singles' in atbat_string): self.hit = True if ('doubles' in atbat_string): self.hit = True if ('triples' in atbat_string): self.hit = True if ('homers' in atbat_string): self.home_run = True if ('walks' in atbat_string): self.walk = True if ('scores' in atbat_string): self.run = True return True
def test_game_events(self): events = mlbgame.game_events('2016_08_02_nyamlb_nynmlb_1') for inning in events: self.assertIsInstance(inning.num, int) if inning.num == 1: i = inning self.assertIsInstance(inning.top, list) self.assertIsInstance(inning.bottom, list) atbats = inning.top + inning.bottom for atbat in atbats: if inning.num == 1 and atbat.num == 1: ab = atbat self.assertIsInstance(atbat.away_team_runs, int) self.assertIsInstance(atbat.b, int) self.assertIsInstance(atbat.b1, (int, str)) self.assertIsInstance(atbat.b2, (int, str)) self.assertIsInstance(atbat.b3, (int, str)) self.assertIsInstance(atbat.batter, int) self.assertIsInstance(atbat.des, str) try: self.assertIsInstance(atbat.des_es, (unicode, str)) except NameError: self.assertIsInstance(atbat.des_es, str) self.assertIsInstance(atbat.event, str) try: self.assertIsInstance(atbat.event_es, (unicode, str)) except NameError: self.assertIsInstance(atbat.event_es, str) self.assertIsInstance(atbat.event_num, int) self.assertIsInstance(atbat.home_team_runs, int) self.assertIsInstance(atbat.num, int) self.assertIsInstance(atbat.o, int) self.assertIsInstance(atbat.pitcher, int) self.assertIsInstance(atbat.pitches, list) self.assertIsInstance(atbat.play_guid, str) self.assertIsInstance(atbat.s, int) self.assertIsInstance(atbat.start_tfs, int) self.assertIsInstance(atbat.start_tfs_zulu, str) for pitch in atbat.pitches: self.assertIsInstance(pitch.des, str) try: self.assertIsInstance(pitch.des_es, (unicode, str)) except NameError: self.assertIsInstance(pitch.des_es, str) self.assertIsInstance(pitch.pitch_type, str) self.assertIsInstance(pitch.start_speed, float) self.assertIsInstance(pitch.sv_id, (str, int)) self.assertIsInstance(pitch.type, str) inning = i self.assertEqual(inning.num, 1) self.assertEqual(inning.__str__(), 'Inning 1') atbat = ab self.assertEqual(atbat.away_team_runs, 0) self.assertEqual(atbat.b, 1) self.assertEqual(atbat.b1, '') self.assertEqual(atbat.b2, '') self.assertEqual(atbat.b3, '') self.assertEqual(atbat.batter, 458731) self.assertEqual( atbat.des, 'Brett Gardner flies out to center fielder Alejandro De Aza. ') self.assertEqual( atbat.des_es, 'Brett Gardner batea elevado de out a jardinero central Alejandro De Aza. ' ) self.assertEqual(atbat.event, 'Flyout') self.assertEqual(atbat.event_es, 'Elevado de Out') self.assertEqual(atbat.event_num, 6) self.assertEqual(atbat.home_team_runs, 0) self.assertEqual(atbat.num, 1) self.assertEqual(atbat.o, 1) self.assertEqual(atbat.pitcher, 594798) self.assertEqual(atbat.play_guid, 'e91fe0bf-6e1e-40a3-953c-47a943b37638') self.assertEqual(atbat.s, 0) self.assertEqual(atbat.start_tfs, 231105) self.assertEqual(atbat.start_tfs_zulu, '2016-08-02T23:11:05Z') self.assertEqual( atbat.__str__(), 'Brett Gardner flies out to center fielder Alejandro De Aza. ') pitch = atbat.pitches[0] self.assertEqual(pitch.des, 'Ball') self.assertEqual(pitch.des_es, 'Bola mala') self.assertEqual(pitch.pitch_type, 'FT') self.assertEqual(pitch.start_speed, 95.2) self.assertEqual(pitch.type, 'B') self.assertEqual(pitch.__str__(), 'Pitch: FT at 95.2: Ball')