]> jfr.im git - yt-dlp.git/blob - devscripts/make_lazy_extractors.py
[youtube] Add `mobile_web` client (#557)
[yt-dlp.git] / devscripts / make_lazy_extractors.py
1 #!/usr/bin/env python3
2 from __future__ import unicode_literals, print_function
3
4 from inspect import getsource
5 import io
6 import os
7 from os.path import dirname as dirn
8 import sys
9
10 print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
11
12 sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
13
14 lazy_extractors_filename = sys.argv[1]
15 if os.path.exists(lazy_extractors_filename):
16 os.remove(lazy_extractors_filename)
17
18 # Block plugins from loading
19 os.rename('ytdlp_plugins', 'ytdlp_plugins_blocked')
20
21 from yt_dlp.extractor import _ALL_CLASSES
22 from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
23
24 os.rename('ytdlp_plugins_blocked', 'ytdlp_plugins')
25
26 with open('devscripts/lazy_load_template.py', 'rt') as f:
27 module_template = f.read()
28
29 module_contents = [
30 module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
31 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
32
33 ie_template = '''
34 class {name}({bases}):
35 _VALID_URL = {valid_url!r}
36 _module = '{module}'
37 '''
38
39 make_valid_template = '''
40 @classmethod
41 def _make_valid_url(cls):
42 return {valid_url!r}
43 '''
44
45
46 def get_base_name(base):
47 if base is InfoExtractor:
48 return 'LazyLoadExtractor'
49 elif base is SearchInfoExtractor:
50 return 'LazyLoadSearchExtractor'
51 else:
52 return base.__name__
53
54
55 def build_lazy_ie(ie, name):
56 valid_url = getattr(ie, '_VALID_URL', None)
57 s = ie_template.format(
58 name=name,
59 bases=', '.join(map(get_base_name, ie.__bases__)),
60 valid_url=valid_url,
61 module=ie.__module__)
62 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
63 s += '\n' + getsource(ie.suitable)
64 if hasattr(ie, '_make_valid_url'):
65 # search extractors
66 s += make_valid_template.format(valid_url=ie._make_valid_url())
67 return s
68
69
70 # find the correct sorting and add the required base classes so that subclasses
71 # can be correctly created
72 classes = _ALL_CLASSES[:-1]
73 ordered_cls = []
74 while classes:
75 for c in classes[:]:
76 bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
77 stop = False
78 for b in bases:
79 if b not in classes and b not in ordered_cls:
80 if b.__name__ == 'GenericIE':
81 exit()
82 classes.insert(0, b)
83 stop = True
84 if stop:
85 break
86 if all(b in ordered_cls for b in bases):
87 ordered_cls.append(c)
88 classes.remove(c)
89 break
90 ordered_cls.append(_ALL_CLASSES[-1])
91
92 names = []
93 for ie in ordered_cls:
94 name = ie.__name__
95 src = build_lazy_ie(ie, name)
96 module_contents.append(src)
97 if ie in _ALL_CLASSES:
98 names.append(name)
99
100 module_contents.append(
101 '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
102
103 module_src = '\n'.join(module_contents) + '\n'
104
105 with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
106 f.write(module_src)