Extract Game dataclass

This commit is contained in:
Ivan Habunek 2022-08-27 12:44:23 +02:00
parent aed0b993a7
commit afe38b84cd
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
3 changed files with 35 additions and 26 deletions

View File

@ -6,6 +6,7 @@ import httpx
import m3u8 import m3u8
from twitchdl import twitch from twitchdl import twitch
from twitchdl.commands.download import _parse_playlists, get_clip_authenticated_url from twitchdl.commands.download import _parse_playlists, get_clip_authenticated_url
from twitchdl.models import Game
TEST_CHANNEL = "bananasaurus_rex" TEST_CHANNEL = "bananasaurus_rex"
@ -47,8 +48,11 @@ def test_get_clips():
def test_get_game(): def test_get_game():
game_id = twitch.get_game_id("The Witness") game = twitch.find_game("The Witness")
assert game_id == "17324" 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") game = twitch.find_game("Does Not Exist Hopefully")
assert game_id is None assert game is None

View File

@ -31,10 +31,11 @@ class VideoQuality():
class Game(): class Game():
id: str id: str
name: str name: str
description: str
@staticmethod @staticmethod
def from_json(data: Json) -> "Game": def from_json(data: Json) -> "Game":
return Game(data["id"], data["name"]) return Game(data["id"], data["name"], data["description"])
@dataclass(frozen=True) @dataclass(frozen=True)

View File

@ -6,7 +6,7 @@ import httpx
from twitchdl import CLIENT_ID from twitchdl import CLIENT_ID
from twitchdl.exceptions import ConsoleError 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 from typing import Dict, Optional
@ -49,23 +49,29 @@ def gql_query(query: str, headers: Dict[str, str] = {}):
return response return response
VIDEO_FIELDS = """ GAME_FIELDS = """
id
name
description
"""
VIDEO_FIELDS = f"""
id id
title title
publishedAt publishedAt
broadcastType broadcastType
lengthSeconds lengthSeconds
game { game {{
name {GAME_FIELDS}
} }}
creator { creator {{
login login
displayName displayName
} }}
""" """
CLIP_FIELDS = """ CLIP_FIELDS = f"""
id id
slug slug
title title
@ -73,19 +79,18 @@ CLIP_FIELDS = """
viewCount viewCount
durationSeconds durationSeconds
url url
videoQualities { videoQualities {{
frameRate frameRate
quality quality
sourceURL sourceURL
} }}
game { game {{
id {GAME_FIELDS}
name }}
} broadcaster {{
broadcaster {
login login
displayName displayName
} }}
""" """
@ -345,16 +350,15 @@ def get_playlists(video_id, access_token):
return response.content.decode('utf-8') return response.content.decode('utf-8')
def get_game_id(name: str) -> Optional[GameID]: def find_game(name: str) -> Optional[Game]:
query = f""" query = f"""
{{ {{
game(name: "{name.strip()}") {{ game(name: "{name.strip()}") {{
id {GAME_FIELDS}
}} }}
}} }}
""" """
response = gql_query(query) response = gql_query(query)
game = response["data"]["game"] if response["data"]["game"]:
if game: return Game.from_json(response["data"]["game"])
return game["id"]