Add clips --copact option

This commit is contained in:
Ivan Habunek
2024-04-06 10:43:12 +02:00
parent a7ad4d8dcc
commit c1b58e178f
4 changed files with 43 additions and 28 deletions

View File

@ -7,6 +7,7 @@
- "Add setting defaults via environment variables, see: https://twitch-dl.bezdomni.net/environment_variables.html" - "Add setting defaults via environment variables, see: https://twitch-dl.bezdomni.net/environment_variables.html"
- "Add `download --concat` option to avoid using ffmeg for joinig vods and concat them instead. This will produce a `.ts` file by default." - "Add `download --concat` option to avoid using ffmeg for joinig vods and concat them instead. This will produce a `.ts` file by default."
- "Add video description to metadata (#129)" - "Add video description to metadata (#129)"
- "Add `clips --compact` option for listing clips in one-per-line mode"
2.1.4: 2.1.4:
date: 2024-01-06 date: 2024-01-06

View File

@ -101,6 +101,12 @@ def cli(ctx: click.Context, color: bool, debug: bool):
help="Fetch all clips, overrides --limit", help="Fetch all clips, overrides --limit",
is_flag=True, is_flag=True,
) )
@click.option(
"-c",
"--compact",
help="Show clips in compact mode, one line per video",
is_flag=True,
)
@click.option( @click.option(
"-d", "-d",
"--download", "--download",
@ -110,9 +116,8 @@ def cli(ctx: click.Context, color: bool, debug: bool):
@click.option( @click.option(
"-l", "-l",
"--limit", "--limit",
help="Number of clips to fetch [max: 100]", help="Number of clips to fetch. Defaults to 40 in compact mode, 10 otherwise.",
type=int, type=int,
default=10,
callback=validate_positive, callback=validate_positive,
) )
@click.option( @click.option(
@ -135,9 +140,10 @@ def cli(ctx: click.Context, color: bool, debug: bool):
def clips( def clips(
channel_name: str, channel_name: str,
all: bool, all: bool,
compact: bool,
download: bool, download: bool,
json: bool, json: bool,
limit: int, limit: int | None,
pager: int | None, pager: int | None,
period: ClipsPeriod, period: ClipsPeriod,
): ):
@ -147,6 +153,7 @@ def clips(
clips( clips(
channel_name, channel_name,
all=all, all=all,
compact=compact,
download=download, download=download,
json=json, json=json,
limit=limit, limit=limit,

View File

@ -1,14 +1,14 @@
import re import re
import sys import sys
from os import path from os import path
from typing import Generator from typing import Callable, Generator
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.download import download_file from twitchdl.download import download_file
from twitchdl.output import green, print_clip, 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 from twitchdl.twitch import Clip
@ -16,14 +16,18 @@ def clips(
channel_name: str, channel_name: str,
*, *,
all: bool = False, all: bool = False,
compact: bool = False,
download: bool = False, download: bool = False,
json: bool = False, json: bool = False,
limit: int = 10, limit: int | None = None,
pager: int | None = None, pager: int | None = None,
period: twitch.ClipsPeriod = "all_time", period: twitch.ClipsPeriod = "all_time",
): ):
# Set different defaults for limit for compact display
default_limit = 40 if compact else 10
# Ignore --limit if --pager or --all are given # Ignore --limit if --pager or --all are given
limit = sys.maxsize if all or pager else limit limit = sys.maxsize if all or pager else (limit or default_limit)
generator = twitch.channel_clips_generator(channel_name, period, limit) generator = twitch.channel_clips_generator(channel_name, period, limit)
@ -33,23 +37,12 @@ def clips(
if download: if download:
return _download_clips(generator) return _download_clips(generator)
print_fn = print_clip_compact if compact else print_clip
if pager: if pager:
return print_paged("Clips", generator, print_clip, pager) return print_paged("Clips", generator, print_fn, pager)
return _print_all(generator, all) return _print_all(generator, print_fn, all)
def _continue():
enter = click.style("Enter", bold=True, fg="green")
ctrl_c = click.style("Ctrl+C", bold=True, fg="yellow")
click.echo(f"Press {enter} to continue, {ctrl_c} to break.")
try:
input()
except KeyboardInterrupt:
return False
return True
def _target_filename(clip: Clip): def _target_filename(clip: Clip):
@ -86,10 +79,13 @@ def _download_clips(generator: Generator[Clip, None, None]):
download_file(url, target) download_file(url, target)
def _print_all(generator: Generator[Clip, None, None], all: bool): def _print_all(
generator: Generator[Clip, None, None],
print_fn: Callable[[Clip], None],
all: bool,
):
for clip in generator: for clip in generator:
click.echo() print_fn(clip)
print_clip(clip)
if not all: if not all:
click.secho( click.secho(

View File

@ -1,10 +1,10 @@
import click
import json import json
from itertools import islice from itertools import islice
from twitchdl import utils
from typing import Any, Callable, Generator, TypeVar from typing import Any, Callable, Generator, TypeVar
import click
from twitchdl import utils
from twitchdl.twitch import Clip, Video from twitchdl.twitch import Clip, Video
T = TypeVar("T") T = TypeVar("T")
@ -124,6 +124,17 @@ def print_clip(clip: Clip):
+ f" Views: {blue(clip['viewCount'])}" + f" Views: {blue(clip['viewCount'])}"
) )
click.secho(clip["url"], italic=True) click.secho(clip["url"], italic=True)
click.echo()
def print_clip_compact(clip: Clip):
slug = clip["slug"]
date = clip["createdAt"][:10]
title = truncate(clip["title"], 50).ljust(50)
game = clip["game"]["name"] if clip["game"] else ""
game = truncate(game, 30).ljust(30)
click.echo(f"{date} {green(title)} {blue(game)} {bold(slug)}")
def prompt_continue(): def prompt_continue():