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 TestNBABoxscores: @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">84</td> <td class="right gamelink"> </td> </tr> <tr class="winner"> <td><a href="/teams/IND/2017.html">Indiana</a></td> <td class="right">105</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/DET/2017.html">Detroit</a></td> <td class="right">84</td> <td class="right gamelink"> <a href="/boxscores/201702040IND.html">Final</a> </td> </tr> <tr class="winner"> <td class="right">105</td> <td class="right"> </td> </tr> </tbody> </table>""") games = self.boxscores._extract_game_info([mock_html]) assert len(games) == 0
def pull_year_data(start, end): games = Boxscores(start, end).games coloumns = [ 'boxscore', 'away_name', 'away_abbr', 'away_score', 'home_name', 'home_abbr', 'home_score', 'winning_name', 'winning_abbr', 'losing_name', 'losing_abbr' ] df = pd.DataFrame(columns=coloumns) for key in games.items(): day = key[1] for i in range(len(day) - 1): df = df.append(day[i], ignore_index=True) return df
def latest_box_score(day_minus=0): day = date.today() - timedelta(days=day_minus) score = Boxscores(day) values = score.games.values() for v in values: if len(v) == 0: return latest_box_score(day_minus + 1) return {date.today(): score}
def games_for_the_day(date1): games = Boxscores(date1) date_string = str(date1.month) + '-' + str(date1.day) + '-' + str(date1.year) list1 = games.games[date_string] boxscores = [] for game in list1: index = game['boxscore'] boxscores.append(index) return boxscores
def save_game_ids(): nbaStartDate2018 = datetime(2018, 10, 17) boxscores = Boxscores(nbaStartDate2018, datetime.now()) boxscore_ids = [] for date, games in boxscores.games.items(): for game in games: boxscore_ids.append(game['boxscore']) with open('game_ids.txt', 'w') as file: file.write('\n'.join(boxscore_ids))
def games_for_the_day(date): games = Boxscores(date) date_string = str(date.month) + '-' + str(date.day) + '-' + str(date.year) list1 = games.games[date_string] print(list1) boxscores = [] print("Here are the games for " + str(date) + ":") print() for game in list1: away_abbr = game['away_abbr'] home_abbr = game['home_abbr'] print(game.keys()) break index = game['boxscore'] total_score = game['away_score'] + game['home_score'] prediction = round(both_teams_avg_game_points_previous_10(away_abbr, home_abbr, index), 2) print(away_abbr + " @ " + home_abbr) print("Predicted Score: " + str(prediction)) print("Actual Score: " + str(total_score)) print()
def test_boxscores_search_multiple_days(self, *args, **kwargs): expected = { '2-4-2017': [ {'boxscore': '201702040ATL', 'away_name': 'Orlando', 'away_abbr': 'ORL', 'away_score': 86, 'home_name': 'Atlanta', 'home_abbr': 'ATL', 'home_score': 113, 'winning_name': 'Atlanta', 'winning_abbr': 'ATL', 'losing_name': 'Orlando', 'losing_abbr': 'ORL'}, {'boxscore': '201702040IND', 'away_name': 'Detroit', 'away_abbr': 'DET', 'away_score': 84, 'home_name': 'Indiana', 'home_abbr': 'IND', 'home_score': 105, 'winning_name': 'Indiana', 'winning_abbr': 'IND', 'losing_name': 'Detroit', 'losing_abbr': 'DET'}, {'boxscore': '201702040MIA', 'away_name': 'Philadelphia', 'away_abbr': 'PHI', 'away_score': 102, 'home_name': 'Miami', 'home_abbr': 'MIA', 'home_score': 125, 'winning_name': 'Miami', 'winning_abbr': 'MIA', 'losing_name': 'Philadelphia', 'losing_abbr': 'PHI'}, {'boxscore': '201702040MIN', 'away_name': 'Memphis', 'away_abbr': 'MEM', 'away_score': 107, 'home_name': 'Minnesota', 'home_abbr': 'MIN', 'home_score': 99, 'winning_name': 'Memphis', 'winning_abbr': 'MEM', 'losing_name': 'Minnesota', 'losing_abbr': 'MIN'}, {'boxscore': '201702040NYK', 'away_name': 'Cleveland', 'away_abbr': 'CLE', 'away_score': 111, 'home_name': 'New York', 'home_abbr': 'NYK', 'home_score': 104, 'winning_name': 'Cleveland', 'winning_abbr': 'CLE', 'losing_name': 'New York', 'losing_abbr': 'NYK'}, {'boxscore': '201702040PHO', 'away_name': 'Milwaukee', 'away_abbr': 'MIL', 'away_score': 137, 'home_name': 'Phoenix', 'home_abbr': 'PHO', 'home_score': 112, 'winning_name': 'Milwaukee', 'winning_abbr': 'MIL', 'losing_name': 'Phoenix', 'losing_abbr': 'PHO'}, {'boxscore': '201702040SAC', 'away_name': 'Golden State', 'away_abbr': 'GSW', 'away_score': 106, 'home_name': 'Sacramento', 'home_abbr': 'SAC', 'home_score': 109, 'winning_name': 'Sacramento', 'winning_abbr': 'SAC', 'losing_name': 'Golden State', 'losing_abbr': 'GSW'}, {'boxscore': '201702040SAS', 'away_name': 'Denver', 'away_abbr': 'DEN', 'away_score': 97, 'home_name': 'San Antonio', 'home_abbr': 'SAS', 'home_score': 121, 'winning_name': 'San Antonio', 'winning_abbr': 'SAS', 'losing_name': 'Denver', 'losing_abbr': 'DEN'}, {'boxscore': '201702040UTA', 'away_name': 'Charlotte', 'away_abbr': 'CHO', 'away_score': 98, 'home_name': 'Utah', 'home_abbr': 'UTA', 'home_score': 105, 'winning_name': 'Utah', 'winning_abbr': 'UTA', 'losing_name': 'Charlotte', 'losing_abbr': 'CHO'}, {'boxscore': '201702040WAS', 'away_name': 'New Orleans', 'away_abbr': 'NOP', 'away_score': 91, 'home_name': 'Washington', 'home_abbr': 'WAS', 'home_score': 105, 'winning_name': 'Washington', 'winning_abbr': 'WAS', 'losing_name': 'New Orleans', 'losing_abbr': 'NOP'} ], '2-5-2017': [ {'boxscore': '201702050BOS', 'away_name': 'LA Clippers', 'away_abbr': 'LAC', 'away_score': 102, 'home_name': 'Boston', 'home_abbr': 'BOS', 'home_score': 107, 'winning_name': 'Boston', 'winning_abbr': 'BOS', 'losing_name': 'LA Clippers', 'losing_abbr': 'LAC'}, {'boxscore': '201702050BRK', 'away_name': 'Toronto', 'away_abbr': 'TOR', 'away_score': 103, 'home_name': 'Brooklyn', 'home_abbr': 'BRK', 'home_score': 95, 'winning_name': 'Toronto', 'winning_abbr': 'TOR', 'losing_name': 'Brooklyn', 'losing_abbr': 'BRK'}, {'boxscore': '201702050OKC', 'away_name': 'Portland', 'away_abbr': 'POR', 'away_score': 99, 'home_name': 'Oklahoma City', 'home_abbr': 'OKC', 'home_score': 105, 'winning_name': 'Oklahoma City', 'winning_abbr': 'OKC', 'losing_name': 'Portland', 'losing_abbr': 'POR'} ] } result = Boxscores(datetime(2017, 2, 4), datetime(2017, 2, 5)).games assert result == expected
def test_boxscores_search_invalid_end(self, *args, **kwargs): result = Boxscores(datetime(2017, 2, 4), datetime(2017, 2, 3)).games assert result == self.expected
def test_boxscores_search_string_representation(self, *args, **kwargs): result = Boxscores(datetime(2017, 2, 4)) assert result.__repr__() == 'NBA games for 2-4-2017'
def test_boxscores_search(self, *args, **kwargs): expected = { 'boxscores': [ { 'home_name': 'Atlanta', 'home_abbr': 'ATL', 'boxscore': '201702040ATL', 'away_name': 'Orlando', 'away_abbr': 'ORL' }, { 'home_name': 'Indiana', 'home_abbr': 'IND', 'boxscore': '201702040IND', 'away_name': 'Detroit', 'away_abbr': 'DET' }, { 'home_name': 'Miami', 'home_abbr': 'MIA', 'boxscore': '201702040MIA', 'away_name': 'Philadelphia', 'away_abbr': 'PHI' }, { 'home_name': 'Minnesota', 'home_abbr': 'MIN', 'boxscore': '201702040MIN', 'away_name': 'Memphis', 'away_abbr': 'MEM' }, { 'home_name': 'New York', 'home_abbr': 'NYK', 'boxscore': '201702040NYK', 'away_name': 'Cleveland', 'away_abbr': 'CLE' }, { 'home_name': 'Phoenix', 'home_abbr': 'PHO', 'boxscore': '201702040PHO', 'away_name': 'Milwaukee', 'away_abbr': 'MIL' }, { 'home_name': 'Sacramento', 'home_abbr': 'SAC', 'boxscore': '201702040SAC', 'away_name': 'Golden State', 'away_abbr': 'GSW' }, { 'home_name': 'San Antonio', 'home_abbr': 'SAS', 'boxscore': '201702040SAS', 'away_name': 'Denver', 'away_abbr': 'DEN' }, { 'home_name': 'Utah', 'home_abbr': 'UTA', 'boxscore': '201702040UTA', 'away_name': 'Charlotte', 'away_abbr': 'CHO' }, { 'home_name': 'Washington', 'home_abbr': 'WAS', 'boxscore': '201702040WAS', 'away_name': 'New Orleans', 'away_abbr': 'NOP' }, ] } result = Boxscores(datetime(2017, 2, 4)).games assert result == expected
from sportsreference.nba.boxscore import Boxscores import datetime if __name__ == "__main__": boxscores = Boxscores(datetime.date(2018, 10, 16), datetime.date(2019, 6, 13)) for _, day_of_games in boxscores.games.items(): for game in day_of_games: print(game['boxscore'])