diff options
author | dirkf <fieldhouse@gmx.net> | 2023-03-14 00:58:59 +0000 |
---|---|---|
committer | dirkf <fieldhouse@gmx.net> | 2023-03-14 16:23:20 +0000 |
commit | 70ff01391068c98b4377c5cc17a8d00d5645e734 (patch) | |
tree | 0266ef37c0b31effc5256f0031ce26cc28ff387a /devscripts | |
parent | e8de54bce50f6f77a4d7e8e80675f7003d5bf630 (diff) |
[devscripts] Add a hack to convert command-line options to API options
Diffstat (limited to 'devscripts')
-rwxr-xr-x | devscripts/cli_to_api.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/devscripts/cli_to_api.py b/devscripts/cli_to_api.py new file mode 100755 index 000000000..2f4d6a458 --- /dev/null +++ b/devscripts/cli_to_api.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# coding: utf-8 + +from __future__ import unicode_literals + +""" +This script displays the API parameters corresponding to a yt-dl command line + +Example: +$ ./cli_to_api.py -f best +{u'format': 'best'} +$ +""" + +# Allow direct execution +import os +import sys +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import youtube_dl +from types import MethodType + + +def cli_to_api(*opts): + YDL = youtube_dl.YoutubeDL + + # to extract the parsed options, break out of YoutubeDL instantiation + + # return options via this Exception + class ParseYTDLResult(Exception): + def __init__(self, result): + super(ParseYTDLResult, self).__init__('result') + self.opts = result + + # replacement constructor that raises ParseYTDLResult + def ytdl_init(ydl, ydl_opts): + super(YDL, ydl).__init__(ydl_opts) + raise ParseYTDLResult(ydl_opts) + + # patch in the constructor + YDL.__init__ = MethodType(ytdl_init, YDL) + + # core parser + def parsed_options(argv): + try: + youtube_dl._real_main(list(argv)) + except ParseYTDLResult as result: + return result.opts + + # from https://github.com/yt-dlp/yt-dlp/issues/5859#issuecomment-1363938900 + default = parsed_options([]) + diff = dict((k, v) for k, v in parsed_options(opts).items() if default[k] != v) + if 'postprocessors' in diff: + diff['postprocessors'] = [pp for pp in diff['postprocessors'] if pp not in default['postprocessors']] + return diff + + +def main(): + from pprint import pprint + pprint(cli_to_api(*sys.argv)) + + +if __name__ == '__main__': + main() |