From c78a37b115e4292419f826401acdd811f2350fa1 Mon Sep 17 00:00:00 2001 From: Chris Howell Date: Wed, 8 Jul 2020 15:23:10 -0700 Subject: [PATCH] Update _cli.py Add formats factory instance that uses the `parsed_args.format` arg to retrieve the formatter class, defaults to JSON if not passed or if given a bad/mistyped name. Might consider error in the case of a bad name given. Shouldn't be too difficult to add that ability if its wanted. --- youtube_transcript_api/_cli.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/youtube_transcript_api/_cli.py b/youtube_transcript_api/_cli.py index 405d6e1..0fdb8e5 100644 --- a/youtube_transcript_api/_cli.py +++ b/youtube_transcript_api/_cli.py @@ -1,10 +1,9 @@ import json -import pprint - import argparse from ._api import YouTubeTranscriptApi +from .formatters import formats class YouTubeTranscriptCli(): @@ -32,9 +31,11 @@ class YouTubeTranscriptCli(): except Exception as exception: exceptions.append(exception) - return '\n\n'.join( - [str(exception) for exception in exceptions] - + ([json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)] if transcripts else []) + Formatter = formats.get_formatter(parsed_args.format) + results = Formatter.format(transcripts) + + return ''.join( + [str(exception) for exception in exceptions] + results ) def _fetch_transcript(self, parsed_args, proxies, cookies, video_id): @@ -98,11 +99,9 @@ class YouTubeTranscriptCli(): help='If this flag is set transcripts which have been manually created will not be retrieved.', ) parser.add_argument( - '--json', - action='store_const', - const=True, - default=False, - help='If this flag is set the output will be JSON formatted.', + '--format', + default=None, + help="Use this flag to set which parser format to use, default is 'json'", ) parser.add_argument( '--translate',