From 52b96aab15e7fd3db519f07edf754b7df943da98 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sat, 24 Aug 2024 20:08:01 +0200 Subject: [PATCH] Simplify persisted queries --- twitchdl/twitch.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/twitchdl/twitch.py b/twitchdl/twitch.py index d130ac4..eff6730 100644 --- a/twitchdl/twitch.py +++ b/twitchdl/twitch.py @@ -2,7 +2,6 @@ Twitch API access. """ -import json import logging import time from typing import Any, Dict, Generator, List, Literal, Mapping, Optional, Tuple, TypedDict, Union @@ -148,9 +147,9 @@ def log_response(response: httpx.Response, duration: float): logger.debug(f"<-- {response.content}") -def gql_post(query: str): +def gql_persisted_query(query: Data): url = "https://gql.twitch.tv/gql" - response = authenticated_post(url, content=query) + response = authenticated_post(url, json=query) gql_raise_on_error(response) return response.json() @@ -238,22 +237,18 @@ def get_clip(slug: str) -> Optional[Clip]: def get_clip_access_token(slug: str) -> ClipAccessToken: - query = f""" - {{ + query = { "operationName": "VideoAccessToken_Clip", - "variables": {{ - "slug": "{slug}" - }}, - "extensions": {{ - "persistedQuery": {{ + "variables": {"slug": slug}, + "extensions": { + "persistedQuery": { "version": 1, - "sha256Hash": "36b89d2507fce29e5ca551df756d27c1cfe079e2609642b4390aa4c35796eb11" - }} - }} - }} - """ + "sha256Hash": "36b89d2507fce29e5ca551df756d27c1cfe079e2609642b4390aa4c35796eb11", + } + }, + } - response = gql_post(query.strip()) + response = gql_persisted_query(query) return response["data"]["clip"] @@ -486,7 +481,7 @@ def get_video_chapters(video_id: str) -> List[Chapter]: }, } - response = gql_post(json.dumps(query)) + response = gql_persisted_query(query) return list(_chapter_nodes(response["data"]["video"]["moments"]))