]>
Commit | Line | Data |
---|---|---|
1 | """No longer used and new code should not use. Exists only for API compat.""" | |
2 | ||
3 | import platform | |
4 | import struct | |
5 | import sys | |
6 | import urllib.parse | |
7 | import zlib | |
8 | ||
9 | from ._utils import Popen, decode_base_n, preferredencoding | |
10 | from .traversal import traverse_obj | |
11 | from ..dependencies import certifi, websockets | |
12 | ||
13 | # isort: split | |
14 | from ..cookies import YoutubeDLCookieJar # noqa: F401 | |
15 | ||
16 | has_certifi = bool(certifi) | |
17 | has_websockets = bool(websockets) | |
18 | ||
19 | ||
20 | def load_plugins(name, suffix, namespace): | |
21 | from ..plugins import load_plugins | |
22 | ret = load_plugins(name, suffix) | |
23 | namespace.update(ret) | |
24 | return ret | |
25 | ||
26 | ||
27 | def traverse_dict(dictn, keys, casesense=True): | |
28 | return traverse_obj(dictn, keys, casesense=casesense, is_user_input=True, traverse_string=True) | |
29 | ||
30 | ||
31 | def decode_base(value, digits): | |
32 | return decode_base_n(value, table=digits) | |
33 | ||
34 | ||
35 | def platform_name(): | |
36 | """ Returns the platform name as a str """ | |
37 | return platform.platform() | |
38 | ||
39 | ||
40 | def get_subprocess_encoding(): | |
41 | if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5: | |
42 | # For subprocess calls, encode with locale encoding | |
43 | # Refer to http://stackoverflow.com/a/9951851/35070 | |
44 | encoding = preferredencoding() | |
45 | else: | |
46 | encoding = sys.getfilesystemencoding() | |
47 | if encoding is None: | |
48 | encoding = 'utf-8' | |
49 | return encoding | |
50 | ||
51 | ||
52 | # UNUSED | |
53 | # Based on png2str() written by @gdkchan and improved by @yokrysty | |
54 | # Originally posted at https://github.com/ytdl-org/youtube-dl/issues/9706 | |
55 | def decode_png(png_data): | |
56 | # Reference: https://www.w3.org/TR/PNG/ | |
57 | header = png_data[8:] | |
58 | ||
59 | if png_data[:8] != b'\x89PNG\x0d\x0a\x1a\x0a' or header[4:8] != b'IHDR': | |
60 | raise OSError('Not a valid PNG file.') | |
61 | ||
62 | int_map = {1: '>B', 2: '>H', 4: '>I'} | |
63 | unpack_integer = lambda x: struct.unpack(int_map[len(x)], x)[0] | |
64 | ||
65 | chunks = [] | |
66 | ||
67 | while header: | |
68 | length = unpack_integer(header[:4]) | |
69 | header = header[4:] | |
70 | ||
71 | chunk_type = header[:4] | |
72 | header = header[4:] | |
73 | ||
74 | chunk_data = header[:length] | |
75 | header = header[length:] | |
76 | ||
77 | header = header[4:] # Skip CRC | |
78 | ||
79 | chunks.append({ | |
80 | 'type': chunk_type, | |
81 | 'length': length, | |
82 | 'data': chunk_data | |
83 | }) | |
84 | ||
85 | ihdr = chunks[0]['data'] | |
86 | ||
87 | width = unpack_integer(ihdr[:4]) | |
88 | height = unpack_integer(ihdr[4:8]) | |
89 | ||
90 | idat = b'' | |
91 | ||
92 | for chunk in chunks: | |
93 | if chunk['type'] == b'IDAT': | |
94 | idat += chunk['data'] | |
95 | ||
96 | if not idat: | |
97 | raise OSError('Unable to read PNG data.') | |
98 | ||
99 | decompressed_data = bytearray(zlib.decompress(idat)) | |
100 | ||
101 | stride = width * 3 | |
102 | pixels = [] | |
103 | ||
104 | def _get_pixel(idx): | |
105 | x = idx % stride | |
106 | y = idx // stride | |
107 | return pixels[y][x] | |
108 | ||
109 | for y in range(height): | |
110 | basePos = y * (1 + stride) | |
111 | filter_type = decompressed_data[basePos] | |
112 | ||
113 | current_row = [] | |
114 | ||
115 | pixels.append(current_row) | |
116 | ||
117 | for x in range(stride): | |
118 | color = decompressed_data[1 + basePos + x] | |
119 | basex = y * stride + x | |
120 | left = 0 | |
121 | up = 0 | |
122 | ||
123 | if x > 2: | |
124 | left = _get_pixel(basex - 3) | |
125 | if y > 0: | |
126 | up = _get_pixel(basex - stride) | |
127 | ||
128 | if filter_type == 1: # Sub | |
129 | color = (color + left) & 0xff | |
130 | elif filter_type == 2: # Up | |
131 | color = (color + up) & 0xff | |
132 | elif filter_type == 3: # Average | |
133 | color = (color + ((left + up) >> 1)) & 0xff | |
134 | elif filter_type == 4: # Paeth | |
135 | a = left | |
136 | b = up | |
137 | c = 0 | |
138 | ||
139 | if x > 2 and y > 0: | |
140 | c = _get_pixel(basex - stride - 3) | |
141 | ||
142 | p = a + b - c | |
143 | ||
144 | pa = abs(p - a) | |
145 | pb = abs(p - b) | |
146 | pc = abs(p - c) | |
147 | ||
148 | if pa <= pb and pa <= pc: | |
149 | color = (color + a) & 0xff | |
150 | elif pb <= pc: | |
151 | color = (color + b) & 0xff | |
152 | else: | |
153 | color = (color + c) & 0xff | |
154 | ||
155 | current_row.append(color) | |
156 | ||
157 | return width, height, pixels | |
158 | ||
159 | ||
160 | def register_socks_protocols(): | |
161 | # "Register" SOCKS protocols | |
162 | # In Python < 2.6.5, urlsplit() suffers from bug https://bugs.python.org/issue7904 | |
163 | # URLs with protocols not in urlparse.uses_netloc are not handled correctly | |
164 | for scheme in ('socks', 'socks4', 'socks4a', 'socks5'): | |
165 | if scheme not in urllib.parse.uses_netloc: | |
166 | urllib.parse.uses_netloc.append(scheme) | |
167 | ||
168 | ||
169 | def handle_youtubedl_headers(headers): | |
170 | filtered_headers = headers | |
171 | ||
172 | if 'Youtubedl-no-compression' in filtered_headers: | |
173 | filtered_headers = {k: v for k, v in filtered_headers.items() if k.lower() != 'accept-encoding'} | |
174 | del filtered_headers['Youtubedl-no-compression'] | |
175 | ||
176 | return filtered_headers | |
177 | ||
178 | ||
179 | def process_communicate_or_kill(p, *args, **kwargs): | |
180 | return Popen.communicate_or_kill(p, *args, **kwargs) |