Remove union types to fix python 3.8 compat

This commit is contained in:
Ivan Habunek 2024-04-23 17:14:27 +02:00
parent e50499351b
commit a0ad66ee69
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
8 changed files with 51 additions and 48 deletions

View File

@ -2,6 +2,7 @@ import logging
import platform
import re
import sys
from typing import Optional
import click
@ -30,13 +31,13 @@ json_option = click.option(
)
def validate_positive(_ctx: click.Context, _param: click.Parameter, value: int | None):
def validate_positive(_ctx: click.Context, _param: click.Parameter, value: Optional[int]):
if value is not None and value <= 0:
raise click.BadParameter("must be greater than 0")
return value
def validate_time(_ctx: click.Context, _param: click.Parameter, value: str) -> int | None:
def validate_time(_ctx: click.Context, _param: click.Parameter, value: str) -> Optional[int]:
"""Parse a time string (hh:mm or hh:mm:ss) to number of seconds."""
if not value:
return None
@ -56,7 +57,7 @@ def validate_time(_ctx: click.Context, _param: click.Parameter, value: str) -> i
return hours * 3600 + minutes * 60 + seconds
def validate_rate(_ctx: click.Context, _param: click.Parameter, value: str) -> int | None:
def validate_rate(_ctx: click.Context, _param: click.Parameter, value: str) -> Optional[int]:
if not value:
return None
@ -143,8 +144,8 @@ def clips(
compact: bool,
download: bool,
json: bool,
limit: int | None,
pager: int | None,
limit: Optional[int],
pager: Optional[int],
period: ClipsPeriod,
):
"""List or download clips for given CHANNEL_NAME."""
@ -255,19 +256,19 @@ def clips(
)
def download(
ids: tuple[str, ...],
auth_token: str | None,
chapter: int | None,
auth_token: Optional[str],
chapter: Optional[int],
concat: bool,
dry_run: bool,
end: int | None,
end: Optional[int],
format: str,
keep: bool,
no_join: bool,
overwrite: bool,
output: str,
quality: str | None,
rate_limit: int | None,
start: int | None,
quality: Optional[str],
rate_limit: Optional[int],
start: Optional[int],
max_workers: int,
):
"""Download videos or clips.
@ -375,8 +376,8 @@ def videos(
compact: bool,
games_tuple: tuple[str, ...],
json: bool,
limit: int | None,
pager: int | None,
limit: Optional[int],
pager: Optional[int],
sort: VideosSort,
type: VideosType,
):

View File

@ -1,7 +1,7 @@
import re
import sys
from os import path
from typing import Callable, Generator
from typing import Callable, Generator, Optional
import click
@ -9,7 +9,7 @@ from twitchdl import twitch, utils
from twitchdl.commands.download import get_clip_authenticated_url
from twitchdl.download import download_file
from twitchdl.output import green, print_clip, print_clip_compact, print_json, print_paged, yellow
from twitchdl.twitch import Clip
from twitchdl.twitch import Clip, ClipsPeriod
def clips(
@ -19,9 +19,9 @@ def clips(
compact: bool = False,
download: bool = False,
json: bool = False,
limit: int | None = None,
pager: int | None = None,
period: twitch.ClipsPeriod = "all_time",
limit: Optional[int] = None,
pager: Optional[int] = None,
period: ClipsPeriod = "all_time",
):
# Set different defaults for limit for compact display
default_limit = 40 if compact else 10

View File

@ -1,4 +1,5 @@
import sys
from typing import Optional
import click
@ -14,8 +15,8 @@ def videos(
compact: bool,
games: list[str],
json: bool,
limit: int | None,
pager: int | None,
limit: Optional[int],
pager: Optional[int],
sort: twitch.VideosSort,
type: twitch.VideosType,
):

View File

@ -1,22 +1,22 @@
from dataclasses import dataclass
from typing import Any
from typing import Any, Optional
@dataclass
class DownloadOptions:
auth_token: str | None
chapter: int | None
auth_token: Optional[str]
chapter: Optional[int]
concat: bool
dry_run: bool
end: int | None
end: Optional[int]
format: str
keep: bool
no_join: bool
overwrite: bool
output: str
quality: str | None
rate_limit: int | None
start: int | None
quality: Optional[str]
rate_limit: Optional[int]
start: Optional[int]
max_workers: int

View File

@ -1,6 +1,6 @@
import json
from itertools import islice
from typing import Any, Callable, Generator, TypeVar
from typing import Any, Callable, Generator, Optional, TypeVar
import click
@ -49,7 +49,7 @@ def print_paged(
generator: Generator[T, Any, Any],
print_fn: Callable[[T], None],
page_size: int,
total_count: int | None = None,
total_count: Optional[int] = None,
):
iterator = iter(generator)
page = list(islice(iterator, page_size))

View File

@ -3,7 +3,7 @@ Parse and manipulate m3u8 playlists.
"""
from dataclasses import dataclass
from typing import Generator, OrderedDict
from typing import Generator, Optional, OrderedDict
import click
import m3u8
@ -15,7 +15,7 @@ from twitchdl.output import bold, dim
@dataclass
class Playlist:
name: str
resolution: str | None
resolution: Optional[str]
url: str
@ -53,8 +53,8 @@ def load_m3u8(playlist_m3u8: str) -> m3u8.M3U8:
def enumerate_vods(
document: m3u8.M3U8,
start: int | None = None,
end: int | None = None,
start: Optional[int] = None,
end: Optional[int] = None,
) -> list[Vod]:
"""Extract VODs for download from document."""
vods = []
@ -97,7 +97,7 @@ def make_join_playlist(
return playlist
def select_playlist(playlists: list[Playlist], quality: str | None) -> Playlist:
def select_playlist(playlists: list[Playlist], quality: Optional[str]) -> Playlist:
return (
select_playlist_by_name(playlists, quality)
if quality is not None

View File

@ -3,7 +3,7 @@ Twitch API access.
"""
import json
from typing import Dict, Generator, Literal, TypedDict
from typing import Dict, Generator, Literal, TypedDict, Optional
import click
import httpx
@ -163,7 +163,7 @@ CLIP_FIELDS = """
"""
def get_video(video_id: str) -> Video | None:
def get_video(video_id: str) -> Optional[Video]:
query = f"""
{{
video(id: "{video_id}") {{
@ -176,7 +176,7 @@ def get_video(video_id: str) -> Video | None:
return response["data"]["video"]
def get_clip(slug: str) -> Clip | None:
def get_clip(slug: str) -> Optional[Clip]:
query = f"""
{{
clip(slug: "{slug}") {{
@ -209,7 +209,7 @@ def get_clip_access_token(slug: str) -> ClipAccessToken:
return response["data"]["clip"]
def get_channel_clips(channel_id: str, period: ClipsPeriod, limit: int, after: str | None = None):
def get_channel_clips(channel_id: str, period: ClipsPeriod, limit: int, after: Optional[str] = None):
"""
List channel clips.
@ -294,8 +294,8 @@ def get_channel_videos(
limit: int,
sort: str,
type: str = "archive",
game_ids: list[str] | None = None,
after: str | None = None,
game_ids: Optional[list[str]] = None,
after: Optional[str] = None,
):
game_ids = game_ids or []
@ -339,7 +339,7 @@ def channel_videos_generator(
max_videos: int,
sort: VideosSort,
type: VideosType,
game_ids: list[str] | None = None,
game_ids: Optional[list[str]] = None,
) -> tuple[int, Generator[Video, None, None]]:
game_ids = game_ids or []
@ -364,7 +364,7 @@ def channel_videos_generator(
return videos["totalCount"], _generator(videos, max_videos)
def get_access_token(video_id: str, auth_token: str | None = None) -> AccessToken:
def get_access_token(video_id: str, auth_token: Optional[str] = None) -> AccessToken:
query = f"""
{{
videoPlaybackAccessToken(

View File

@ -1,4 +1,5 @@
import re
from typing import Optional, Union
import unicodedata
import click
@ -11,7 +12,7 @@ def _format_size(value: float, digits: int, unit: str):
return f"{int(value)}{unit}"
def format_size(bytes_: int | float, digits: int = 1):
def format_size(bytes_: Union[int, float], digits: int = 1):
if bytes_ < 1024:
return _format_size(bytes_, digits, "B")
@ -26,7 +27,7 @@ def format_size(bytes_: int | float, digits: int = 1):
return _format_size(mega / 1024, digits, "GB")
def format_duration(total_seconds: int | float) -> str:
def format_duration(total_seconds: Union[int, float]) -> str:
total_seconds = int(total_seconds)
hours = total_seconds // 3600
remainder = total_seconds % 3600
@ -42,7 +43,7 @@ def format_duration(total_seconds: int | float) -> str:
return f"{seconds} sec"
def format_time(total_seconds: int | float, force_hours: bool = False) -> str:
def format_time(total_seconds: Union[int, float], force_hours: bool = False) -> str:
total_seconds = int(total_seconds)
hours = total_seconds // 3600
remainder = total_seconds % 3600
@ -55,7 +56,7 @@ def format_time(total_seconds: int | float, force_hours: bool = False) -> str:
return f"{minutes:02}:{seconds:02}"
def read_int(msg: str, min: int, max: int, default: int | None = None) -> int:
def read_int(msg: str, min: int, max: int, default: Optional[int] = None) -> int:
while True:
try:
val = click.prompt(msg, default=default, type=int)
@ -93,7 +94,7 @@ CLIP_PATTERNS = [
]
def parse_video_identifier(identifier: str) -> str | None:
def parse_video_identifier(identifier: str) -> Optional[str]:
"""Given a video ID or URL returns the video ID, or null if not matched"""
for pattern in VIDEO_PATTERNS:
match = re.match(pattern, identifier)
@ -101,7 +102,7 @@ def parse_video_identifier(identifier: str) -> str | None:
return match.group("id")
def parse_clip_identifier(identifier: str) -> str | None:
def parse_clip_identifier(identifier: str) -> Optional[str]:
"""Given a clip slug or URL returns the clip slug, or null if not matched"""
for pattern in CLIP_PATTERNS:
match = re.match(pattern, identifier)