1 """No longer used and new code should not use. Exists only for API compat."""
9 from ._utils
import decode_base_n
, preferredencoding
10 from .traversal
import traverse_obj
11 from ..dependencies
import certifi
, websockets
14 from ..cookies
import YoutubeDLCookieJar
# noqa: F401
16 has_certifi
= bool(certifi
)
17 has_websockets
= bool(websockets
)
20 def load_plugins(name
, suffix
, namespace
):
21 from ..plugins
import load_plugins
22 ret
= load_plugins(name
, suffix
)
27 def traverse_dict(dictn
, keys
, casesense
=True):
28 return traverse_obj(dictn
, keys
, casesense
=casesense
, is_user_input
=True, traverse_string
=True)
31 def decode_base(value
, digits
):
32 return decode_base_n(value
, table
=digits
)
36 """ Returns the platform name as a str """
37 return platform
.platform()
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()
46 encoding
= sys
.getfilesystemencoding()
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/
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.')
62 int_map
= {1: '>B', 2: '>H', 4: '>I'}
63 unpack_integer
= lambda x
: struct
.unpack(int_map
[len(x
)], x
)[0]
68 length
= unpack_integer(header
[:4])
71 chunk_type
= header
[:4]
74 chunk_data
= header
[:length
]
75 header
= header
[length
:]
77 header
= header
[4:] # Skip CRC
85 ihdr
= chunks
[0]['data']
87 width
= unpack_integer(ihdr
[:4])
88 height
= unpack_integer(ihdr
[4:8])
93 if chunk
['type'] == b
'IDAT':
97 raise OSError('Unable to read PNG data.')
99 decompressed_data
= bytearray(zlib
.decompress(idat
))
109 for y
in range(height
):
110 basePos
= y
* (1 + stride
)
111 filter_type
= decompressed_data
[basePos
]
115 pixels
.append(current_row
)
117 for x
in range(stride
):
118 color
= decompressed_data
[1 + basePos
+ x
]
119 basex
= y
* stride
+ x
124 left
= _get_pixel(basex
- 3)
126 up
= _get_pixel(basex
- stride
)
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
140 c
= _get_pixel(basex
- stride
- 3)
148 if pa
<= pb
and pa
<= pc
:
149 color
= (color
+ a
) & 0xff
151 color
= (color
+ b
) & 0xff
153 color
= (color
+ c
) & 0xff
155 current_row
.append(color
)
157 return width
, height
, pixels
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
)
169 def handle_youtubedl_headers(headers
):
170 filtered_headers
= headers
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']
176 return filtered_headers