Add Video typed dict

This commit is contained in:
Ivan Habunek 2024-04-02 09:37:53 +02:00
parent 3270d857b1
commit 6a1900b628
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
4 changed files with 35 additions and 12 deletions

View File

@ -20,6 +20,7 @@ from twitchdl.entities import Data, DownloadOptions
from twitchdl.exceptions import ConsoleError from twitchdl.exceptions import ConsoleError
from twitchdl.http import download_all from twitchdl.http import download_all
from twitchdl.output import blue, bold, dim, green, print_log, yellow from twitchdl.output import blue, bold, dim, green, print_log, yellow
from twitchdl.twitch import Video
def download(ids: list[str], args: DownloadOptions): def download(ids: list[str], args: DownloadOptions):
@ -115,7 +116,7 @@ def _concat_vods(vod_paths: list[str], target: str):
raise ConsoleError(f"Joining files failed: {result.stderr}") raise ConsoleError(f"Joining files failed: {result.stderr}")
def get_video_placeholders(video: Data, format: str) -> Data: def get_video_placeholders(video: Video, format: str) -> Data:
date, time = video['publishedAt'].split("T") date, time = video['publishedAt'].split("T")
game = video["game"]["name"] if video["game"] else "Unknown" game = video["game"]["name"] if video["game"] else "Unknown"
@ -134,7 +135,7 @@ def get_video_placeholders(video: Data, format: str) -> Data:
} }
def _video_target_filename(video: Data, args: DownloadOptions): def _video_target_filename(video: Video, args: DownloadOptions):
subs = get_video_placeholders(video, args.format) subs = get_video_placeholders(video, args.format)
try: try:

View File

@ -47,7 +47,7 @@ def info(id: str, *, json: bool = False):
raise ConsoleError(f"Invalid input: {id}") raise ConsoleError(f"Invalid input: {id}")
def video_info(video, playlists, chapters): def video_info(video: Video, playlists, chapters):
click.echo() click.echo()
print_video(video) print_video(video)

View File

@ -6,6 +6,7 @@ from twitchdl import utils
from typing import Any, Callable, Generator, TypeVar from typing import Any, Callable, Generator, TypeVar
from twitchdl.entities import Data from twitchdl.entities import Data
from twitchdl.twitch import Video
T = TypeVar("T") T = TypeVar("T")
@ -78,7 +79,7 @@ def print_paged(
def print_video(video: Data): def print_video(video: Video):
published_at = video["publishedAt"].replace("T", " @ ").replace("Z", "") published_at = video["publishedAt"].replace("T", " @ ").replace("Z", "")
length = utils.format_duration(video["lengthSeconds"]) length = utils.format_duration(video["lengthSeconds"])
@ -89,7 +90,7 @@ def print_video(video: Data):
url = f"https://www.twitch.tv/videos/{video['id']}" url = f"https://www.twitch.tv/videos/{video['id']}"
click.secho(f"Video {video['id']}", bold=True) click.secho(f"Video {video['id']}", bold=True)
click.secho(f"{video['title']}", fg="green") click.secho(video["title"], fg="green")
if channel or playing: if channel or playing:
click.echo(" ".join([channel, playing])) click.echo(" ".join([channel, playing]))
@ -102,7 +103,7 @@ def print_video(video: Data):
click.echo() click.echo()
def print_video_compact(video: Data): def print_video_compact(video: Video):
id = video["id"] id = video["id"]
date = video["publishedAt"][:10] date = video["publishedAt"][:10]
game = video["game"]["name"] if video["game"] else "" game = video["game"]["name"] if video["game"] else ""

View File

@ -6,7 +6,7 @@ import httpx
import json import json
import click import click
from typing import Dict, Generator, Literal from typing import Dict, Generator, Literal, TypedDict
from twitchdl import CLIENT_ID from twitchdl import CLIENT_ID
from twitchdl.entities import Data from twitchdl.entities import Data
from twitchdl.exceptions import ConsoleError from twitchdl.exceptions import ConsoleError
@ -16,6 +16,26 @@ ClipsPeriod = Literal["last_day", "last_week", "last_month", "all_time"]
VideosSort = Literal["views", "time"] VideosSort = Literal["views", "time"]
VideosType = Literal["archive", "highlight", "upload"] VideosType = Literal["archive", "highlight", "upload"]
class User(TypedDict):
login: str
displayName: str
class Game(TypedDict):
id: str
name: str
class Video(TypedDict):
id: str
title: str
description: str
publishedAt: str
broadcastType: str
lengthSeconds: int
game: Game
creator: User
class GQLError(click.ClickException): class GQLError(click.ClickException):
def __init__(self, errors: list[str]): def __init__(self, errors: list[str]):
@ -67,6 +87,7 @@ VIDEO_FIELDS = """
broadcastType broadcastType
lengthSeconds lengthSeconds
game { game {
id
name name
} }
creator { creator {
@ -100,7 +121,7 @@ CLIP_FIELDS = """
""" """
def get_video(video_id: str): def get_video(video_id: str) -> Video | None:
query = f""" query = f"""
{{ {{
video(id: "{video_id}") {{ video(id: "{video_id}") {{
@ -274,10 +295,10 @@ def channel_videos_generator(
sort: VideosSort, sort: VideosSort,
type: VideosType, type: VideosType,
game_ids: list[str] | None = None game_ids: list[str] | None = None
) -> tuple[int, Generator[Data, None, None]]: ) -> tuple[int, Generator[Video, None, None]]:
game_ids = game_ids or [] game_ids = game_ids or []
def _generator(videos: Data, max_videos: int) -> Generator[Data, None, None]: def _generator(videos: Data, max_videos: int) -> Generator[Video, None, None]:
for video in videos["edges"]: for video in videos["edges"]:
if max_videos < 1: if max_videos < 1:
return return
@ -298,7 +319,7 @@ def channel_videos_generator(
return videos["totalCount"], _generator(videos, max_videos) return videos["totalCount"], _generator(videos, max_videos)
def get_access_token(video_id, auth_token=None): def get_access_token(video_id: str, auth_token: str | None = None):
query = f""" query = f"""
{{ {{
videoPlaybackAccessToken( videoPlaybackAccessToken(
@ -315,7 +336,7 @@ def get_access_token(video_id, auth_token=None):
}} }}
""" """
headers = {} headers: dict[str, str] = {}
if auth_token is not None: if auth_token is not None:
headers['authorization'] = f'OAuth {auth_token}' headers['authorization'] = f'OAuth {auth_token}'