def scrape_and_clean_game(date, away, home, year): # scrape game df = get_pbp(date, away, home) # playoffs? if (int(date[5:7]) >= 5) and (int(date[8:10]) >= 22): # playoff start date; TODO: change to auto detect playoffs = "Playoffs" else: playoffs = "Regular" # change colnames and add new columns df.columns = [ 'quarter', 'time_remaining', 'away_action', 'home_action', 'away_score', 'home_score' ] df.insert(0, 'home_team', [home for i in range(len(df.index))]) df.insert(0, 'away_team', [away for i in range(len(df.index))]) df.insert(0, 'playoffs', [playoffs for i in range(len(df.index))]) df.insert(0, 'month', [date[5:7] for i in range(len(df.index))]) season = str(int(year) - 1) + "-" + year df.insert(0, 'season', [season for i in range(len(df.index))]) df.insert(0, 'date', [date for i in range(len(df.index))]) gameId = (date + away + home).replace('-', '') df.insert(0, 'gameId', [gameId for i in range(len(df.index))]) return df
def scrape_and_clean_game(date, away, home, playoffs, year): # scrape game df = get_pbp(date, away, home) # change colnames and add new columns df.columns = [ 'quarter', 'time_remaining', 'away_action', 'home_action', 'away_score', 'home_score' ] df.insert(0, 'home_team', [home for i in range(len(df.index))]) df.insert(0, 'away_team', [away for i in range(len(df.index))]) df.insert(0, 'playoffs', [playoffs for i in range(len(df.index))]) df.insert(0, 'month', [date[5:7] for i in range(len(df.index))]) season = str(int(year) - 1) + "-" + year df.insert(0, 'season', [season for i in range(len(df.index))]) df.insert(0, 'date', [date for i in range(len(df.index))]) gameId = (date + away + home).replace('-', '') df.insert(0, 'gameId', [gameId for i in range(len(df.index))]) return df
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Jan 30 14:43:11 2020 @author: Nillan """ from basketball_reference_scraper.shot_charts import get_shot_chart from basketball_reference_scraper.pbp import get_pbp d = get_shot_chart('2019-12-28', 'TOR', 'BOS') pbp = get_pbp('2019-12-28', 'TOR', 'BOS') # when joel embiid is making more threes, how does bill simmons do #by qtr?
def test_pbp(self): df = get_pbp('2020-01-06', 'DEN', 'ATL') expected_columns = ['QUARTER', 'TIME_REMAINING', 'DENVER_ACTION', 'ATLANTA_ACTION', 'DENVER_SCORE', 'ATLANTA_SCORE'] self.assertListEqual(list(df.columns), expected_columns)
s = get_schedule(2018, playoffs=False) print(s) s = get_standings(date='2020-01-06') print(s) from basketball_reference_scraper.box_scores import get_box_scores s = get_box_scores('2020-01-13', 'CHI', 'BOS', period='GAME', stat_type='BASIC') print(s) from basketball_reference_scraper.pbp import get_pbp s = get_pbp('2020-01-13', 'CHI', 'BOS') print(s) from basketball_reference_scraper.shot_charts import get_shot_chart s = get_shot_chart('2020-01-13', 'CHI', 'BOS') print(s) from basketball_reference_scraper.injury_report import get_injury_report s = get_injury_report() print(s)
def test_pbp(self): df = get_pbp('2020-01-06', 'DEN', 'ATL') expected_columns = ['Clock', 'Denver', 'Atlanta', 'Denver Score', 'Atlanta Score'] self.assertListEqual(list(df.columns), expected_columns)
def main(): """Generate a summary email for last night's NBA games""" # loop through last night's games yest_month = (datetime.now() - timedelta(1)).strftime('%B').lower() schedule = get_season_schedule(yest_month) # save df to create each plot plot_dfs = [] plot_titles = [] for i, row in schedule.iterrows(): if row.Date2 == (datetime.now() - timedelta(1)).strftime('%Y-%m-%d'): date = row.Date2 away = row.Visitor home = row.Home pbp = get_pbp(date, away, home).fillna('no action') dfs_box = get_box(date, away, home) # returns list of [away_df, home_df] df_away_raw = dfs_box[0] df_home_raw = dfs_box[1] # cut 'out' players and change columns to numeric trimmed_dfs = cols_to_numeric( cut_players_out(df_away_raw, df_home_raw, away, home)) df_away = trimmed_dfs[0] df_home = trimmed_dfs[1] # print team scores and top performers print_team_scoring_and_top_performers(df_away, df_home, away, home) # free throws print_free_throws(df_away, df_home, away, home) # 3 pointers print_three_pointers(df_away, df_home, away, home) # shots at rim shots_at_rim = count_shots_at_rim(pbp, away, home) write_to_txt([shots_at_rim[8], shots_at_rim[9]]) # rebounds print_rebounding(df_away, df_home, away, home) # possessions and OT print_possessions_and_ot(pbp) # add link to highlights url = "https://www.youtube.com/playlist?list=PLlVlyGVtvuVkIjURb-twQc1lsE4rT1wfJ" write_to_txt([f"Game highlights here: {url} \n"]) # save plot df for current game plot_dfs.append(plot_score_vs_time(pbp)) plot_titles.append(f"{away} v {home}") print("Sleeping for 3 seconds... \n") print("\n") time.sleep(3) # space between games in the text file write_to_txt(["\n"]) """ # plot score vs. time for each game # fig, axs = plt.subplots(x,y) [2,2] for 4, [3,2] for 6, [3,3] for 9 etc. num_games = len(plot_dfs) print(f"num_games: {num_games}") x, y = 0, 0 x_len = int((num_games - 0.01)**0.5) + 1 y_len = int((num_games - x_len) / (x_len + 0.01)) + 2 print(f"x_len: {x_len}") print(f"y_len: {y_len}") fig, axs = plt.subplots(x_len, y_len) for df, title in zip(plot_dfs, plot_titles): x1 = df.iloc[:,0] x2 = df.iloc[:,1] _y = df.index axs[x,y].plot(x1, _y) axs[x,y].plot(x2, _y) axs[x,y].set_ylabel("Points") axs[x,y].set_title(title) axs[x,y].set_xlabel("Minutes played") axs[x,y].set_xticks([0, 12, 24, 36, 48]) axs[x,y].grid('on') if x == x_len - 1: x = 0 y += 1 else: x += 1 fig.tight_layout() plt.savefig(f'game_flow_charts/gameflows_{date}.png') print("Game flows saved") """ print("Finished")