def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_get_team_details') \ .and_return((None, None, None, None, None, None)) flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None)
class TestMLBBoxscores: @patch('requests.get', side_effect=mock_pyquery) def setup_method(self, *args, **kwargs): flexmock(Boxscores) \ .should_receive('_get_team_details') \ .and_return((None, None, None, None, None, None)) flexmock(Boxscores) \ .should_receive('_find_games') \ .and_return(None) self.boxscores = Boxscores(None) def test_improper_loser_boxscore_format_skips_game(self): mock_html = pq("""<table class="teams"> <tbody> <tr class="loser"> <td class="right">1</td> <td class="right gamelink"> </td> </tr> <tr class="winner"> <td><a href="/teams/BAL/2017.shtml">Baltimore Orioles</a></td> <td class="right">3</td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert len(games) == 0 def test_improper_winner_boxscore_format_skips_game(self): mock_html = pq("""<table class="teams"> <tbody> <tr class="loser"> <td><a href="/teams/TEX/2017.shtml">Texas Rangers</a></td> <td class="right">1</td> <td class="right gamelink"> <a href="/boxes/BAL/BAL201707170.shtml">Final</a> </td> </tr> <tr class="winner"> <td class="right">3</td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert len(games) == 0
def do_games(year): games = Boxscores(datetime(year, 9, 1), datetime(year + 1, 7, 1)) ngames = [] for month, games in games.games.items(): for game in games: game["month"] = datetime.strptime(month, "%m-%d-%Y") ngames.append(game) df = pd.DataFrame(ngames) return df
def test_boxscores_search_multiple_days(self, *args, **kwargs): expected = { '7-17-2017': [ {'boxscore': 'ATL/ATL201707170', 'away_name': 'Chicago Cubs', 'away_abbr': 'CHC', 'away_score': 4, 'home_name': 'Atlanta Braves', 'home_abbr': 'ATL', 'home_score': 3, 'winning_name': 'Chicago Cubs', 'winning_abbr': 'CHC', 'losing_name': 'Atlanta Braves', 'losing_abbr': 'ATL'}, {'boxscore': 'BAL/BAL201707170', 'away_name': 'Texas Rangers', 'away_abbr': 'TEX', 'away_score': 1, 'home_name': 'Baltimore Orioles', 'home_abbr': 'BAL', 'home_score': 3, 'winning_name': 'Baltimore Orioles', 'winning_abbr': 'BAL', 'losing_name': 'Texas Rangers', 'losing_abbr': 'TEX'}, {'boxscore': 'BOS/BOS201707170', 'away_name': 'Toronto Blue Jays', 'away_abbr': 'TOR', 'away_score': 4, 'home_name': 'Boston Red Sox', 'home_abbr': 'BOS', 'home_score': 3, 'winning_name': 'Toronto Blue Jays', 'winning_abbr': 'TOR', 'losing_name': 'Boston Red Sox', 'losing_abbr': 'BOS'}, {'boxscore': 'CIN/CIN201707170', 'away_name': 'Washington Nationals', 'away_abbr': 'WSN', 'away_score': 6, 'home_name': 'Cincinnati Reds', 'home_abbr': 'CIN', 'home_score': 1, 'winning_name': 'Washington Nationals', 'winning_abbr': 'WSN', 'losing_name': 'Cincinnati Reds', 'losing_abbr': 'CIN'}, {'boxscore': 'COL/COL201707170', 'away_name': 'San Diego Padres', 'away_abbr': 'SDP', 'away_score': 6, 'home_name': 'Colorado Rockies', 'home_abbr': 'COL', 'home_score': 9, 'winning_name': 'Colorado Rockies', 'winning_abbr': 'COL', 'losing_name': 'San Diego Padres', 'losing_abbr': 'SDP'}, {'boxscore': 'HOU/HOU201707170', 'away_name': 'Seattle Mariners', 'away_abbr': 'SEA', 'away_score': 9, 'home_name': 'Houston Astros', 'home_abbr': 'HOU', 'home_score': 7, 'winning_name': 'Seattle Mariners', 'winning_abbr': 'SEA', 'losing_name': 'Houston Astros', 'losing_abbr': 'HOU'}, {'boxscore': 'KCA/KCA201707170', 'away_name': 'Detroit Tigers', 'away_abbr': 'DET', 'away_score': 10, 'home_name': 'Kansas City Royals', 'home_abbr': 'KCR', 'home_score': 2, 'winning_name': 'Detroit Tigers', 'winning_abbr': 'DET', 'losing_name': 'Kansas City Royals', 'losing_abbr': 'KCR'}, {'boxscore': 'MIA/MIA201707170', 'away_name': 'Philadelphia Phillies', 'away_abbr': 'PHI', 'away_score': 5, 'home_name': 'Miami Marlins', 'home_abbr': 'MIA', 'home_score': 6, 'winning_name': 'Miami Marlins', 'winning_abbr': 'MIA', 'losing_name': 'Philadelphia Phillies', 'losing_abbr': 'PHI'}, {'boxscore': 'MIN/MIN201707170', 'away_name': 'New York Yankees', 'away_abbr': 'NYY', 'away_score': 2, 'home_name': 'Minnesota Twins', 'home_abbr': 'MIN', 'home_score': 4, 'winning_name': 'Minnesota Twins', 'winning_abbr': 'MIN', 'losing_name': 'New York Yankees', 'losing_abbr': 'NYY'}, {'boxscore': 'NYN/NYN201707170', 'away_name': 'St. Louis Cardinals', 'away_abbr': 'STL', 'away_score': 6, 'home_name': 'New York Mets', 'home_abbr': 'NYM', 'home_score': 3, 'winning_name': 'St. Louis Cardinals', 'winning_abbr': 'STL', 'losing_name': 'New York Mets', 'losing_abbr': 'NYM'}, {'boxscore': 'OAK/OAK201707170', 'away_name': 'Tampa Bay Rays', 'away_abbr': 'TBR', 'away_score': 3, 'home_name': 'Oakland Athletics', 'home_abbr': 'OAK', 'home_score': 2, 'winning_name': 'Tampa Bay Rays', 'winning_abbr': 'TBR', 'losing_name': 'Oakland Athletics', 'losing_abbr': 'OAK'}, {'boxscore': 'PIT/PIT201707170', 'away_name': 'Milwaukee Brewers', 'away_abbr': 'MIL', 'away_score': 2, 'home_name': 'Pittsburgh Pirates', 'home_abbr': 'PIT', 'home_score': 4, 'winning_name': 'Pittsburgh Pirates', 'winning_abbr': 'PIT', 'losing_name': 'Milwaukee Brewers', 'losing_abbr': 'MIL'}, {'boxscore': 'SFN/SFN201707170', 'away_name': 'Cleveland Indians', 'away_abbr': 'CLE', 'away_score': 5, 'home_name': 'San Francisco Giants', 'home_abbr': 'SFG', 'home_score': 3, 'winning_name': 'Cleveland Indians', 'winning_abbr': 'CLE', 'losing_name': 'San Francisco Giants', 'losing_abbr': 'SFG'} ], '7-18-2017': [ {'boxscore': 'ANA/ANA201707180', 'away_name': 'Washington Nationals', 'away_abbr': 'WSN', 'away_score': 4, 'home_name': 'Los Angeles Angels', 'home_abbr': 'LAA', 'home_score': 3, 'winning_name': 'Washington Nationals', 'winning_abbr': 'WSN', 'losing_name': 'Los Angeles Angels', 'losing_abbr': 'LAA'}, {'boxscore': 'ATL/ATL201707180', 'away_name': 'Chicago Cubs', 'away_abbr': 'CHC', 'away_score': 5, 'home_name': 'Atlanta Braves', 'home_abbr': 'ATL', 'home_score': 1, 'winning_name': 'Chicago Cubs', 'winning_abbr': 'CHC', 'losing_name': 'Atlanta Braves', 'losing_abbr': 'ATL'}, {'boxscore': 'BAL/BAL201707180', 'away_name': 'Texas Rangers', 'away_abbr': 'TEX', 'away_score': 1, 'home_name': 'Baltimore Orioles', 'home_abbr': 'BAL', 'home_score': 12, 'winning_name': 'Baltimore Orioles', 'winning_abbr': 'BAL', 'losing_name': 'Texas Rangers', 'losing_abbr': 'TEX'}, {'boxscore': 'BOS/BOS201707180', 'away_name': 'Toronto Blue Jays', 'away_abbr': 'TOR', 'away_score': 4, 'home_name': 'Boston Red Sox', 'home_abbr': 'BOS', 'home_score': 5, 'winning_name': 'Boston Red Sox', 'winning_abbr': 'BOS', 'losing_name': 'Toronto Blue Jays', 'losing_abbr': 'TOR'}, {'boxscore': 'CHA/CHA201707180', 'away_name': 'Los Angeles Dodgers', 'away_abbr': 'LAD', 'away_score': 1, 'home_name': 'Chicago White Sox', 'home_abbr': 'CHW', 'home_score': 0, 'winning_name': 'Los Angeles Dodgers', 'winning_abbr': 'LAD', 'losing_name': 'Chicago White Sox', 'losing_abbr': 'CHW'}, {'boxscore': 'CIN/CIN201707180', 'away_name': 'Arizona Diamondbacks', 'away_abbr': 'ARI', 'away_score': 11, 'home_name': 'Cincinnati Reds', 'home_abbr': 'CIN', 'home_score': 2, 'winning_name': 'Arizona Diamondbacks', 'winning_abbr': 'ARI', 'losing_name': 'Cincinnati Reds', 'losing_abbr': 'CIN'}, {'boxscore': 'COL/COL201707180', 'away_name': 'San Diego Padres', 'away_abbr': 'SDP', 'away_score': 7, 'home_name': 'Colorado Rockies', 'home_abbr': 'COL', 'home_score': 9, 'winning_name': 'Colorado Rockies', 'winning_abbr': 'COL', 'losing_name': 'San Diego Padres', 'losing_abbr': 'SDP'}, {'boxscore': 'HOU/HOU201707180', 'away_name': 'Seattle Mariners', 'away_abbr': 'SEA', 'away_score': 2, 'home_name': 'Houston Astros', 'home_abbr': 'HOU', 'home_score': 6, 'winning_name': 'Houston Astros', 'winning_abbr': 'HOU', 'losing_name': 'Seattle Mariners', 'losing_abbr': 'SEA'}, {'boxscore': 'KCA/KCA201707180', 'away_name': 'Detroit Tigers', 'away_abbr': 'DET', 'away_score': 9, 'home_name': 'Kansas City Royals', 'home_abbr': 'KCR', 'home_score': 3, 'winning_name': 'Detroit Tigers', 'winning_abbr': 'DET', 'losing_name': 'Kansas City Royals', 'losing_abbr': 'KCR'}, {'boxscore': 'MIA/MIA201707180', 'away_name': 'Philadelphia Phillies', 'away_abbr': 'PHI', 'away_score': 5, 'home_name': 'Miami Marlins', 'home_abbr': 'MIA', 'home_score': 2, 'winning_name': 'Philadelphia Phillies', 'winning_abbr': 'PHI', 'losing_name': 'Miami Marlins', 'losing_abbr': 'MIA'}, {'boxscore': 'MIN/MIN201707180', 'away_name': 'New York Yankees', 'away_abbr': 'NYY', 'away_score': 6, 'home_name': 'Minnesota Twins', 'home_abbr': 'MIN', 'home_score': 3, 'winning_name': 'New York Yankees', 'winning_abbr': 'NYY', 'losing_name': 'Minnesota Twins', 'losing_abbr': 'MIN'}, {'boxscore': 'NYN/NYN201707180', 'away_name': 'St. Louis Cardinals', 'away_abbr': 'STL', 'away_score': 5, 'home_name': 'New York Mets', 'home_abbr': 'NYM', 'home_score': 0, 'winning_name': 'St. Louis Cardinals', 'winning_abbr': 'STL', 'losing_name': 'New York Mets', 'losing_abbr': 'NYM'}, {'boxscore': 'OAK/OAK201707180', 'away_name': 'Tampa Bay Rays', 'away_abbr': 'TBR', 'away_score': 4, 'home_name': 'Oakland Athletics', 'home_abbr': 'OAK', 'home_score': 3, 'winning_name': 'Tampa Bay Rays', 'winning_abbr': 'TBR', 'losing_name': 'Oakland Athletics', 'losing_abbr': 'OAK'}, {'boxscore': 'PIT/PIT201707180', 'away_name': 'Milwaukee Brewers', 'away_abbr': 'MIL', 'away_score': 3, 'home_name': 'Pittsburgh Pirates', 'home_abbr': 'PIT', 'home_score': 4, 'winning_name': 'Pittsburgh Pirates', 'winning_abbr': 'PIT', 'losing_name': 'Milwaukee Brewers', 'losing_abbr': 'MIL'}, {'boxscore': 'SFN/SFN201707180', 'away_name': 'Cleveland Indians', 'away_abbr': 'CLE', 'away_score': 1, 'home_name': 'San Francisco Giants', 'home_abbr': 'SFG', 'home_score': 2, 'winning_name': 'San Francisco Giants', 'winning_abbr': 'SFG', 'losing_name': 'Cleveland Indians', 'losing_abbr': 'CLE'} ] } result = Boxscores(datetime(2017, 7, 17), datetime(2017, 7, 18)).games assert result == expected
def test_boxscores_search_invalid_end(self, *args, **kwargs): result = Boxscores(datetime(2017, 7, 17), datetime(2017, 7, 16)).games assert result == self.expected
#!/usr/bin/python3 from sportsreference.nba.teams import Teams teams = Teams() for team in teams: print(team.name, team.abbreviation) print("-------------------------------") from datetime import datetime from sportsreference.mlb.boxscore import Boxscores games_today = Boxscores(datetime.today()) print(games_today.games) # Prints a dictionary of all matchups for today print("-------------------------------------") from sportsreference.mlb.roster import Roster dodgers = Roster('LAD') for player in dodgers.players: # Prints the name of all players who played for the Dodgers in the most # recent season. print(player.name)
def test_boxscores_search(self, *args, **kwargs): expected = { 'boxscores': [ {'home_name': 'Atlanta Braves', 'home_abbr': 'ATL', 'boxscore': 'ATL/ATL201707170', 'away_name': 'Chicago Cubs', 'away_abbr': 'CHC'}, {'home_name': 'Baltimore Orioles', 'home_abbr': 'BAL', 'boxscore': 'BAL/BAL201707170', 'away_name': 'Texas Rangers', 'away_abbr': 'TEX'}, {'home_name': 'Boston Red Sox', 'home_abbr': 'BOS', 'boxscore': 'BOS/BOS201707170', 'away_name': 'Toronto Blue Jays', 'away_abbr': 'TOR'}, {'home_name': 'Cincinnati Reds', 'home_abbr': 'CIN', 'boxscore': 'CIN/CIN201707170', 'away_name': 'Washington Nationals', 'away_abbr': 'WSN'}, {'home_name': 'Colorado Rockies', 'home_abbr': 'COL', 'boxscore': 'COL/COL201707170', 'away_name': 'San Diego Padres', 'away_abbr': 'SDP'}, {'home_name': 'Houston Astros', 'home_abbr': 'HOU', 'boxscore': 'HOU/HOU201707170', 'away_name': 'Seattle Mariners', 'away_abbr': 'SEA'}, {'home_name': 'Kansas City Royals', 'home_abbr': 'KCR', 'boxscore': 'KCA/KCA201707170', 'away_name': 'Detroit Tigers', 'away_abbr': 'DET'}, {'home_name': 'Miami Marlins', 'home_abbr': 'MIA', 'boxscore': 'MIA/MIA201707170', 'away_name': 'Philadelphia Phillies', 'away_abbr': 'PHI'}, {'home_name': 'Minnesota Twins', 'home_abbr': 'MIN', 'boxscore': 'MIN/MIN201707170', 'away_name': 'New York Yankees', 'away_abbr': 'NYY'}, {'home_name': 'New York Mets', 'home_abbr': 'NYM', 'boxscore': 'NYN/NYN201707170', 'away_name': 'St. Louis Cardinals', 'away_abbr': 'STL'}, {'home_name': 'Oakland Athletics', 'home_abbr': 'OAK', 'boxscore': 'OAK/OAK201707170', 'away_name': 'Tampa Bay Rays', 'away_abbr': 'TBR'}, {'home_name': 'Pittsburgh Pirates', 'home_abbr': 'PIT', 'boxscore': 'PIT/PIT201707170', 'away_name': 'Milwaukee Brewers', 'away_abbr': 'MIL'}, {'home_name': 'San Francisco Giants', 'home_abbr': 'SFG', 'boxscore': 'SFN/SFN201707170', 'away_name': 'Cleveland Indians', 'away_abbr': 'CLE'} ] } result = Boxscores(datetime(2017, 7, 17)).games assert result == expected
def test_boxscores_search_string_representation(self, *args, **kwargs): result = Boxscores(datetime(2017, 7, 17)) assert result.__repr__() == 'MLB games for 7-17-2017'