From 789d3d19394a2a10962b15c30368e5ff24569810 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Wed, 28 Aug 2024 13:12:11 +0200 Subject: [PATCH] Add --target-dir option to clips command --- CHANGELOG.md | 5 +++++ changelog.yaml | 5 +++++ twitchdl/cli.py | 18 ++++++++++++++++++ twitchdl/commands/clips.py | 10 +++++++--- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc2561f..2e66951 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ twitch-dl changelog +### [2.4.0 (TBA)](https://github.com/ihabunek/twitch-dl/releases/tag/2.4.0) + +* Add `clips --target-dir` option. Use in conjunction with `--download` to + specify target directory. + ### [2.3.1 (2024-05-19)](https://github.com/ihabunek/twitch-dl/releases/tag/2.3.1) * Fix fetching access token (#155, thanks @KryptonicDragon) diff --git a/changelog.yaml b/changelog.yaml index a616957..457a56d 100644 --- a/changelog.yaml +++ b/changelog.yaml @@ -1,3 +1,8 @@ +2.4.0: + date: TBA + changes: + - "Add `clips --target-dir` option. Use in conjunction with `--download` to specify target directory." + 2.3.1: date: 2024-05-19 changes: diff --git a/twitchdl/cli.py b/twitchdl/cli.py index 7265666..f6d6d32 100644 --- a/twitchdl/cli.py +++ b/twitchdl/cli.py @@ -2,6 +2,7 @@ import logging import platform import re import sys +from pathlib import Path from typing import Optional, Tuple import click @@ -141,6 +142,18 @@ def cli(ctx: click.Context, color: bool, debug: bool, verbose: bool): default="all_time", type=click.Choice(["last_day", "last_week", "last_month", "all_time"]), ) +@click.option( + "-t", + "--target-dir", + help="Target directory when downloading clips", + type=click.Path( + file_okay=False, + readable=False, + writable=True, + path_type=Path, + ), + default=Path(), +) @json_option def clips( channel_name: str, @@ -151,10 +164,14 @@ def clips( limit: Optional[int], pager: Optional[int], period: ClipsPeriod, + target_dir: Path, ): """List or download clips for given CHANNEL_NAME.""" from twitchdl.commands.clips import clips + if not target_dir.exists(): + target_dir.mkdir(parents=True, exist_ok=True) + clips( channel_name, all=all, @@ -164,6 +181,7 @@ def clips( limit=limit, pager=pager, period=period, + target_dir=target_dir, ) diff --git a/twitchdl/commands/clips.py b/twitchdl/commands/clips.py index c4249ab..5c533f2 100644 --- a/twitchdl/commands/clips.py +++ b/twitchdl/commands/clips.py @@ -23,6 +23,7 @@ def clips( limit: Optional[int] = None, pager: Optional[int] = None, period: ClipsPeriod = "all_time", + target_dir: Path = Path(), ): # Set different defaults for limit for compact display default_limit = 40 if compact else 10 @@ -36,7 +37,7 @@ def clips( return print_json(list(generator)) if download: - return _download_clips(generator) + return _download_clips(target_dir, generator) print_fn = print_clip_compact if compact else print_clip @@ -68,9 +69,12 @@ def _target_filename(clip: Clip): return f"{name}.{ext}" -def _download_clips(generator: Generator[Clip, None, None]): +def _download_clips(target_dir: Path, generator: Generator[Clip, None, None]): + if not target_dir.exists(): + target_dir.mkdir(parents=True, exist_ok=True) + for clip in generator: - target = Path(_target_filename(clip)) + target = target_dir / _target_filename(clip) if target.exists(): click.echo(f"Already downloaded: {green(target)}")