aboutsummaryrefslogtreecommitdiff
path: root/devscripts/cli_to_api.py
blob: 9fb1d2ba8437b5d23215ade6e8cbd453fc811c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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([])

    def neq_opt(a, b):
        if a == b:
            return False
        if a is None and repr(type(object)).endswith(".utils.DateRange'>"):
            return '0001-01-01 - 9999-12-31' != '{0}'.format(b)
        return a != b

    diff = dict((k, v) for k, v in parsed_options(opts).items() if neq_opt(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 PrettyPrinter

    pprint = PrettyPrinter()
    super_format = pprint.format

    def format(object, context, maxlevels, level):
        if repr(type(object)).endswith(".utils.DateRange'>"):
            return '{0}: {1}>'.format(repr(object)[:-2], object), True, False
        return super_format(object, context, maxlevels, level)

    pprint.format = format

    pprint.pprint(cli_to_api(*sys.argv))


if __name__ == '__main__':
    main()