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
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

View File

@ -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)

View File

@ -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"])