def scrape_shifts(game_id, players, date): """ Scrape the Shift charts (or TOI tables) :param game_id: json game id :param players: dict of players with numbers and id's :param date: date of game :return: DataFrame with info or None if it fails """ shifts_df = None # Control for fact that shift json is only available from 2010 onwards if shared.get_season(date) >= 2010: shifts_df = json_shifts.scrape_game(game_id) if shifts_df is None or shifts_df.empty: shifts_df = html_shifts.scrape_game(game_id, players) if shifts_df is None or shifts_df.empty: shared.print_error("Unable to scrape shifts for game " + game_id) broken_shifts_games.extend([[game_id, date]]) return None shifts_df['Date'] = date return shifts_df
def test_scrape_shifts(): """ Tests scraping the json shifts. 1. We either want a pandas df or None. 2. Checks to see if the proper game scraped is the right amount of shifts 3. Checks if right columns are included """ scraped_shifts = json_shifts.scrape_game("2016020001") assert isinstance(scraped_shifts, pd.DataFrame) assert scraped_shifts.shape[0] == 850 shift_columns = ['Game_Id', 'Period', 'Team', 'Player', 'Player_Id', 'Start', 'End', 'Duration'] assert list(scraped_shifts.columns) == shift_columns