def getStatsForTeam(team, startDate, endDate, season='2019-20'): time.sleep(1) # Uses NBA_API to access the dictionary holding basic stats for every team per 100 possessions generalTeamInfo = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=teams[team], per_mode_detailed='Per100Possessions', date_from_nullable=startDate, date_to_nullable=endDate, season=season, headers=customHeaders, timeout=120) generalTeamDict = generalTeamInfo.get_normalized_dict() generalTeamDashboard = generalTeamDict['OverallTeamDashboard'][0] # Returns Win PCT, Rebounds, Turnovers, and Plus Minus winPercentage = generalTeamDashboard['W_PCT'] rebounds = generalTeamDashboard['REB'] turnovers = generalTeamDashboard['TOV'] plusMinus = generalTeamDashboard['PLUS_MINUS'] # Uses NBA_API to access the dictionary holding advanced stats for every team advancedTeamInfo = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=teams[team], measure_type_detailed_defense='Advanced', date_from_nullable=startDate, date_to_nullable=endDate, season=season, headers=customHeaders, timeout=120) advancedTeamDict = advancedTeamInfo.get_normalized_dict() advancedTeamDashboard = advancedTeamDict['OverallTeamDashboard'][0] # Variables holding OFF Rating, DEF Rating, and TS% offensiveRating = advancedTeamDashboard['OFF_RATING'] defensiveRating = advancedTeamDashboard['DEF_RATING'] trueShootingPercentage = advancedTeamDashboard['TS_PCT'] # Puts all the stats for specified team into a dictionary allStats = { 'W_PCT': winPercentage, 'REB': rebounds, 'TOV': turnovers, 'PLUS_MINUS': plusMinus, 'OFF_RATING': offensiveRating, 'DEF_RATING': defensiveRating, 'TS_PCT': trueShootingPercentage, } return allStats
def pobieranie_statystyk_dla_druzyny(druzyna, data_startowa, data_koncowa, sezon='2019-20'): time.sleep(1) # Pobieranie bazowych statystyk dla 100 posiadań. bazowe_statystyki_druzyny = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=druzyny[druzyna], per_mode_detailed='Per100Possessions', date_from_nullable=data_startowa, date_to_nullable=data_koncowa, season=sezon, headers=headers, timeout=120) bazowe_statystyki_druzyny_slownik = bazowe_statystyki_druzyny.get_normalized_dict() bazowe_statystyki_druzyny_tablica = bazowe_statystyki_druzyny_slownik[ 'OverallTeamDashboard'][0] # Rozbicie tablicy z bazowymi statystykami na pojedyńcze statystyki. procent_wygranych = bazowe_statystyki_druzyny_tablica['W_PCT'] zbiorki = bazowe_statystyki_druzyny_tablica['REB'] straty = bazowe_statystyki_druzyny_tablica['TOV'] plus_minus = bazowe_statystyki_druzyny_tablica['PLUS_MINUS'] # Pobieranie zaawansowanych statystyk dla 100 posiadań. zaawansowane_statystyki_druzyny = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=druzyny[druzyna], measure_type_detailed_defense='Advanced', date_from_nullable=data_startowa, date_to_nullable=data_koncowa, season=sezon, headers=headers, timeout=120) zaawansowane_statystyki_druzyny_slownik = zaawansowane_statystyki_druzyny.get_normalized_dict() zaawansowane_statystyki_druzyny_tablica = zaawansowane_statystyki_druzyny_slownik[ 'OverallTeamDashboard'][0] # Rozbicie tablicy z zaawansowanymi statystykami na pojedyńcze statystyki. rating_ofensywny = zaawansowane_statystyki_druzyny_tablica['OFF_RATING'] rating_defensywny = zaawansowane_statystyki_druzyny_tablica['DEF_RATING'] wskaznik_tsp = zaawansowane_statystyki_druzyny_tablica['TS_PCT'] # wrzucenie bazowych i zaawansowanych statystyk do jednego słownika wszystkie_statystyki = { 'W_PCT': procent_wygranych, 'REB': zbiorki, 'TOV': straty, 'PLUS_MINUS': plus_minus, 'OFF_RATING': rating_ofensywny, 'DEF_RATING': rating_defensywny, 'TS_PCT': wskaznik_tsp, } return wszystkie_statystyki
def __init__(self, team_id, date, statPrefixString, season): self.team_info = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=team_id, season=season, season_type_all_star="Regular Season", per_mode_detailed='PerMinute', date_to_nullable=date).get_dict() self.team_id = team_id self.statPrefixString = statPrefixString self.statHeaderArray = self.team_info["resultSets"][0]["headers"] self.statNumberArray = self.team_info["resultSets"][0]["rowSet"][0] self.indexesToProduce = [27, 10, 13, 16, 17, 18, 20, 21, 22, 23, 28]
async def team_stats(self, ctx, season_type, season_year, *, object_search): try: if season_type == ("RegularSeason"): season_type = SeasonTypeAllStar.regular elif season_type == ("PreSeason"): season_type = SeasonTypeAllStar.preseason elif season_type == ("Playoffs"): season_type == SeasonTypeAllStar.playoffs # Had issues with using the user input itself as a variable so had to use a conditional statement approach. object_row = (sheet.find(object_search).row) object_column = (sheet.find(object_search).col) object_id = sheet.cell(object_row, object_column - 1).value object_id = str(object_id) stats_team = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( last_n_games=0, measure_type_detailed_defense='Base', month=0, opponent_team_id=0, pace_adjust='N', per_mode_detailed='PerGame', period=0, plus_minus='N', rank='N', season=season_year, season_type_all_star=season_type, team_id=object_id) df_stats_team = stats_team.get_data_frames() team_ppg = df_stats_team[0].iat[0, 27] team_fgp = df_stats_team[0].iat[0, 10] team_tpp = df_stats_team[0].iat[0, 13] team_ftp = df_stats_team[0].iat[0, 16] team_rpg = df_stats_team[0].iat[0, 19] team_apg = df_stats_team[0].iat[0, 20] team_spg = df_stats_team[0].iat[0, 22] team_bpg = df_stats_team[0].iat[0, 23] team_tpg = df_stats_team[0].iat[0, 21] stats_team_embed = discord.Embed( title=(f'Team Stats for the {object_search}'), colour=discord.Colour(0x0e1cf5)) stats_team_embed.set_footer(text='gimme help | RIP Kobe') stats_team_embed.set_image( url= 'https://cdn.vox-cdn.com/thumbor/v1jR5XEgcDrnwTq_uSZt4ApiIqg=/1400x1400/filters:format(jpeg)/cdn.vox-cdn.com/uploads/chorus_asset/file/20025173/nba_covid_3_ringer_illo.jpg' ) stats_team_embed.set_author( name= f'{season_type} stats for the {season_year} {object_search}') stats_team_embed.add_field(name='PPG', value=team_ppg) stats_team_embed.add_field(name='FG%', value=f'{round((team_fgp * 100), 1)}%') stats_team_embed.add_field(name='3P%', value=f'{round((team_tpp * 100), 1)}%') stats_team_embed.add_field(name='FT%', value=f'{round((team_ftp * 100), 1)}%') stats_team_embed.add_field(name='RPG', value=team_rpg) stats_team_embed.add_field(name='APG', value=team_apg) stats_team_embed.add_field(name='SPG', value=team_spg) stats_team_embed.add_field(name='BPG', value=team_bpg) stats_team_embed.add_field(name='TPG', value=team_tpg) await ctx.send(embed=stats_team_embed) # With discord.py's implementation of classes, creating a function that would cut down the amount of code proved to be oddly difficult and not worth the time. except (UnboundLocalError, gspread.exceptions.CellNotFound): await ctx.send(""" I think you spelled it wrong hombre, make sure you're using this format: `gimme team_stats RegularSeason 2013-14 Toronto Raptors`. Also, make sure you're not using shortened versions of names like `Raptors`, `Toronto`, or `TOR`. """)
def retrieve_all_teams_stats(date_from, date_to, season, save_location): print('Retrieving stats for the ' + season + ' season . . .') teams_stats = [] # Loop through the teams and retrieve their stats for team in teams.get_teams(): team_id = team['id'] team_name = team['full_name'] print(' Retrieving stats for the ' + team_name + ' . . .') # The stats from the 'Traditional' section general_team_info = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=team_id, per_mode_detailed='PerGame', season_type_all_star='Regular Season', date_from_nullable=date_from, date_to_nullable=date_to, season=season, headers=custom_headers, timeout=120) general_team_dict = general_team_info.get_normalized_dict() general_team_dashboard = general_team_dict['OverallTeamDashboard'][0] games_played = general_team_dashboard['GP'] points = general_team_dashboard['PTS'] wins = general_team_dashboard['W'] losses = general_team_dashboard['L'] win_percentage = general_team_dashboard['W_PCT'] field_goal_percentage = general_team_dashboard['FG_PCT'] three_point_percentage = general_team_dashboard['FG3_PCT'] free_throw_percentage = general_team_dashboard['FT_PCT'] offensive_rebounds = general_team_dashboard['OREB'] defensive_rebounds = general_team_dashboard['DREB'] rebounds = general_team_dashboard['REB'] assists = general_team_dashboard['AST'] turnovers = general_team_dashboard['TOV'] plus_minus = general_team_dashboard['PLUS_MINUS'] # The stats from the 'Advanced' section advanced_team_info = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=team_id, measure_type_detailed_defense='Advanced', season_type_all_star='Regular Season', date_from_nullable=date_from, date_to_nullable=date_to, season=season, headers=custom_headers, timeout=120) advanced_team_dict = advanced_team_info.get_normalized_dict() advanced_team_dashboard = advanced_team_dict['OverallTeamDashboard'][0] offensive_rating = advanced_team_dashboard['OFF_RATING'] defensive_rating = advanced_team_dashboard['DEF_RATING'] net_rating = advanced_team_dashboard['NET_RATING'] assist_percentage = advanced_team_dashboard['AST_PCT'] assist_to_turnover_ratio = advanced_team_dashboard['AST_TO'] assist_ratio = advanced_team_dashboard['AST_RATIO'] offensive_rebound_percentage = advanced_team_dashboard['OREB_PCT'] defensive_rebound_percentage = advanced_team_dashboard['DREB_PCT'] rebound_percentage = advanced_team_dashboard['REB_PCT'] turnover_percentage = advanced_team_dashboard['TM_TOV_PCT'] effective_field_goal_percentage = advanced_team_dashboard['EFG_PCT'] true_shooting_percentage = advanced_team_dashboard['TS_PCT'] player_impact_estimate = advanced_team_dashboard['PIE'] # Temporary dictionary to hold the stats # Put the percentage columns as percentages values teams_stats_temp = { 'ID': team_id, 'NAME': team_name, 'GP': games_played, 'PTS': points, 'W': wins, 'L': losses, 'WIN%': win_percentage * 100, 'FG%': field_goal_percentage * 100, '3P%': three_point_percentage * 100, 'FT%': free_throw_percentage * 100, 'OREB': offensive_rebounds, 'DREB': defensive_rebounds, 'REB': rebounds, 'AST': assists, 'TOV': turnovers, '+/-': plus_minus, 'OFF RATING': offensive_rating, 'DEF RATING': defensive_rating, 'NET RATING': net_rating, 'AST%': assist_percentage * 100, 'AST/TO': assist_to_turnover_ratio, 'AST RATIO': assist_ratio, 'OREB%': offensive_rebound_percentage * 100, 'DREB%': defensive_rebound_percentage * 100, 'REB%': rebound_percentage * 100, 'TOV%': turnover_percentage * 100, 'EFG%': effective_field_goal_percentage * 100, 'TS%': true_shooting_percentage * 100, 'PIE': player_impact_estimate * 100 } teams_stats.append(teams_stats_temp) teams_stats_df = pd.DataFrame.from_dict(teams_stats) print('Number of records retrieved: {}'.format(len(teams_stats_df))) # Sort by the team names teams_stats_df_sorted = teams_stats_df.sort_values('NAME', ascending=True) print(teams_stats_df_sorted.to_string(index=False)) # Save the data as CSV teams_stats_df_sorted.to_csv('data/' + save_location + 'teams_stats_' + season + '.csv', index=False) print('CSV Saved for the ' + season + ' season!')
def retrieve_specific_team_stats(team_id, team_name, date_from, date_to, season): print('Retrieving stats for the ' + team_name + ' . . .') team_stats = [] # The stats from the 'Traditional' section general_team_info = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=team_id, per_mode_detailed='PerGame', season_type_all_star='Regular Season', date_from_nullable=date_from, date_to_nullable=date_to, season=season, headers=custom_headers, timeout=120) general_team_dict = general_team_info.get_normalized_dict() general_team_dashboard = general_team_dict['OverallTeamDashboard'][0] field_goal_percentage = general_team_dashboard['FG_PCT'] three_point_percentage = general_team_dashboard['FG3_PCT'] free_throw_percentage = general_team_dashboard['FT_PCT'] # The stats from the 'Advanced' section advanced_team_info = teamdashboardbygeneralsplits.TeamDashboardByGeneralSplits( team_id=team_id, measure_type_detailed_defense='Advanced', season_type_all_star='Regular Season', date_from_nullable=date_from, date_to_nullable=date_to, season=season, headers=custom_headers, timeout=120) advanced_team_dict = advanced_team_info.get_normalized_dict() advanced_team_dashboard = advanced_team_dict['OverallTeamDashboard'][0] assist_percentage = advanced_team_dashboard['AST_PCT'] offensive_rebound_percentage = advanced_team_dashboard['OREB_PCT'] defensive_rebound_percentage = advanced_team_dashboard['DREB_PCT'] rebound_percentage = advanced_team_dashboard['REB_PCT'] turnover_percentage = advanced_team_dashboard['TM_TOV_PCT'] effective_field_goal_percentage = advanced_team_dashboard['EFG_PCT'] true_shooting_percentage = advanced_team_dashboard['TS_PCT'] # Dictionary to hold the stats # Put the percentage columns as percentages values team_stats_dict = { 'ID': team_id, 'NAME': team_name, 'FG%': field_goal_percentage * 100, '3P%': three_point_percentage * 100, 'FT%': free_throw_percentage * 100, 'AST%': assist_percentage * 100, 'OREB%': offensive_rebound_percentage * 100, 'DREB%': defensive_rebound_percentage * 100, 'REB%': rebound_percentage * 100, 'TOV%': turnover_percentage * 100, 'EFG%': effective_field_goal_percentage * 100, 'TS%': true_shooting_percentage * 100 } team_stats.append(team_stats_dict) team_stats_df = pd.DataFrame.from_dict(team_stats) print('Done!') return (team_stats_df)