twitch-dl/twitchdl/commands/videos.py

81 lines
1.9 KiB
Python
Raw Normal View History

import sys
2021-01-07 08:34:14 +00:00
from twitchdl import twitch
from twitchdl.exceptions import ConsoleError
2022-08-18 08:03:40 +00:00
from twitchdl.output import print_out, print_paged_videos, print_video, print_json, print_video_compact
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,
sort: str,
type: str,
):
game_ids = _get_game_ids(games)
# Set different defaults for limit for compact display
2024-03-23 09:50:42 +00:00
limit = limit or (40 if compact else 10)
# 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
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)
2024-03-23 09:50:42 +00:00
if json:
videos = list(generator)
print_json({
"count": len(videos),
"totalCount": total_count,
"videos": videos
})
return
if total_count == 0:
print_out("<yellow>No videos found</yellow>")
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)
return
2021-01-07 08:34:14 +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:
print_out()
print_video(video)
count += 1
2021-01-07 08:34:14 +00:00
print_out()
print_out("-" * 80)
2024-03-28 11:06:50 +00:00
print_out(f"<yellow>Videos 1-{count} of {total_count}</yellow>")
2021-01-07 08:34:14 +00:00
if total_count > count:
print_out()
print_out(
"<dim>There are more videos. Increase the --limit, use --all or --pager to see the rest.</dim>"
)
2021-01-07 08:34:14 +00:00
def _get_game_ids(names):
if not names:
return []
game_ids = []
for name in names:
2024-03-28 11:06:50 +00:00
print_out(f"<dim>Looking up game '{name}'...</dim>")
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