def scrape_date_range(from_date, to_date, if_scrape_shifts, data_format='csv', preseason=False, rescrape=False, docs_dir=False): """ Scrape games in given date range :param from_date: date you want to scrape from :param to_date: date you want to scrape to :param if_scrape_shifts: Boolean indicating whether to also scrape shifts :param data_format: format you want data in - csv or pandas (csv is default) :param preseason: Boolean indicating whether to include preseason games (default if False) This is may or may not work!!! I don't give a shit. :param rescrape: If you want to rescrape pages already scraped. Only applies if you supply a docs dir. (def. = None) :param docs_dir: Directory that either contains previously scraped docs or one that you want them to be deposited in after scraping. When True it'll refer to (or if needed create) such a repository in the home directory. When provided a string it'll try to use that. Here it must be a valid directory otheriwse it won't work (I won't make it for you). When False the files won't be saved. :return: Dictionary with DataFrames and errors or None """ # First check if the inputs are good shared.check_data_format(data_format) shared.check_valid_dates(from_date, to_date) # Check on the docs_dir and re_scrape shared.add_dir(docs_dir) shared.if_rescrape(rescrape) games = json_schedule.scrape_schedule(from_date, to_date, preseason) pbp_df, shifts_df = scrape_list_of_games(games, if_scrape_shifts) if data_format.lower() == 'csv': shared.to_csv(from_date + '--' + to_date, pbp_df, "nhl", "pbp") shared.to_csv(from_date + '--' + to_date, shifts_df, "nhl", "shifts") else: return {"pbp": pbp_df, "shifts": shifts_df} if if_scrape_shifts else {"pbp": pbp_df}
def scrape_schedule(from_date, to_date, data_format='pandas', rescrape=False, docs_dir=False): """ Scrape the games schedule in a given range. :param from_date: date you want to scrape from :param to_date: date you want to scrape to :param data_format: format you want data in - csv or pandas (pandas is default) :param rescrape: If you want to rescrape pages already scraped. Only applies if you supply a docs dir. (def. = None) :param docs_dir: Directory that either contains previously scraped docs or one that you want them to be deposited in after scraping. When True it'll refer to (or if needed create) such a repository in the home directory. When provided a string it'll try to use that. Here it must be a valid directory otheriwse it won't work (I won't make it for you). When False the files won't be saved. :return: DataFrame of None """ cols = ["game_id", "date", "venue", "home_team", "away_team", "start_time", "home_score", "away_score", "status"] # First check if the inputs are good shared.check_data_format(data_format) shared.check_valid_dates(from_date, to_date) # Check on the docs_dir and re_scrape shared.add_dir(docs_dir) shared.if_rescrape(rescrape) print("Scraping the schedule between {} and {}".format(from_date, to_date)) # live = True allows us to scrape games that aren't final sched = json_schedule.scrape_schedule(from_date, to_date, preseason=True, not_over=True) sched_df = pd.DataFrame(sched, columns=cols) if data_format.lower() == 'csv': shared.to_csv(from_date + '--' + to_date, sched_df, "nhl", "schedule") else: return sched_df
def scrape_date_range(from_date, to_date, data_format='csv', rescrape=False, docs_dir=None): """ Scrape games in given date range :param from_date: date you want to scrape from :param to_date: date you want to scrape to :param data_format: format you want data in - csv or pandas (csv is default) :param rescrape: If you want to rescrape pages already scraped. Only applies if you supply a docs dir. (def. = None) :param docs_dir: Directory that either contains previously scraped docs or one that you want them to be deposited in after scraping. (default is None) :return: Dictionary with DataFrames and errors or None """ # First check if the inputs are good shared.check_data_format(data_format) shared.check_valid_dates(from_date, to_date) # Check on the docs_dir and re_scrape shared.add_dir(docs_dir) shared.if_rescrape(rescrape) # Get dates and convert to just a list of game ids games = html_schedule.scrape_dates(from_date, to_date) game_ids = [game['game_id'] for game in games] # Scrape all PBP pbp_df = scrape_list_of_games(game_ids) # Merge in subtype pbp_df = pd.merge(pbp_df, pd.DataFrame(games, columns=['game_id', 'sub_type']), on="game_id", how="left") print_errors() if data_format.lower() == 'csv': shared.to_csv(from_date + '--' + to_date, pbp_df, None, "nwhl") else: return pbp_df
def test_check_valid_dates(): """ Test if given valid date range""" shared.check_valid_dates("2017-10-01", "2018-01-05") with pytest.raises(ValueError): shared.check_valid_dates("2017-12-01", "2017-11-30")