gamechanger updates (added events, teams). not used yet.

This commit is contained in:
2022-06-20 09:35:59 -05:00
parent c0c857a765
commit 9b26bbe973
8 changed files with 169 additions and 33 deletions

View File

@@ -7,7 +7,7 @@ import pytz
import requests
from bs4 import BeautifulSoup
url = "https://gc.com/t/{season_id}/{team_id}/{page}"
url = "https://gc.com/t/{season_id}/{team_slug}-{team_id}/{page}"
def get_authenticated_session(request):
@@ -35,12 +35,16 @@ def get_authenticated_session(request):
def submit_lineup(request, lineup):
authenticated_session = get_authenticated_session(request)
season_id = request.user.gamechanger_preferences.season_id
team_id = request.user.gamechanger_preferences.team_id
season_id = request.user.gamechanger_preferences.managed_team.season_slug
team_slug = request.user.gamechanger_preferences.managed_team.slug
team_id = request.user.gamechanger_preferences.managed_team.id
authenticated_session.headers.update(
{
"referer": url.format(
season_id=season_id, team_id=team_id, page="lineup_edit"
season_id=season_id,
team_slug=team_slug,
team_id=team_id,
page="lineup_edit",
),
"x-csrftoken": authenticated_session.cookies.get("csrftoken"),
"Content-Type": "application/x-www-form-urlencoded;",
@@ -48,12 +52,10 @@ def submit_lineup(request, lineup):
)
r = authenticated_session.post(
cookies=authenticated_session.cookies,
url="https://gc.com/do-save-lineup/{team_id}".format(
team_id=team_id.split("-").pop()
),
url=f"https://gc.com/do-save-lineup/{team_id}",
json={"lineup": lineup},
)
if r.status_code == 200:
if r.status_code == 20 and r.content == b"OK":
return r
else:
raise requests.exceptions.RequestException(
@@ -61,8 +63,10 @@ def submit_lineup(request, lineup):
)
def scrape_page(season_id, team_id, page):
r = requests.get(url.format(season_id=season_id, team_id=team_id, page=page))
def scrape_page(season_id, team_id, team_slug, page):
r = requests.get(
url.format(season_id=season_id, team_id=team_id, team_slug=team_slug, page=page)
)
initialize_page_json = re.search(
r'page.initialize\(\$.parseJSON\("(.*?)"\)', r.content.decode("unicode_escape")
)
@@ -160,14 +164,34 @@ def stream():
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
season_id = request.user.gamechanger_preferences.managed_team.season_slug
team_id = request.user.gamechanger_preferences.managed_team.id
team_slug = request.user.gamechanger_preferences.managed_team.slug
page = "stats/batting/Qualified/standard/csv"
authenticated_session.headers.update(
{
"referer": url.format(
season_id=season_id, team_id=team_id, team_slug=team_slug, page="stats"
),
"x-csrftoken": authenticated_session.cookies.get("csrftoken"),
}
)
r = authenticated_session.get(
url.format(season_id=season_id, team_id=team_id, page=page)
cookies=authenticated_session.cookies,
url=url.format(
season_id=season_id, team_id=team_id, team_slug=team_slug, page=page
),
)
roster = scrape_page(season_id, team_id, "roster")
if (
r.status_code != 200
or "Please sign in or join to continue." in r.content.decode("utf-8")
):
raise Exception("Stats fetch failed.")
roster = scrape_page(
season_id=season_id, team_id=team_id, team_slug=team_slug, page="roster"
)
id_lookup = {
(p.get("fname"), p.get("lname")): p.get("player_id") for p in roster["roster"]
}