diff --git a/tests/test_api.py b/tests/test_api.py index e4ba17c..4c6f6ce 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -6,6 +6,7 @@ import httpx import m3u8 from twitchdl import twitch from twitchdl.commands.download import _parse_playlists, get_clip_authenticated_url +from twitchdl.models import Game TEST_CHANNEL = "bananasaurus_rex" @@ -47,8 +48,11 @@ def test_get_clips(): def test_get_game(): - game_id = twitch.get_game_id("The Witness") - assert game_id == "17324" + game = twitch.find_game("The Witness") + assert isinstance(game, Game) + assert game.id == "17324" + assert game.name == "The Witness" + assert game.description - game_id = twitch.get_game_id("Does Not Exist Hoepfully") - assert game_id is None + game = twitch.find_game("Does Not Exist Hopefully") + assert game is None diff --git a/twitchdl/models.py b/twitchdl/models.py index 8b100b6..2cefa3f 100644 --- a/twitchdl/models.py +++ b/twitchdl/models.py @@ -31,10 +31,11 @@ class VideoQuality(): class Game(): id: str name: str + description: str @staticmethod def from_json(data: Json) -> "Game": - return Game(data["id"], data["name"]) + return Game(data["id"], data["name"], data["description"]) @dataclass(frozen=True) diff --git a/twitchdl/twitch.py b/twitchdl/twitch.py index 7deec44..806d2ed 100644 --- a/twitchdl/twitch.py +++ b/twitchdl/twitch.py @@ -6,7 +6,7 @@ import httpx from twitchdl import CLIENT_ID from twitchdl.exceptions import ConsoleError -from twitchdl.models import Clip, ClipsPage, ClipGenerator, GameID +from twitchdl.models import Clip, ClipsPage, ClipGenerator, Game from typing import Dict, Optional @@ -49,23 +49,29 @@ def gql_query(query: str, headers: Dict[str, str] = {}): return response -VIDEO_FIELDS = """ +GAME_FIELDS = """ + id + name + description +""" + +VIDEO_FIELDS = f""" id title publishedAt broadcastType lengthSeconds - game { - name - } - creator { + game {{ + {GAME_FIELDS} + }} + creator {{ login displayName - } + }} """ -CLIP_FIELDS = """ +CLIP_FIELDS = f""" id slug title @@ -73,19 +79,18 @@ CLIP_FIELDS = """ viewCount durationSeconds url - videoQualities { + videoQualities {{ frameRate quality sourceURL - } - game { - id - name - } - broadcaster { + }} + game {{ + {GAME_FIELDS} + }} + broadcaster {{ login displayName - } + }} """ @@ -345,16 +350,15 @@ def get_playlists(video_id, access_token): return response.content.decode('utf-8') -def get_game_id(name: str) -> Optional[GameID]: +def find_game(name: str) -> Optional[Game]: query = f""" {{ game(name: "{name.strip()}") {{ - id + {GAME_FIELDS} }} }} """ response = gql_query(query) - game = response["data"]["game"] - if game: - return game["id"] + if response["data"]["game"]: + return Game.from_json(response["data"]["game"])