gamechanger updates (added events, teams)

This commit is contained in:
2022-06-15 10:26:12 -05:00
parent f5382f2d55
commit a3d6853ac5
8 changed files with 290 additions and 42 deletions

View File

@@ -1,8 +1,11 @@
import csv
import datetime
import json
import re
import pytz
import requests
from bs4 import BeautifulSoup
url = "https://gc.com/t/{season_id}/{team_id}/{page}"
@@ -67,6 +70,94 @@ def scrape_page(season_id, team_id, page):
return json.loads(m)
def get_teams(session):
url = "https://gc.com/account/teams"
session.headers.update(
{
"referer": url.format("https://gc.com/account/profile"),
"x-csrftoken": session.cookies.get("csrftoken"),
}
)
page = session.get(cookies=session.cookies, url=url)
soup = BeautifulSoup(page.content, "html.parser")
team_elements = [i for i in soup.find_all("li") if i.attrs.get("data-team-id")]
teams = []
for i, team_element in enumerate(team_elements):
league_type, number_of_games = [
c.text.strip() for c in team_element.findChildren("li")
][1:3]
season_slug, team_slug = (
team_element.find("a").attrs.get("href", "///").split("/")[2:]
)
teams.append(
{
"name": team_element.find("a").text,
"id": team_element.attrs.get("data-team-id"),
"season": team_element.findPrevious("header").text,
"league_type": league_type,
"number_of_games": number_of_games,
"season_slug": season_slug,
"team_slug": team_slug,
}
)
return teams
pass
def get_events(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"
authenticated_session.get(
url.format(season_id=season_id, team_id=team_id, page=page)
)
authenticated_session.headers.update(
{
"x-csrftoken": authenticated_session.cookies.get("csrftoken"),
}
)
page = authenticated_session.get(
cookies=authenticated_session.cookies,
url=url.format(season_id=season_id, team_id=team_id, page="schedule/games"),
)
soup = BeautifulSoup(page.content, "html.parser")
game_elements = [r for r in soup.find_all("tr") if "game" in r.attrs.get("class")]
games = []
for i, game_element in enumerate(game_elements):
game_slug = game_element.find("a").attrs.get("href").split("/")[1]
title = game_element.find("a").text
jslocaldate, jslocaltime_start, jslocaltime_arrival = (
t.attrs.get("datetime") for t in game_element.findAll("time")
)
games.append(
{
"id": game_element.attrs.get("data-id"),
"title": title,
"game_slug": game_slug,
"start": pytz.timezone("utc").localize(
datetime.datetime.fromisoformat(jslocaltime_start)
),
}
)
return games
def stream():
# game_page = authenticated_session.get(
# cookies=authenticated_session.cookies,
# url=f"https://gc.com/{game_slug}",
# )
# game_soup = BeautifulSoup(game_page.content, "html.parser")
# data_push_url_rel = game_soup.find("body").attrs.get("data-push-url")[2:]
# data_push_url = f"https://{data_push_url_rel}?sabertooth_aware=true"
# stream_page = authenticated_session.get(
# cookies=authenticated_session.cookies, url=data_push_url
# )
# game_stream = json.loads(stream_page.content)
pass
def stats(request):
authenticated_session = get_authenticated_session(request)
season_id = request.user.gamechanger_preferences.season_id