diff --git a/nba_data/models.py b/nba_data/models.py index 4262fe9..c765bc0 100644 --- a/nba_data/models.py +++ b/nba_data/models.py @@ -101,6 +101,41 @@ class PlayerAdvancedData(models.Model): def __str__(self): return self.player_name + +class PlayerPlayoffTotalsData(models.Model): + player_name = models.CharField(max_length=255, null=True) + age = models.IntegerField(null=True) + games = models.IntegerField(null=True) + games_started = models.IntegerField(null=True) + minutes_played = models.IntegerField(null=True) + field_goals = models.IntegerField(null=True) + field_attempts = models.IntegerField(null=True) + field_percent = models.DecimalField(max_digits=6, decimal_places=3, null=True) + three_fg = models.IntegerField(null=True) + three_attempts = models.IntegerField(null=True) + three_percent = models.DecimalField(max_digits=6, decimal_places=3, null=True) + two_fg = models.IntegerField(null=True) + two_attempts = models.IntegerField(null=True) + two_percent = models.DecimalField(max_digits=6, decimal_places=3, null=True) + effect_fg_percent = models.DecimalField(max_digits=6, decimal_places=3, null=True) + ft = models.IntegerField(null=True) + fta = models.IntegerField(null=True) + ft_percent = models.DecimalField(max_digits=6, decimal_places=3, null=True) + ORB= models.IntegerField(null=True) + DRB = models.IntegerField(null=True) + TRB = models.IntegerField(null=True) + AST = models.IntegerField(null=True) + STL = models.IntegerField(null=True) + BLK = models.IntegerField(null=True) + TOV = models.IntegerField(null=True) + PF = models.IntegerField(null=True) + PTS = models.IntegerField(null=True) + team = models.CharField(max_length=30, default='') + season = models.IntegerField(null=True) + + def __str__(self): + return self.player_name + diff --git a/nba_data/nba_scraper_playoffs_totals.py b/nba_data/nba_scraper_playoffs_totals.py new file mode 100644 index 0000000..c6030d5 --- /dev/null +++ b/nba_data/nba_scraper_playoffs_totals.py @@ -0,0 +1,111 @@ +import os +import sys +import django +import requests +from bs4 import BeautifulSoup +import csv +import json +import time + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nba_stats.settings') +django.setup() + +from nba_data.models import PlayerPlayoffTotalsData + +# Change CHO to CHA 2014 and prior. + +teams = ['HOU' ,'PHI', 'BOS', 'NYK', 'BRK', 'TOR', 'MEM', 'NOP', 'DAL', 'SAS', 'DEN', 'MIN', 'OKC', 'UTA', 'POR', 'MIL', 'CLE', 'CHI', 'IND', 'DET', 'SAC', 'PHO', 'GSW', 'LAC', 'LAL', 'MIA', 'ATL', 'WAS', 'ORL', 'CHO'] +team_abbreviations = ['ATL', 'BOS', 'BRK', 'CHI', 'CHO', 'CLE', 'DAL', 'DEN', 'DET', 'GSW', 'HOU', 'IND', 'LAC', 'LAL', 'MEM', 'MIA', 'MIL', 'MIN', 'NOP', 'NYK', 'OKC', 'ORL', 'PHI', 'PHO', 'POR', 'SAC', 'SAS', 'TOR', 'UTA', 'WAS'] + +season = input('What season? ') + +for team in teams: + url_make = 'https://www.basketball-reference.com/teams/' + team + '/' + season + '.html' + response = requests.get(url_make) + + soup = BeautifulSoup(response.text, "html.parser") + + table = soup.find("table", {"id": "playoffs_totals"}) + + if table is None: + print(f"Table not found for team {team} and season {season}.") + # sys.exit(1) + continue + + header_row = table.find("thead").find("tr") + rows = table.find("tbody").find_all("tr") + + + # Extract coloumn headers + headers = [col.text.strip() for col in header_row.find_all("th")][1:] + headers.extend(['team', 'season']) # Add 'team' and 'season' fields to headers + + # Extract rows as dictionaries + data = [] + for row in rows: + cols = row.find_all("td") + cols = [col.text.strip() for col in cols] + row_dict = {header: (None if col == "" else col) for header, col in zip(headers, cols)} + row_dict['team'] = team + row_dict['season'] = season + data.append(row_dict) + + + # locate current directory + script_dir = os.path.dirname(os.path.abspath(__file__)) + + # save as CSV file + csv_output_path = os.path.join(script_dir, '..', 'data', f'{team}_{season}_playoffs_totals_output.csv') + with open(csv_output_path, "w", newline="") as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=headers) + writer.writeheader() + for row in data: + writer.writerow(row) + + # Save as JSON + json_output_path = os.path.join(script_dir, '..', 'data', f'{team}_{season}_playoffs_totals_output.json') + with open(json_output_path, 'w') as jsonfile: + json.dump(data, jsonfile) + + # save data to database + for row in data: + player_data = PlayerPlayoffTotalsData( + player_name = row['Player'], + age = row['Age'], + games = row['G'], + games_started = row['GS'], + minutes_played = row['MP'], + field_goals = row['FG'], + field_attempts = row['FGA'], + field_percent = row['FG%'], + three_fg = row['3P'], + three_attempts = row['3PA'], + three_percent = row['3P%'], + two_fg = row['2P'], + two_attempts = row['2PA'], + two_percent = row['2P%'], + effect_fg_percent = row['eFG%'], + ft = row['FT'], + fta = row['FTA'], + ft_percent = row['FT%'], + ORB= row['ORB'], + DRB = row['DRB'], + TRB = row['TRB'], + AST = row['AST'], + STL = row['STL'], + BLK = row['BLK'], + TOV = row['TOV'], + PF = row['PF'], + PTS = row['PTS'], + team = row['team'], + season = row['season'], + ) + player_data.save() + +print('success') + + + \ No newline at end of file diff --git a/nba_data/views.py b/nba_data/views.py index 6fbf2ee..680787c 100644 --- a/nba_data/views.py +++ b/nba_data/views.py @@ -208,7 +208,7 @@ class TopPtsScatterPlotData(APIView): return Response(data) - +# change lines 223, 227, and 233 where the comments are to alter seasons returned. class TopPtsScatterPlotDataFast(APIView): def get(self, request): with connection.cursor() as cursor: @@ -220,14 +220,17 @@ class TopPtsScatterPlotDataFast(APIView): WHERE player_name IN (SELECT player_name FROM ( SELECT player_name, SUM("PTS") as total_pts FROM nba_data_playertotalsdata + -- WHERE season >= 2021 GROUP BY player_name ORDER BY total_pts DESC LIMIT 25) as top_25_players) + -- AND season >= 2021 GROUP BY player_name, season ), player_season_ws AS ( SELECT player_name, season, SUM("ws") as season_ws FROM nba_data_playeradvanceddata + -- WHERE season >= 2021 GROUP BY player_name, season ) SELECT player_season_pts.player_name, player_season_pts.season, player_season_pts.season_pts, player_season_ws.season_ws