Add support for JSON output to videos command

This commit is contained in:
Matt Calvert 2022-02-09 21:49:46 +01:00 committed by Ivan Habunek
parent b4c31b04e1
commit 4928188055
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 15 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from twitchdl import twitch from twitchdl import twitch
from twitchdl.exceptions import ConsoleError from twitchdl.exceptions import ConsoleError
from twitchdl.output import print_out, print_video from twitchdl.output import print_out, print_video, print_json
def _continue(): def _continue():
@ -33,13 +33,21 @@ def _get_game_ids(names):
return game_ids return game_ids
def _videos_json(generator):
print_json([video["edges"] for video, has_more in generator][0])
def videos(args): def videos(args):
game_ids = _get_game_ids(args.game) game_ids = _get_game_ids(args.game)
print_out("<dim>Loading videos...</dim>")
generator = twitch.channel_videos_generator( generator = twitch.channel_videos_generator(
args.channel_name, args.limit, args.sort, args.type, game_ids=game_ids) args.channel_name, args.limit, args.sort, args.type, game_ids=game_ids)
if args.json:
return _videos_json(generator)
print_out("<dim>Loading videos...</dim>")
first = 1 first = 1
for videos, has_more in generator: for videos, has_more in generator:

View File

@ -77,6 +77,11 @@ COMMANDS = [
"choices": ["archive", "highlight", "upload"], "choices": ["archive", "highlight", "upload"],
"default": "archive", "default": "archive",
}), }),
(["-j", "--json"], {
"help": "Show results as JSON",
"action": "store_true",
"default": False,
}),
(["-p", "--pager"], { (["-p", "--pager"], {
"help": "If there are more results than LIMIT, ask to show next page", "help": "If there are more results than LIMIT, ask to show next page",
"action": "store_true", "action": "store_true",