Example #1
0
 def test_retro_id(self):
     """
     Retro ID
     """
     self.assertEqual(Game._get_retro_id('SEA', dt.strptime('2015-08-12', '%Y-%m-%d'), 1), 'SEA201508120')
     self.assertEqual(Game._get_retro_id('OAK', dt.strptime('2015-04-03', '%Y-%m-%d'), 2), 'OAK201504031')
     self.assertEqual(Game._get_retro_id('MIA', dt.strptime('2015-10-01', '%Y-%m-%d'), 3), 'MIA201510012')
Example #2
0
 def test_team_stadium_not_exists(self):
     """
     Stadium Data(not exists)
     """
     self.assertEqual(Game._get_stadium_attribute(self.dummy, 'id'), 'Unknown')
     self.assertEqual(Game._get_stadium_attribute(self.dummy, 'name'), 'Unknown')
     self.assertEqual(Game._get_stadium_attribute(self.dummy, 'location'), 'Unknown')
Example #3
0
 def test_team_stadium_exists(self):
     """
     Stadium Data
     """
     self.assertEqual(Game._get_stadium_attribute(self.game, 'id'), '680')
     self.assertEqual(Game._get_stadium_attribute(self.game, 'name'), 'Safeco Field')
     self.assertEqual(Game._get_stadium_attribute(self.game, 'location'), 'Seattle, WA')
Example #4
0
 def test_regular_season(self):
     """
     Regular Season Data
     """
     self.assertEqual(Game._get_game_type_des('R'), 'Regular Season')
     self.assertEqual(Game._get_st_fl('R'), 'F')
     self.assertEqual(Game._get_regseason_fl('R'), 'T')
     self.assertEqual(Game._get_playoff_fl('R'), 'F')
Example #5
0
 def test_league_champion_ship(self):
     """
     League Champion Ship Data
     """
     self.assertEqual(Game._get_game_type_des('L'), 'LCS')
     self.assertEqual(Game._get_st_fl('L'), 'F')
     self.assertEqual(Game._get_regseason_fl('L'), 'F')
     self.assertEqual(Game._get_playoff_fl('L'), 'T')
Example #6
0
 def test_game_type_unknown(self):
     """
     Unknow Game Type
     """
     self.assertEqual(Game._get_game_type_des('U'), 'Unknown')
     self.assertEqual(Game._get_st_fl('U'), 'F')
     self.assertEqual(Game._get_regseason_fl('U'), 'F')
     self.assertEqual(Game._get_playoff_fl('U'), 'F')
Example #7
0
 def test_world_series(self):
     """
     World Series Data
     """
     self.assertEqual(Game._get_game_type_des('W'), 'World Series')
     self.assertEqual(Game._get_st_fl('W'), 'F')
     self.assertEqual(Game._get_regseason_fl('W'), 'F')
     self.assertEqual(Game._get_playoff_fl('W'), 'T')
Example #8
0
 def test_divisional_series(self):
     """
     Divisional Series Data
     """
     self.assertEqual(Game._get_game_type_des('D'), 'Divisional Series')
     self.assertEqual(Game._get_st_fl('D'), 'F')
     self.assertEqual(Game._get_regseason_fl('D'), 'F')
     self.assertEqual(Game._get_playoff_fl('D'), 'T')
Example #9
0
 def test_wild_card_game(self):
     """
     Wild-card Game Data
     """
     self.assertEqual(Game._get_game_type_des('F'), 'Wild-card Game')
     self.assertEqual(Game._get_st_fl('F'), 'F')
     self.assertEqual(Game._get_regseason_fl('F'), 'F')
     self.assertEqual(Game._get_playoff_fl('F'), 'T')
Example #10
0
 def test_spring_training(self):
     """
     Spring Training Data
     """
     self.assertEqual(Game._get_game_type_des('S'), 'Spring Training')
     self.assertEqual(Game._get_st_fl('S'), 'T')
     self.assertEqual(Game._get_regseason_fl('S'), 'F')
     self.assertEqual(Game._get_playoff_fl('S'), 'F')
Example #11
0
    def test_generate_game_object(self):
        """
        Game Object Data
        """
        game = Game._generate_game_object(self.game, dt.strptime('2015-08-12', '%Y-%m-%d'), 1)

        # Base Data
        self.assertEqual(game.game_type, 'R')
        self.assertEqual(game.game_type_des, 'Regular Season')
        self.assertEqual(game.st_fl, 'F')
        self.assertEqual(game.regseason_fl, 'T')
        self.assertEqual(game.local_game_time, '12:40')
        self.assertEqual(game.game_id, '415346')

        # Team Data
        self.assertEqual(game.home_team_id, 'sea')
        self.assertEqual(game.away_team_id, 'bal')
        self.assertEqual(game.home_team_lg, 'AL')
        self.assertEqual(game.away_team_lg, 'AL')
        self.assertEqual(game.home_team_name, 'Seattle')
        self.assertEqual(game.away_team_name, 'Baltimore')
        self.assertEqual(game.home_team_name_full, 'Seattle Mariners')
        self.assertEqual(game.away_team_name_full, 'Baltimore Orioles')

        # Stadium Data
        self.assertEqual(game.park_id, '680')
        self.assertEqual(game.park_name, 'Safeco Field')
        self.assertEqual(game.park_loc, 'Seattle, WA')

        # Retro ID
        self.assertEqual(game.retro_game_id, 'SEA201508120')
Example #12
0
    def test_row(self):
        """
        Game Object Data(row data)
        """
        game = Game._generate_game_object(self.game, dt.strptime('2015-08-13', '%Y-%m-%d'), 2)
        row = game.row()

        # Base Data
        self.assertEqual(row['game_type'], 'R')
        self.assertEqual(row['game_type_des'], 'Regular Season')
        self.assertEqual(row['st_fl'], 'F')
        self.assertEqual(row['regseason_fl'], 'T')
        self.assertEqual(row['local_game_time'], '12:40')
        self.assertEqual(row['game_id'], '415346')

        # Team Data
        self.assertEqual(row['home_team_id'], 'sea')
        self.assertEqual(row['away_team_id'], 'bal')
        self.assertEqual(row['home_team_lg'], 'AL')
        self.assertEqual(row['away_team_lg'], 'AL')
        self.assertEqual(row['home_team_name'], 'Seattle')
        self.assertEqual(row['away_team_name'], 'Baltimore')
        self.assertEqual(row['home_team_name_full'], 'Seattle Mariners')
        self.assertEqual(row['away_team_name_full'], 'Baltimore Orioles')

        # Stadium Data
        self.assertEqual(row['park_id'], '680')
        self.assertEqual(row['park_name'], 'Safeco Field')
        self.assertEqual(row['park_loc'], 'Seattle, WA')

        # Retro ID
        self.assertEqual(row['retro_game_id'], 'SEA201508131')
Example #13
0
 def test_team_attributes_not_exists(self):
     """
     Team attributes Data(not exists)
     """
     self.assertEqual(Game._get_team_attribute(self.dummy, 'home', 'code'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'away', 'code'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'home', 'league'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'away', 'league'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'home', 'name'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'away', 'name'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'home', 'name_full'), 'Unknown')
     self.assertEqual(Game._get_team_attribute(self.dummy, 'home', 'name_full'), 'Unknown')
Example #14
0
 def test_team_attributes_exists(self):
     """
     Team attributes Data
     """
     self.assertEqual(Game._get_team_attribute(self.game, 'home', 'code'), 'sea')
     self.assertEqual(Game._get_team_attribute(self.game, 'away', 'code'), 'bal')
     self.assertEqual(Game._get_team_attribute(self.game, 'home', 'league'), 'AL')
     self.assertEqual(Game._get_team_attribute(self.game, 'away', 'league'), 'AL')
     self.assertEqual(Game._get_team_attribute(self.game, 'home', 'name'), 'Seattle')
     self.assertEqual(Game._get_team_attribute(self.game, 'away', 'name'), 'Baltimore')
     self.assertEqual(Game._get_team_attribute(self.game, 'home', 'name_full'), 'Seattle Mariners')
     self.assertEqual(Game._get_team_attribute(self.game, 'away', 'name_full'), 'Baltimore Orioles')
Example #15
0
    def _download(self, timestamp):
        """
        download MLBAM Game Day
        :param timestamp: day
        """
        games, atbats, pitches = [], [], []
        rosters, coaches, umpires = [], [], []
        timestamp_params = {
            'year': str(timestamp.year),
            'month': str(timestamp.month).zfill(2),
            'day': str(timestamp.day).zfill(2)
        }

        logging.info('->- Game data download start({year}/{month}/{day})'.format(**timestamp_params))

        base_url = self.DELIMITER.join([self.url, self.PAGE_URL_GAME_DAY.format(**timestamp_params)])
        html = MlbamUtil.find_xml(base_url, self.parser)

        href = self.PAGE_URL_GAME_PREFIX.format(**timestamp_params)
        for gid in html.find_all('a', href=re.compile(href)):
            gid_path = gid.get_text().strip()
            gid_url = self.DELIMITER.join([base_url, gid_path])
            # Read XML & create dataset
            try:
                game = Game.read_xml(gid_url, self.parser, timestamp, MlbAm._get_game_number(gid_path))
                players = Players.read_xml(gid_url, self.parser, game)
                innings = Inning.read_xml(gid_url, self.parser, game, players)
            except MlbAmHttpNotFound as e:
                logging.warning(e.msg)
                continue

            # append a dataset
            games.append(game.row())
            rosters.extend([roseter.row() for roseter in players.rosters.values()])
            coaches.extend([coach.row() for coach in players.coaches.values()])
            umpires.extend([umpire.row() for umpire in players.umpires.values()])
            atbats.extend(innings.atbats)
            pitches.extend(innings.pitches)

        # writing csv
        day = "".join([timestamp_params['year'], timestamp_params['month'], timestamp_params['day']])
        for params in (
                {'datasets': games, 'filename': Game.DOWNLOAD_FILE_NAME},
                {'datasets': rosters, 'filename': Players.Player.DOWNLOAD_FILE_NAME},
                {'datasets': coaches, 'filename': Players.Coach.DOWNLOAD_FILE_NAME},
                {'datasets': umpires, 'filename': Players.Umpire.DOWNLOAD_FILE_NAME},
                {'datasets': atbats, 'filename': AtBat.DOWNLOAD_FILE_NAME},
                {'datasets': pitches, 'filename': Pitch.DOWNLOAD_FILE_NAME},
        ):
            self._write_csv(params['datasets'], params['filename'].format(day=day, extension=self.extension))
        time.sleep(2)

        logging.info('-<- Game data download end({year}/{month}/{day})'.format(**timestamp_params))
Example #16
0
 def test_not_interleague(self):
     """
     inter league(not)
     """
     self.assertEqual(Game._get_interleague_fl('AL', 'AL'), 'F')
     self.assertEqual(Game._get_interleague_fl('NL', 'NL'), 'F')
Example #17
0
 def test_interleague(self):
     """
     inter league
     """
     self.assertEqual(Game._get_interleague_fl('AL', 'NL'), 'T')
Example #18
0
    def _download(self, timestamp):
        """
        download MLBAM Game Day
        :param timestamp: day
        """
        games, atbats, pitches = [], [], []
        rosters, coaches, umpires = [], [], []
        boxscores, actions = [], []
        timestamp_params = {
            'year': str(timestamp.year),
            'month': str(timestamp.month).zfill(2),
            'day': str(timestamp.day).zfill(2)
        }

        logging.info(
            '->- Game data download start({year}/{month}/{day})'.format(
                **timestamp_params))

        base_url = self.DELIMITER.join(
            [self.url,
             self.PAGE_URL_GAME_DAY.format(**timestamp_params)])
        html = MlbamUtil.find_xml(base_url, self.parser)

        href = self.PAGE_URL_GAME_PREFIX.format(**timestamp_params)
        for gid in html.find_all('a', href=re.compile(href)):
            gid_path = gid.get_text().strip()
            gid_url = self.DELIMITER.join([base_url, gid_path])
            # Read XML & create dataset
            try:
                game = Game.read_xml(gid_url, self.parser, timestamp,
                                     MlbAm._get_game_number(gid_path))
                players = Players.read_xml(gid_url, self.parser, game)
                innings = Inning.read_xml(gid_url, self.parser, game, players)
                boxscore = BoxScore.read_xml(gid_url, self.parser, game,
                                             players)
            except MlbAmHttpNotFound as e:
                logging.warning(e.msg)
                continue

            # append a dataset
            games.append(game.row())
            rosters.extend(
                [roseter.row() for roseter in players.rosters.values()])
            coaches.extend([coach.row() for coach in players.coaches.values()])
            umpires.extend(
                [umpire.row() for umpire in players.umpires.values()])
            atbats.extend(innings.atbats)
            pitches.extend(innings.pitches)
            actions.extend(innings.actions)
            boxscores.append(boxscore.row())

        # writing csv
        day = "".join([
            timestamp_params['year'], timestamp_params['month'],
            timestamp_params['day']
        ])
        for params in (
            {
                'datasets': games,
                'filename': Game.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': rosters,
                'filename': Players.Player.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': coaches,
                'filename': Players.Coach.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': umpires,
                'filename': Players.Umpire.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': atbats,
                'filename': AtBat.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': pitches,
                'filename': Pitch.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': boxscores,
                'filename': BoxScore.DOWNLOAD_FILE_NAME
            },
            {
                'datasets': actions,
                'filename': InningAction.DOWNLOAD_FILE_NAME
            },
        ):
            self._write_csv(
                params['datasets'],
                params['filename'].format(day=day, extension=self.extension))
        time.sleep(2)

        logging.info('-<- Game data download end({year}/{month}/{day})'.format(
            **timestamp_params))
Example #19
0
 def test_unknown_interleague(self):
     """
     inter league(unknown)
     """
     self.assertEqual(Game._get_interleague_fl('AL', 'U'), 'U')
     self.assertEqual(Game._get_interleague_fl('U', 'NL'), 'U')