Exemple #1
0
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 evalGame(game_id, sheet):
    try:
        innings = mlbgame.box_score(game_id).innings
        if not innings:
            print('debug stmt')
        if innings[0]['home'] + innings[0]['away'] is 0:
            id_cell = sheet.find(game_id)
            sheet.update_cell(id_cell.row + 3, id_cell.col + 3, 'win')
            time.sleep(1)
    except:
        print("innings issues")
Exemple #3
0
def getAvgRunsInFirstHome(year, month, team):
    month = mlbgame.games(year, month, home=team)
    games = mlbgame.combine_games(month)
    firstInningRunsOppo = 0
    firstInningRunsHome = 0
    num_of_games = 0
    for game in games:
        innings = mlbgame.box_score(game.game_id).innings
        if innings:
            firstInningRunsOppo += innings[0]['away']
            firstInningRunsHome += innings[0]['home']
            num_of_games += 1
    return {
        "games_played": num_of_games,
        "opponent": firstInningRunsOppo / num_of_games,
        "home": firstInningRunsHome / num_of_games
    }
Exemple #4
0
 def test_box_score(self):
     box_score = mlbgame.box_score('2016_08_02_nyamlb_nynmlb_1')
     self.assertEqual(box_score.game_id, '2016_08_02_nyamlb_nynmlb_1')
     self.assertIsInstance(box_score.innings, list)
     for inning in box_score:
         self.assertIn('inning', inning)
         self.assertIn('away', inning)
         self.assertIn('home', inning)
     self.assertEqual(box_score.innings[0]['inning'], 1)
     self.assertEqual(box_score.innings[0]['away'], 0)
     self.assertEqual(box_score.innings[0]['home'], 0)
     self.assertEqual(box_score.innings[1]['inning'], 2)
     self.assertEqual(box_score.innings[1]['away'], 0)
     self.assertEqual(box_score.innings[1]['home'], 0)
     self.assertEqual(box_score.innings[2]['inning'], 3)
     self.assertEqual(box_score.innings[2]['away'], 0)
     self.assertEqual(box_score.innings[2]['home'], 2)
     self.assertEqual(box_score.innings[3]['inning'], 4)
     self.assertEqual(box_score.innings[3]['away'], 0)
     self.assertEqual(box_score.innings[3]['home'], 0)
     self.assertEqual(box_score.innings[4]['inning'], 5)
     self.assertEqual(box_score.innings[4]['away'], 0)
     self.assertEqual(box_score.innings[4]['home'], 1)
     self.assertEqual(box_score.innings[5]['inning'], 6)
     self.assertEqual(box_score.innings[5]['away'], 0)
     self.assertEqual(box_score.innings[5]['home'], 0)
     self.assertEqual(box_score.innings[6]['inning'], 7)
     self.assertEqual(box_score.innings[6]['away'], 0)
     self.assertEqual(box_score.innings[6]['home'], 4)
     self.assertEqual(box_score.innings[7]['inning'], 8)
     self.assertEqual(box_score.innings[7]['away'], 0)
     self.assertEqual(box_score.innings[7]['home'], 0)
     self.assertEqual(box_score.innings[8]['inning'], 9)
     self.assertEqual(box_score.innings[8]['away'], 1)
     self.assertEqual(box_score.innings[8]['home'], 'x')
     self.assertEqual(box_score.print_scoreboard(), (
         'Inning\t1 2 3 4 5 6 7 8 9 \n'
         '---------------------------\n'
         'Away\t0 0 0 0 0 0 0 0 1 \n'
         'Home\t0 0 2 0 1 0 4 0 x '
         ))
Exemple #5
0
 def test_box_score_empty(self):
     self.assertRaises(ValueError, lambda: mlbgame.box_score('game_id'))
     self.assertRaises(
         ValueError, lambda: mlbgame.box_score('2016_08_02_nymlb_nymlb_1'))
Exemple #6
0
def get_box_score(game):
    return mlb.box_score(game["game_id"])