mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-13 05:13:46 +00:00
9 files changed
+119
-17
No files matched your search
@@ -9,14 +9,15 @@
|
|||||||
|
|
||||||
|
|
||||||
2. Backend API
|
2. Backend API
|
||||||
-REST or GraphQL
|
|
||||||
- Flask (Python): Lightweight and easy to set up, suitable for small to medium projects.
|
|
||||||
- Django (Python): A more comprehensive framework that includes an ORM and admin panel out-of-the-box.
|
- Django (Python): A more comprehensive framework that includes an ORM and admin panel out-of-the-box.
|
||||||
- Express (Node.js): A fast and minimalistic web framework for Node.js.
|
- Django REST Framework
|
||||||
- Ruby on Rails (Ruby): A powerful and mature full-stack web framework.
|
|
||||||
|
|
||||||
|
|
||||||
3. Frontend
|
3. Backend DB
|
||||||
|
-Initially SQL lite, but it quickly met its limitations when trying to scrape data for 30 teams at once. It could not handle all the simulataneous connections.
|
||||||
|
- PostgreSQL is the new DB.
|
||||||
|
|
||||||
|
4. Frontend
|
||||||
- React/Redux
|
- React/Redux
|
||||||
- D3.js
|
- D3.js
|
||||||
- Chart.js
|
- Chart.js
|
||||||
@@ -24,7 +25,7 @@
|
|||||||
- Websockets or Server-Sent Events
|
- Websockets or Server-Sent Events
|
||||||
|
|
||||||
|
|
||||||
4. Deploy
|
1. Deploy
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from nba_data.models import PlayerData
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
# Provides a description for use with the custom django management command (python manage.py help).
|
||||||
|
help = 'Delete all player data'
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
# Delete all player data
|
||||||
|
PlayerData.objects.all().delete()
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS('Successfully deleted all players in DB'))
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from nba_data.models import PlayerData
|
||||||
|
from django.db import connection
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
# Provides a description for use with the custom django management command (python manage.py help).
|
||||||
|
help = 'Delete all player data and reset primary key sequence in DB'
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
# Delete all player data
|
||||||
|
PlayerData.objects.all().delete()
|
||||||
|
|
||||||
|
# Reset the PK sequence
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute('ALTER SEQUENCE nba_data_playerdata_id_seq RESTART WITH 1;')
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS('Successfully deleted all players in DB'))
|
||||||
+8
-4
@@ -32,7 +32,8 @@ for team in teams:
|
|||||||
|
|
||||||
if table is None:
|
if table is None:
|
||||||
print(f"Table not found for team {team} and season {season}.")
|
print(f"Table not found for team {team} and season {season}.")
|
||||||
sys.exit(1)
|
# sys.exit(1)
|
||||||
|
continue
|
||||||
|
|
||||||
header_row = table.find("thead").find("tr")
|
header_row = table.find("thead").find("tr")
|
||||||
rows = table.find("tbody").find_all("tr")
|
rows = table.find("tbody").find_all("tr")
|
||||||
@@ -57,15 +58,15 @@ for team in teams:
|
|||||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
# CSV file
|
# CSV file
|
||||||
csv_output_path = os.path.join(script_dir, '..', 'data', 'output.csv')
|
csv_output_path = os.path.join(script_dir, '..', 'data', f'{team}_{season}_output.csv')
|
||||||
with open(csv_output_path, "w", newline="") as csvfile:
|
with open(csv_output_path, "w", newline="") as csvfile:
|
||||||
writer = csv.DictWriter(csvfile, fieldnames =headers)
|
writer = csv.DictWriter(csvfile, fieldnames=headers)
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
for row in data:
|
for row in data:
|
||||||
writer.writerow(row)
|
writer.writerow(row)
|
||||||
|
|
||||||
# Save as JSON
|
# Save as JSON
|
||||||
json_output_path = os.path.join(script_dir, '..', 'data', 'output.json')
|
json_output_path = os.path.join(script_dir, '..', 'data', f'{team}_{season}_output.json')
|
||||||
with open(json_output_path, 'w') as jsonfile:
|
with open(json_output_path, 'w') as jsonfile:
|
||||||
json.dump(data, jsonfile)
|
json.dump(data, jsonfile)
|
||||||
|
|
||||||
@@ -103,3 +104,6 @@ for team in teams:
|
|||||||
season=row['season'],
|
season=row['season'],
|
||||||
)
|
)
|
||||||
player_data.save()
|
player_data.save()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# Serializers are defined that will convert model instances to JSON
|
||||||
|
from rest_framework import serializers
|
||||||
|
from .models import PlayerData
|
||||||
|
|
||||||
|
class PlayerDataSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = PlayerData
|
||||||
|
fields = '__all__'
|
||||||
+6
-2
@@ -1,6 +1,10 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
from .views import get_player_data
|
from .views import PlayerDataList, PlayerDataByTeamList, PlayerDataBySeasonList, PlayerDataByNameList, TopScorersbySeasonList
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/player_data/', get_player_data, name='player_data')
|
path('api/playerdata/', PlayerDataList.as_view(), name='playerdata_list'),
|
||||||
|
path('api/playerdata/team/<str:team>/', PlayerDataByTeamList.as_view(), name='playerdata_by_team'),
|
||||||
|
path('api/playerdata/season/<str:season>/', PlayerDataBySeasonList.as_view(), name='playerdata_by_season'),
|
||||||
|
path('api/playerdata/name/<str:name>/', PlayerDataByNameList.as_view(), name='playerdata_by_name'),
|
||||||
|
path('api/playerdata/topscorers/season/<str:season>/', TopScorersbySeasonList.as_view(), name='top_scorers_by_season'),
|
||||||
]
|
]
|
||||||
+45
-3
@@ -2,9 +2,51 @@ from django.shortcuts import render
|
|||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from .models import PlayerData
|
from .models import PlayerData
|
||||||
|
from rest_framework import generics
|
||||||
|
from .serializers import PlayerDataSerializer
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
def get_player_data(request):
|
# API view to list all PlayerData instances
|
||||||
data = serializers.serialize('json', PlayerData.objects.all())
|
class PlayerDataList(generics.ListCreateAPIView):
|
||||||
return JsonResponse(data, safe=False)
|
queryset = PlayerData.objects.all().order_by('id')
|
||||||
|
serializer_class = PlayerDataSerializer
|
||||||
|
# pagination_class = None
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch player data by team
|
||||||
|
class PlayerDataByTeamList(generics.ListAPIView):
|
||||||
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
team = self.kwargs['team']
|
||||||
|
return PlayerData.objects.filter(team=team)
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch player data by season
|
||||||
|
class PlayerDataBySeasonList(generics.ListAPIView):
|
||||||
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
season = self.kwargs['season']
|
||||||
|
return PlayerData.objects.filter(season=season)
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch player data by player name
|
||||||
|
class PlayerDataByNameList(generics.ListAPIView):
|
||||||
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
name = self.kwargs['name']
|
||||||
|
return PlayerData.objects.filter(name__icontains=name)
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch Top 20 players by PTS DESC for the season specified.
|
||||||
|
class TopScorersbySeasonList(generics.ListAPIView):
|
||||||
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
season = self.kwargs['season']
|
||||||
|
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
||||||
|
|
||||||
+15
-1
@@ -34,7 +34,7 @@ SECRET_KEY = SECRET_DJANGO
|
|||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
ALLOWED_HOSTS = []
|
ALLOWED_HOSTS = ['*']
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
@@ -43,6 +43,8 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'nba_data',
|
'nba_data',
|
||||||
|
'corsheaders',
|
||||||
|
'rest_framework',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
@@ -50,6 +52,7 @@ INSTALLED_APPS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
'corsheaders.middleware.CorsMiddleware',
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
@@ -135,3 +138,14 @@ STATIC_URL = 'static/'
|
|||||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
|
||||||
|
'PAGE_SIZE': 100, # Adjust this value according to the number of records you want to display per page
|
||||||
|
}
|
||||||
|
|
||||||
|
CORS_ALLOWED_ORIGINS = [
|
||||||
|
"http://localhost:5173", # Replace this with the origin of your frontend app
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
+1
-1
@@ -18,6 +18,6 @@ from django.contrib import admin
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include('nba_data.urls')),
|
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
path('', include('nba_data.urls')),
|
||||||
]
|
]
|
||||||
Reference in new issue
Block a user