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.--> <!-- 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) ### [2.3.1 (2024-05-19)](https://github.com/ihabunek/twitch-dl/releases/tag/2.3.1)
* Fix fetching access token (#155, thanks @KryptonicDragon) * 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: 2.3.1:
date: 2024-05-19 date: 2024-05-19
changes: changes:

View File

@ -2,6 +2,7 @@ import logging
import platform import platform
import re import re
import sys import sys
from pathlib import Path
from typing import Optional, Tuple from typing import Optional, Tuple
import click import click
@ -141,6 +142,18 @@ def cli(ctx: click.Context, color: bool, debug: bool, verbose: bool):
default="all_time", default="all_time",
type=click.Choice(["last_day", "last_week", "last_month", "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 @json_option
def clips( def clips(
channel_name: str, channel_name: str,
@ -151,10 +164,14 @@ def clips(
limit: Optional[int], limit: Optional[int],
pager: Optional[int], pager: Optional[int],
period: ClipsPeriod, period: ClipsPeriod,
target_dir: Path,
): ):
"""List or download clips for given CHANNEL_NAME.""" """List or download clips for given CHANNEL_NAME."""
from twitchdl.commands.clips import clips from twitchdl.commands.clips import clips
if not target_dir.exists():
target_dir.mkdir(parents=True, exist_ok=True)
clips( clips(
channel_name, channel_name,
all=all, all=all,
@ -164,6 +181,7 @@ def clips(
limit=limit, limit=limit,
pager=pager, pager=pager,
period=period, period=period,
target_dir=target_dir,
) )

View File

@ -23,6 +23,7 @@ def clips(
limit: Optional[int] = None, limit: Optional[int] = None,
pager: Optional[int] = None, pager: Optional[int] = None,
period: ClipsPeriod = "all_time", period: ClipsPeriod = "all_time",
target_dir: Path = Path(),
): ):
# Set different defaults for limit for compact display # Set different defaults for limit for compact display
default_limit = 40 if compact else 10 default_limit = 40 if compact else 10
@ -36,7 +37,7 @@ def clips(
return print_json(list(generator)) return print_json(list(generator))
if download: if download:
return _download_clips(generator) return _download_clips(target_dir, generator)
print_fn = print_clip_compact if compact else print_clip print_fn = print_clip_compact if compact else print_clip
@ -68,9 +69,12 @@ def _target_filename(clip: Clip):
return f"{name}.{ext}" 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: for clip in generator:
target = Path(_target_filename(clip)) target = target_dir / _target_filename(clip)
if target.exists(): if target.exists():
click.echo(f"Already downloaded: {green(target)}") click.echo(f"Already downloaded: {green(target)}")