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'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('', include('nba_data.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
from bs4 import BeautifulSoup
import csv
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? ')
season = input('What season? ')
@@ -26,7 +34,6 @@ rows = table.find("tbody").find_all("tr")
# Extract coloumn headers
headers = [col.text.strip() for col in header_row.find_all("th")][1:]
# extract rows as dictionaries
data = []
for row in rows:
@@ -34,14 +41,12 @@ for row in rows:
cols = [col.text.strip() for col in cols]
row_dict = dict(zip(headers, cols))
data.append(row_dict)
## locate current directory
# locate current directory
script_dir = os.path.dirname(os.path.abspath(__file__))
## CSV file
# CSV file
csv_output_path = os.path.join(script_dir, '..', 'data', 'output.csv')
with open(csv_output_path, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames =headers)
writer.writeheader()
@@ -49,34 +54,31 @@ with open(csv_output_path, "w", newline="") as csvfile:
writer.writerow(row)
# Save as JSON
json_output_path = os.path.join(script_dir, '..', 'data', 'output.json')
with open(json_output_path, 'w') as jsonfile:
json.dump(data, jsonfile)
# save data to database
for row in data:
player_data = PlayerData(
name=row['name'],
age=row['age'],
games=row['games'],
games_started=row['games_started'],
minutes_pg=row['minutes_pg'],
field_goals=row['field_goals'],
field_attempts=row['field_attempts'],
field_percent=row['field_percent'],
three_fg=row['three_fg'],
three_attempts=row['three_attempts'],
three_percent=row['three_percent'],
two_fg=row['two_fg'],
two_attempts=row['two_attempts'],
two_percent=row['two_percent'],
effect_fg_percent=row['effect_fg_percent'],
ft=row['ft'],
fta=row['fta'],
ft_percent=row['ft_percent'],
name=row['Player'],
age=row['Age'],
games=row['G'],
games_started=row['GS'],
minutes_pg=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'],
@@ -87,4 +89,4 @@ for row in data:
PF=row['PF'],
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')
]