def test_overtime_play_by_play_to_json_file(self): client.play_by_play( home_team=Team.PORTLAND_TRAIL_BLAZERS, day=22, month=10, year=2018, output_type=OutputType.JSON, output_file_path="./2018_10_22_POR_pbp.json", output_write_option=OutputWriteOption.WRITE, )
def test_BOS_2018_10_16_play_by_play_json_to_file(self): client.play_by_play( home_team=Team.BOSTON_CELTICS, day=16, month=10, year=2018, output_type=OutputType.JSON, output_file_path="./2018_10_16_BOS_pbp.json", output_write_option=OutputWriteOption.WRITE, )
def test_2003_10_29_TOR_box_scores(self): play_by_play( home_team=Team.TORONTO_RAPTORS, day=29, month=10, year=2003, output_type=OutputType.CSV, output_file_path=self.output_file_path, output_write_option=OutputWriteOption.WRITE, ) with open(self.output_file_path, "r") as output_file, \ open(self.expected_output_file_path, "r") as expected_output_file: self.assertEqual(output_file.readlines(), expected_output_file.readlines())
def test_get_box_scores_from_2003_json(self): play_by_play( home_team=Team.TORONTO_RAPTORS, day=29, month=10, year=2003, output_type=OutputType.JSON, output_file_path=self.output_file_path, output_write_option=OutputWriteOption.WRITE, ) with open(self.output_file_path, "r") as output_file, \ open(self.expected_output_file_path, "r") as expected_output_file: self.assertEqual( json.load(output_file), json.load(expected_output_file), )
def test_get_box_scores_from_2003_json(self): result = play_by_play(home_team=Team.TORONTO_RAPTORS, day=29, month=10, year=2003, output_type=OutputType.JSON, output_file_path="./foobar.json", output_write_option=OutputWriteOption.WRITE)
def test_BOS_2018_10_16_play_by_play(self): play_by_play = client.play_by_play( home_team=Team.BOSTON_CELTICS, day=16, month=10, year=2018, ) self.assertIsNotNone(play_by_play)
def pbp_for_schedule_record(path): return lambda match: play_by_play( match["home_team"], match["start_time"].day, # days are zero based match["start_time"].month, match["start_time"].year, OutputType.JSON, format_path(path, match), OutputWriteOption.WRITE)
def test_overtime_play_by_play(self): play_by_play = client.play_by_play( home_team=Team.PORTLAND_TRAIL_BLAZERS, day=22, month=10, year=2018, ) last_play = play_by_play[-1] self.assertIsNotNone(last_play) self.assertEqual(1, last_play["period"]) self.assertEqual(PeriodType.OVERTIME, last_play["period_type"])
def test_last_play_by_play_for_2018_10_29(self): result = play_by_play(home_team=Team.MILWAUKEE_BUCKS, day=27, month=10, year=2018) self.assertEqual( result[464], { "period": 4, "period_type": PeriodType.QUARTER, "relevant_team": Team.MILWAUKEE_BUCKS, "away_team": Team.ORLANDO_MAGIC, "home_team": Team.MILWAUKEE_BUCKS, "away_score": 91, "home_score": 113, "description": "Defensive rebound by T. Maker", "remaining_seconds_in_period": 2.0, })
def test_last_play_by_play_for_2019_01_01(self): result = play_by_play(home_team=Team.DENVER_NUGGETS, day=1, month=1, year=2019) self.assertEqual( result[463], { "period": 4, "period_type": PeriodType.QUARTER, "relevant_team": Team.DENVER_NUGGETS, "away_team": Team.NEW_YORK_KNICKS, "home_team": Team.DENVER_NUGGETS, "away_score": 108, "home_score": 115, "description": "Defensive rebound by M. Beasley", "remaining_seconds_in_period": 12.0, })
def test_last_play_by_play_for_overtime_game(self): result = play_by_play(home_team=Team.SACRAMENTO_KINGS, day=1, month=1, year=2019) self.assertEqual( result[507], { "period": 1, "period_type": PeriodType.OVERTIME, "relevant_team": Team.PORTLAND_TRAIL_BLAZERS, "away_team": Team.PORTLAND_TRAIL_BLAZERS, "home_team": Team.SACRAMENTO_KINGS, "away_score": 113, "home_score": 108, "description": "Defensive rebound by J. Nurkić", "remaining_seconds_in_period": 3.0, })
def test_first_play_by_play_for_2019_01_01(self): result = play_by_play(home_team=Team.DENVER_NUGGETS, day=1, month=1, year=2019) self.assertEqual( result[0], { "period": 1, "period_type": PeriodType.QUARTER, "relevant_team": Team.DENVER_NUGGETS, "away_team": Team.NEW_YORK_KNICKS, "home_team": Team.DENVER_NUGGETS, "away_score": 0, "home_score": 0, "description": "M. Plumlee misses 2-pt hook shot from 5 ft", "remaining_seconds_in_period": 693.0, })
def test_first_play_by_play_for_2018_10_29(self): result = play_by_play(home_team=Team.MILWAUKEE_BUCKS, day=27, month=10, year=2018) self.assertEqual( result[0], { "period": 1, "period_type": PeriodType.QUARTER, "relevant_team": Team.ORLANDO_MAGIC, "away_team": Team.ORLANDO_MAGIC, "home_team": Team.MILWAUKEE_BUCKS, "away_score": 0, "home_score": 0, "description": "N. Vučević misses 2-pt hook shot from 3 ft", "remaining_seconds_in_period": 703.0, })
def process_play_by_play(*, year: int, output_dir: str) -> None: sched = client.season_schedule(season_end_year=year) failed_game_descriptions = [] for game in sched: home_team = game['home_team'].name start_time = _convert_time_from_utc(game['start_time']) start_time_str = start_time.strftime('%Y-%m-%d') game_description = f"[{start_time_str}]: Pulling data for home team {home_team}" print(game_description) try: play_by_play = client.play_by_play( home_team=game['home_team'], year=start_time.year, month=start_time.month, day=start_time.day, ) except Exception: print("FAILURE! Skipping game") failed_game_descriptions.append(game_description) continue for play in play_by_play: play['date'] = start_time_str play['period_type'] = play['period_type'].name play['away_team'] = play['away_team'].name play['home_team'] = play['home_team'].name output_filename = f"game_date={start_time_str}_hometeam={home_team}.tsv" pd.DataFrame.from_records(play_by_play).to_csv( os.path.join(output_dir, output_filename), header=True, index=False, sep='\t', ) # show failures if failed_game_descriptions: print(f"****{len(failed_game_descriptions)} games failed to parse****") for description in failed_game_descriptions: print(description)
def test_get_play_by_play_single_digit_month_and_day(self): result = play_by_play(home_team=Team.DENVER_NUGGETS, day=1, month=1, year=2019) self.assertIsNotNone(result)
def test_get_play_by_play(self): result = play_by_play(home_team=Team.MILWAUKEE_BUCKS, day=27, month=10, year=2018) self.assertIsNotNone(result)
def test_total_play_by_play_length_for_2018_10_29(self): result = play_by_play(home_team=Team.MILWAUKEE_BUCKS, day=27, month=10, year=2018) self.assertEqual(len(result), 465)
def test_non_unicode_matches(self): result = play_by_play(home_team=Team.GOLDEN_STATE_WARRIORS, day=16, month=10, year=2018) self.assertIsNotNone(result)
def test_total_play_by_play_length_for_single_digit_month_and_day(self): result = play_by_play(home_team=Team.DENVER_NUGGETS, day=1, month=1, year=2019) self.assertEqual(len(result), 464)
from basketball_reference_web_scraper import client from basketball_reference_web_scraper.data import OutputType, Team print(client.season_schedule(season_end_year=2021)) client.players_advanced_season_totals( season_end_year=2018, output_type=OutputType.CSV, output_file_path="./2017_2018_player_season_totals.csv" ) client.play_by_play( home_team=Team.BOSTON_CELTICS, year=2018, month=10, day=16, output_type=OutputType.CSV, output_file_path="./2018_10_06_BOS_PBP.csv" ) client.regular_season_player_box_scores( player_identifier="westbru01", season_end_year=2018, output_type=OutputType.CSV, output_file_path="./2017_2018_russell_westbrook_regular_season_box_scores.csv" )