2022-02-23 18:07:27 +00:00
|
|
|
import sys
|
|
|
|
|
2024-03-31 19:39:54 +00:00
|
|
|
import click
|
|
|
|
|
2021-01-07 08:34:14 +00:00
|
|
|
from twitchdl import twitch
|
|
|
|
from twitchdl.exceptions import ConsoleError
|
2024-03-31 19:39:54 +00:00
|
|
|
from twitchdl.output import print_log, print_paged_videos, print_video, print_json, print_video_compact
|
2022-02-23 18:07:27 +00:00
|
|
|
|
|
|
|
|
2024-03-23 09:50:42 +00:00
|
|
|
def videos(
|
|
|
|
channel_name: str,
|
|
|
|
*,
|
|
|
|
all: bool,
|
|
|
|
compact: bool,
|
|
|
|
games: list[str],
|
|
|
|
json: bool,
|
|
|
|
limit: int | None,
|
|
|
|
pager: int | None,
|
2024-03-30 06:52:43 +00:00
|
|
|
sort: twitch.VideosSort,
|
|
|
|
type: twitch.VideosType,
|
2024-03-23 09:50:42 +00:00
|
|
|
):
|
|
|
|
game_ids = _get_game_ids(games)
|
2022-08-18 08:06:23 +00:00
|
|
|
|
|
|
|
# Set different defaults for limit for compact display
|
2024-03-23 09:50:42 +00:00
|
|
|
limit = limit or (40 if compact else 10)
|
2022-08-18 08:06:23 +00:00
|
|
|
|
2022-02-23 18:07:27 +00:00
|
|
|
# Ignore --limit if --pager or --all are given
|
2024-03-23 09:50:42 +00:00
|
|
|
max_videos = sys.maxsize if all or pager else limit
|
2022-02-23 18:07:27 +00:00
|
|
|
|
|
|
|
total_count, generator = twitch.channel_videos_generator(
|
2024-03-23 09:50:42 +00:00
|
|
|
channel_name, max_videos, sort, type, game_ids=game_ids)
|
2022-02-23 18:07:27 +00:00
|
|
|
|
2024-03-23 09:50:42 +00:00
|
|
|
if json:
|
2022-02-23 18:07:27 +00:00
|
|
|
videos = list(generator)
|
|
|
|
print_json({
|
|
|
|
"count": len(videos),
|
|
|
|
"totalCount": total_count,
|
|
|
|
"videos": videos
|
|
|
|
})
|
|
|
|
return
|
|
|
|
|
|
|
|
if total_count == 0:
|
2024-03-31 19:39:54 +00:00
|
|
|
click.echo("No videos found")
|
2022-02-23 18:07:27 +00:00
|
|
|
return
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2024-03-23 09:50:42 +00:00
|
|
|
if pager:
|
|
|
|
print_paged_videos(generator, pager, total_count)
|
2022-02-23 18:07:27 +00:00
|
|
|
return
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-23 18:07:27 +00:00
|
|
|
count = 0
|
|
|
|
for video in generator:
|
2024-03-23 09:50:42 +00:00
|
|
|
if compact:
|
2022-08-18 08:03:40 +00:00
|
|
|
print_video_compact(video)
|
|
|
|
else:
|
2024-03-31 19:39:54 +00:00
|
|
|
click.echo()
|
2022-08-18 08:03:40 +00:00
|
|
|
print_video(video)
|
2022-02-23 18:07:27 +00:00
|
|
|
count += 1
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2024-03-31 19:39:54 +00:00
|
|
|
click.echo()
|
|
|
|
click.echo("-" * 80)
|
|
|
|
click.echo(f"Videos 1-{count} of {total_count}")
|
2021-01-07 08:34:14 +00:00
|
|
|
|
2022-02-23 18:07:27 +00:00
|
|
|
if total_count > count:
|
2024-03-31 19:39:54 +00:00
|
|
|
click.secho(
|
|
|
|
"\nThere are more videos. Increase the --limit, use --all or --pager to see the rest.",
|
|
|
|
dim=True
|
2022-02-23 18:07:27 +00:00
|
|
|
)
|
2021-01-07 08:34:14 +00:00
|
|
|
|
|
|
|
|
2024-03-30 06:52:43 +00:00
|
|
|
def _get_game_ids(names: list[str]) -> list[str]:
|
2021-01-07 08:34:14 +00:00
|
|
|
if not names:
|
|
|
|
return []
|
|
|
|
|
|
|
|
game_ids = []
|
|
|
|
for name in names:
|
2024-03-30 14:36:53 +00:00
|
|
|
print_log(f"Looking up game '{name}'...")
|
2021-01-07 08:34:14 +00:00
|
|
|
game_id = twitch.get_game_id(name)
|
|
|
|
if not game_id:
|
2024-03-28 11:06:50 +00:00
|
|
|
raise ConsoleError(f"Game '{name}' not found")
|
2021-01-07 08:34:14 +00:00
|
|
|
game_ids.append(int(game_id))
|
|
|
|
|
|
|
|
return game_ids
|