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.
This commit is contained in:
parent
b4592043dc
commit
c78a37b115
|
@ -1,10 +1,9 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import pprint
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
from ._api import YouTubeTranscriptApi
|
from ._api import YouTubeTranscriptApi
|
||||||
|
from .formatters import formats
|
||||||
|
|
||||||
|
|
||||||
class YouTubeTranscriptCli():
|
class YouTubeTranscriptCli():
|
||||||
|
@ -32,9 +31,11 @@ class YouTubeTranscriptCli():
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
exceptions.append(exception)
|
exceptions.append(exception)
|
||||||
|
|
||||||
return '\n\n'.join(
|
Formatter = formats.get_formatter(parsed_args.format)
|
||||||
[str(exception) for exception in exceptions]
|
results = Formatter.format(transcripts)
|
||||||
+ ([json.dumps(transcripts) if parsed_args.json else pprint.pformat(transcripts)] if transcripts else [])
|
|
||||||
|
return ''.join(
|
||||||
|
[str(exception) for exception in exceptions] + results
|
||||||
)
|
)
|
||||||
|
|
||||||
def _fetch_transcript(self, parsed_args, proxies, cookies, video_id):
|
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.',
|
help='If this flag is set transcripts which have been manually created will not be retrieved.',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--json',
|
'--format',
|
||||||
action='store_const',
|
default=None,
|
||||||
const=True,
|
help="Use this flag to set which parser format to use, default is 'json'",
|
||||||
default=False,
|
|
||||||
help='If this flag is set the output will be JSON formatted.',
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--translate',
|
'--translate',
|
||||||
|
|
Loading…
Reference in New Issue