Skip to content

Commit 70ff013

Browse files
committed
[devscripts] Add a hack to convert command-line options to API options
1 parent e8de54b commit 70ff013

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

devscripts/cli_to_api.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
from __future__ import unicode_literals
5+
6+
"""
7+
This script displays the API parameters corresponding to a yt-dl command line
8+
9+
Example:
10+
$ ./cli_to_api.py -f best
11+
{u'format': 'best'}
12+
$
13+
"""
14+
15+
# Allow direct execution
16+
import os
17+
import sys
18+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
19+
20+
import youtube_dl
21+
from types import MethodType
22+
23+
24+
def cli_to_api(*opts):
25+
YDL = youtube_dl.YoutubeDL
26+
27+
# to extract the parsed options, break out of YoutubeDL instantiation
28+
29+
# return options via this Exception
30+
class ParseYTDLResult(Exception):
31+
def __init__(self, result):
32+
super(ParseYTDLResult, self).__init__('result')
33+
self.opts = result
34+
35+
# replacement constructor that raises ParseYTDLResult
36+
def ytdl_init(ydl, ydl_opts):
37+
super(YDL, ydl).__init__(ydl_opts)
38+
raise ParseYTDLResult(ydl_opts)
39+
40+
# patch in the constructor
41+
YDL.__init__ = MethodType(ytdl_init, YDL)
42+
43+
# core parser
44+
def parsed_options(argv):
45+
try:
46+
youtube_dl._real_main(list(argv))
47+
except ParseYTDLResult as result:
48+
return result.opts
49+
50+
# from https://github.com/yt-dlp/yt-dlp/issues/5859#issuecomment-1363938900
51+
default = parsed_options([])
52+
diff = dict((k, v) for k, v in parsed_options(opts).items() if default[k] != v)
53+
if 'postprocessors' in diff:
54+
diff['postprocessors'] = [pp for pp in diff['postprocessors'] if pp not in default['postprocessors']]
55+
return diff
56+
57+
58+
def main():
59+
from pprint import pprint
60+
pprint(cli_to_api(*sys.argv))
61+
62+
63+
if __name__ == '__main__':
64+
main()

0 commit comments

Comments
 (0)