started loading stats from gamechanger
This commit is contained in:
8
gamechanger/templates/gamechanger/stats.html
Normal file
8
gamechanger/templates/gamechanger/stats.html
Normal file
@@ -0,0 +1,8 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
{% for id, stat_row in stats.items %}
|
||||
<p>
|
||||
{{ id }}: {{ stat_row }}
|
||||
</p>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
@@ -1,10 +1,17 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import AccountFormView, PreferencesFormView, lineup_submit, roster_import
|
||||
from .views import (
|
||||
AccountFormView,
|
||||
PreferencesFormView,
|
||||
lineup_submit,
|
||||
roster_import,
|
||||
stats,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("account/", AccountFormView.as_view(), name="gamechanger_account"),
|
||||
path("preferences/", PreferencesFormView.as_view(), name="gamechanger_preferences"),
|
||||
path("roster/import", roster_import, name="gamechanger_import_roster"),
|
||||
path("lineup/submit", lineup_submit, name="gamechanger_lineup_submit"),
|
||||
path("stats", stats, name="gamechanger_stats"),
|
||||
]
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import csv
|
||||
import json
|
||||
import re
|
||||
|
||||
import requests
|
||||
import vcr
|
||||
|
||||
url = "https://gc.com/t/{season_id}/{team_id}/{page}"
|
||||
|
||||
|
||||
@vcr.use_cassette(
|
||||
"gamechanger/fixtures/authenticated_session.yaml", record_mode="new_episodes"
|
||||
)
|
||||
def get_authenticated_session(request):
|
||||
gc_username = request.user.gamechanger_account.user
|
||||
gc_password = request.user.gamechanger_account.password
|
||||
@@ -66,5 +71,67 @@ def scrape_page(season_id, team_id, page):
|
||||
return json.loads(m)
|
||||
|
||||
|
||||
@vcr.use_cassette("gamechanger/fixtures/stats.yaml", record_mode="new_episodes")
|
||||
def stats(request):
|
||||
authenticated_session = get_authenticated_session(request)
|
||||
season_id = request.user.gamechanger_preferences.season_id
|
||||
team_id = request.user.gamechanger_preferences.team_id
|
||||
page = "stats/batting/Qualified/standard/csv"
|
||||
r = authenticated_session.get(
|
||||
url.format(season_id=season_id, team_id=team_id, page=page)
|
||||
)
|
||||
|
||||
with vcr.use_cassette(
|
||||
"gamechanger/fixtures/roster.yaml", record_mode="new_episodes"
|
||||
):
|
||||
roster = scrape_page(season_id, team_id, "roster")
|
||||
id_lookup = {
|
||||
(p.get("fname"), p.get("lname")): p.get("player_id")
|
||||
for p in roster["roster"]
|
||||
}
|
||||
|
||||
decoded_content = r.content.decode("utf-8")
|
||||
|
||||
cr = csv.reader(decoded_content.splitlines(), delimiter=",")
|
||||
my_list = list(cr)
|
||||
player_keys = [
|
||||
(i, key)
|
||||
for i, key in enumerate(my_list[1][: my_list[0].index("Offensive Stats")])
|
||||
]
|
||||
offensive_keys = [
|
||||
(i, key)
|
||||
for i, key in enumerate(
|
||||
my_list[1][
|
||||
my_list[0]
|
||||
.index("Offensive Stats") : my_list[0]
|
||||
.index("Defensive Stats")
|
||||
- 1
|
||||
],
|
||||
start=my_list[0].index("Offensive Stats"),
|
||||
)
|
||||
]
|
||||
defensive_keys = [
|
||||
(i, key)
|
||||
for i, key in enumerate(
|
||||
my_list[1][my_list[0].index("Defensive Stats") :],
|
||||
start=my_list[0].index("Defensive Stats"),
|
||||
)
|
||||
]
|
||||
|
||||
stats = {}
|
||||
for row in my_list[2:]:
|
||||
player_keys
|
||||
number, lname, fname = row[:3]
|
||||
if number == "Team":
|
||||
break
|
||||
gamechanger_id = id_lookup[(fname, lname)]
|
||||
stats[gamechanger_id] = {
|
||||
"offensive": {k: row[i] for i, k in offensive_keys},
|
||||
"defensive": {k: row[i] for i, k in defensive_keys},
|
||||
}
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
# d = scrape_page(season_id, team_id, page)
|
||||
pass
|
||||
|
||||
@@ -172,3 +172,9 @@ def lineup_submit(request):
|
||||
gamechanger.submit_lineup(request, lineup)
|
||||
return HttpResponse(status=200)
|
||||
return HttpResponseServerError()
|
||||
|
||||
|
||||
def stats(request):
|
||||
s = gamechanger.stats(request)
|
||||
return render(request, "gamechanger/stats.html", context={"stats": s})
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user