Add --target-dir option to clips command

This commit is contained in:
Ivan Habunek 2024-08-28 13:12:11 +02:00
parent 07efac1ae7
commit 789d3d1939
No known key found for this signature in database
GPG Key ID: 01DB3DD0D824504C
4 changed files with 35 additions and 3 deletions

View File

@ -3,6 +3,11 @@ twitch-dl changelog
<!-- Do not edit. This file is automatically generated from changelog.yaml.-->
### [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)

View File

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

View File

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

View File

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