import requests def download_data(query: str, params={}) -> dict | Exception: url = "https://esports-api.lolesports.com/persisted/gw/" header = { "x-api-key": "0TvQnueqKa5mxJntVWt0w4LpLfEkrV1Ta8rQBb9Z", "hl": "en-US", } params["hl"] = "en-US" response = requests.get(url + query, headers=header, params=params) if response.status_code == 200: return response.json() raise Exception(f"bad query {response.json()}") def download_window(game_id: str) -> dict | None: url = f"https://feed.lolesports.com/livestats/v1/window/{game_id}" response = requests.get(url) # else the game was never played if response.status_code == 200: return response.json() return None def leagueid(name: str) -> str | Exception: leagues: dict = download_data("getLeagues")["data"]["leagues"] for league in leagues: if league["name"] == name: return league["id"] raise Exception(f"league {name} not found") def league_tournaments(id: int) -> dict: return download_data("getTournamentsForLeague", params={"leagueId": id})["data"]["leagues"][0]["tournaments"] def year_tournaments(tournaments: list[dict], year: str) -> list[dict]: res = [] for t in tournaments: if year in t["slug"]: res.append(t) return res def tournaments_matches_and_games(tournaments: list[dict]) -> (list[dict], list[list[dict]]): """ return matches, games """ ids = [t["id"] for t in tournaments] # print(ids) events = [download_data("getCompletedEvents", params={"tournamentId": id})["data"]["schedule"]["events"] for id in ids] matches = [] games = [] for event in events: for match in event: matches.append(match["match"]) games.append(match["games"]) # print(match["games"]) return matches, games def game_windows(games: list[dict]) -> list[dict]: windows = [] for game in games: window = download_window(game["id"]) if window is not None: windows.append(download_window(game["id"])) return windows