Fix a crash when downloading clips

This commit is contained in:
Ivan Habunek 2024-08-28 14:16:49 +02:00
parent 789d3d1939
commit 42f7a9a1a5
No known key found for this signature in database
GPG Key ID: 01DB3DD0D824504C
3 changed files with 12 additions and 5 deletions

View File

@ -2,6 +2,7 @@
date: TBA date: TBA
changes: changes:
- "Add `clips --target-dir` option. Use in conjunction with `--download` to specify target directory." - "Add `clips --target-dir` option. Use in conjunction with `--download` to specify target directory."
- "Fix a crash when downloading clips (#160)"
2.3.1: 2.3.1:
date: 2024-05-19 date: 2024-05-19

View File

@ -2,12 +2,13 @@ import re
import sys import sys
from os import path from os import path
from pathlib import Path from pathlib import Path
from typing import Callable, Generator, Optional from typing import Callable, Generator, List, Optional
import click import click
from twitchdl import twitch, utils from twitchdl import twitch, utils
from twitchdl.commands.download import get_clip_authenticated_url from twitchdl.commands.download import get_clip_authenticated_url
from twitchdl.entities import VideoQuality
from twitchdl.http import download_file from twitchdl.http import download_file
from twitchdl.output import green, print_clip, print_clip_compact, print_json, print_paged, yellow from twitchdl.output import green, print_clip, print_clip_compact, print_json, print_paged, yellow
from twitchdl.twitch import Clip, ClipsPeriod from twitchdl.twitch import Clip, ClipsPeriod
@ -47,8 +48,8 @@ def clips(
return _print_all(generator, print_fn, all) return _print_all(generator, print_fn, all)
def _target_filename(clip: Clip): def _target_filename(clip: Clip, video_qualities: List[VideoQuality]):
url = clip["videoQualities"][0]["sourceURL"] url = video_qualities[0]["sourceURL"]
_, ext = path.splitext(url) _, ext = path.splitext(url)
ext = ext.lstrip(".") ext = ext.lstrip(".")
@ -74,7 +75,12 @@ def _download_clips(target_dir: Path, generator: Generator[Clip, None, None]):
target_dir.mkdir(parents=True, exist_ok=True) target_dir.mkdir(parents=True, exist_ok=True)
for clip in generator: for clip in generator:
target = target_dir / _target_filename(clip) # videoQualities can be null in some circumstances, see:
# https://github.com/ihabunek/twitch-dl/issues/160
if not clip["videoQualities"]:
continue
target = target_dir / _target_filename(clip, clip["videoQualities"])
if target.exists(): if target.exists():
click.echo(f"Already downloaded: {green(target)}") click.echo(f"Already downloaded: {green(target)}")

View File

@ -60,7 +60,7 @@ class Clip(TypedDict):
viewCount: int viewCount: int
durationSeconds: int durationSeconds: int
url: str url: str
videoQualities: List[VideoQuality] videoQualities: Optional[List[VideoQuality]]
game: Game game: Game
broadcaster: User broadcaster: User