From 438f084ef1bbbfdc27fae2aad1a56c84ebdeb3b5 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 23 Jun 2022 07:35:41 -0500 Subject: [PATCH] allow for storing season_slug, team_slug, team_id to client --- gamescrapyr/gamescrapyr.py | 41 ++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/gamescrapyr/gamescrapyr.py b/gamescrapyr/gamescrapyr.py index edea15d..ae2db1f 100644 --- a/gamescrapyr/gamescrapyr.py +++ b/gamescrapyr/gamescrapyr.py @@ -14,7 +14,10 @@ class GameChangerClient(): session = requests.Session() url = "https://gc.com/t/{season_slug}/{team_slug}-{team_id}/{page}" - def __init__(self, email, password): + def __init__(self, email, password, season_slug=None, team_slug=None, team_id=None): + self.season_slug=season_slug + self.team_slug=team_slug + self.team_id=team_id r = requests.get("https://gc.com/login") payload = { @@ -45,6 +48,17 @@ class GameChangerClient(): pass + def is_authorized(self): + login_url = "https://gc.com/login" + + check = self.session.get(login_url) + + # If authorized, the site will redirect away from login page + if check.url != login_url: + return True + else: + return False + def _scrape_from_page_initialize_json(self, season_slug, team_id, team_slug, page): resp = self.session.get( self.url.format(season_slug=season_slug, team_id=team_id, team_slug=team_slug, page=page) @@ -55,11 +69,17 @@ class GameChangerClient(): m = initialize_page_json.group(1) return json.loads(m) - def get_roster(self, season_slug, team_id, team_slug): + def get_roster(self, season_slug=None, team_id=None, team_slug=None): + if not season_slug: season_slug=self.season_slug + if not team_id: team_id=self.team_id + if not team_slug: team_slug=self.team_slug data = self._scrape_from_page_initialize_json(season_slug, team_id, team_slug, "roster") return data['roster'] - def get_stats(self, season_slug, team_id, team_slug): + def get_stats(self, season_slug=None, team_id=None, team_slug=None): + if not season_slug: season_slug=self.season_slug + if not team_id: team_id=self.team_id + if not team_slug: team_slug=self.team_slug resp = self.session.get( self.url.format( page="stats/batting/Qualified/standard/csv", @@ -122,7 +142,10 @@ class GameChangerClient(): return stats - def get_games(self, season_slug, team_id, team_slug): + def get_games(self, season_slug=None, team_id=None, team_slug=None): + if not season_slug: season_slug=self.season_slug + if not team_id: team_id=self.team_id + if not team_slug: team_slug=self.team_slug page = self.session.get( url=self.url.format( page="schedule/games", @@ -149,7 +172,10 @@ class GameChangerClient(): ) return games - def submit_lineup(self, lineup, season_slug, team_slug, team_id): + def submit_lineup(self, lineup, season_slug=None, team_id=None, team_slug=None): + if not season_slug: season_slug=self.season_slug + if not team_id: team_id=self.team_id + if not team_slug: team_slug=self.team_slug headers = self.session.headers headers.update( { @@ -191,7 +217,10 @@ class GameChangerClient(): game_stream = json.loads(stream_page.content) return game_stream - def get_lineup(self, season_slug, team_slug, team_id): + def get_lineup(self, season_slug=None, team_id=None, team_slug=None): + if not season_slug: season_slug=self.season_slug + if not team_id: team_id=self.team_id + if not team_slug: team_slug=self.team_slug resp = self.session.get( self.url.format( page="lineup",