data collection fix

This commit is contained in:
nprasad2077 committed 2023-04-14 16:28:12 -05:00
1 parent c49af239eb
commit a752f9c840
8 files changed
+40 -31

No files matched your search

BIN
View File
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+2 -1
View File
@@ -15,8 +15,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('', include('nba_data.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
File renamed without changes.
+32 -30
View File
@@ -1,10 +1,18 @@
import os
import sys
import django
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import csv import csv
import json import json
import os
from .models import PlayerData
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nbaStats.settings')
django.setup()
from nba_data.models import PlayerData
team = input('What team? ') team = input('What team? ')
season = input('What season? ') season = input('What season? ')
@@ -26,7 +34,6 @@ rows = table.find("tbody").find_all("tr")
# Extract coloumn headers # Extract coloumn headers
headers = [col.text.strip() for col in header_row.find_all("th")][1:] headers = [col.text.strip() for col in header_row.find_all("th")][1:]
# extract rows as dictionaries # extract rows as dictionaries
data = [] data = []
for row in rows: for row in rows:
@@ -34,14 +41,12 @@ for row in rows:
cols = [col.text.strip() for col in cols] cols = [col.text.strip() for col in cols]
row_dict = dict(zip(headers, cols)) row_dict = dict(zip(headers, cols))
data.append(row_dict) data.append(row_dict)
## locate current directory # locate current directory
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', '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()
@@ -49,34 +54,31 @@ with open(csv_output_path, "w", newline="") as csvfile:
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', '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)
# save data to database # save data to database
for row in data: for row in data:
player_data = PlayerData( player_data = PlayerData(
name=row['name'], name=row['Player'],
age=row['age'], age=row['Age'],
games=row['games'], games=row['G'],
games_started=row['games_started'], games_started=row['GS'],
minutes_pg=row['minutes_pg'], minutes_pg=row['MP'],
field_goals=row['field_goals'], field_goals=row['FG'],
field_attempts=row['field_attempts'], field_attempts=row['FGA'],
field_percent=row['field_percent'], field_percent=row['FG%'],
three_fg=row['three_fg'], three_fg=row['3P'],
three_attempts=row['three_attempts'], three_attempts=row['3PA'],
three_percent=row['three_percent'], three_percent=row['3P%'],
two_fg=row['two_fg'], two_fg=row['2P'],
two_attempts=row['two_attempts'], two_attempts=row['2PA'],
two_percent=row['two_percent'], two_percent=row['2P%'],
effect_fg_percent=row['effect_fg_percent'], effect_fg_percent=row['eFG%'],
ft=row['ft'], ft=row['FT'],
fta=row['fta'], fta=row['FTA'],
ft_percent=row['ft_percent'], ft_percent=row['FT%'],
ORB=row['ORB'], ORB=row['ORB'],
DRB=row['DRB'], DRB=row['DRB'],
TRB=row['TRB'], TRB=row['TRB'],
@@ -87,4 +89,4 @@ for row in data:
PF=row['PF'], PF=row['PF'],
PTS=row['PTS'], PTS=row['PTS'],
) )
player_data.save() player_data.save()
+6
View File
@@ -0,0 +1,6 @@
from django.urls import path
from .views import get_player_data
urlpatterns = [
path('api/player_data/', get_player_data, name='player_data')
]