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
13 has_certifi
= bool(certifi
)
14 has_websockets
= bool(websockets
)
17 def load_plugins(name
, suffix
, namespace
):
18 from ..plugins
import load_plugins
19 ret
= load_plugins(name
, suffix
)
24 def traverse_dict(dictn
, keys
, casesense
=True):
25 return traverse_obj(dictn
, keys
, casesense
=casesense
, is_user_input
=True, traverse_string
=True)
28 def decode_base(value
, digits
):
29 return decode_base_n(value
, table
=digits
)
33 """ Returns the platform name as a str """
34 return platform
.platform()
37 def get_subprocess_encoding():
38 if sys
.platform
== 'win32' and sys
.getwindowsversion()[0] >= 5:
39 # For subprocess calls, encode with locale encoding
40 # Refer to http://stackoverflow.com/a/9951851/35070
41 encoding
= preferredencoding()
43 encoding
= sys
.getfilesystemencoding()
50 # Based on png2str() written by @gdkchan and improved by @yokrysty
51 # Originally posted at https://github.com/ytdl-org/youtube-dl/issues/9706
52 def decode_png(png_data
):
53 # Reference: https://www.w3.org/TR/PNG/
56 if png_data
[:8] != b
'\x89PNG\x0d\x0a\x1a\x0a' or header
[4:8] != b
'IHDR':
57 raise OSError('Not a valid PNG file.')
59 int_map
= {1: '>B', 2: '>H', 4: '>I'}
60 unpack_integer
= lambda x
: struct
.unpack(int_map
[len(x
)], x
)[0]
65 length
= unpack_integer(header
[:4])
68 chunk_type
= header
[:4]
71 chunk_data
= header
[:length
]
72 header
= header
[length
:]
74 header
= header
[4:] # Skip CRC
82 ihdr
= chunks
[0]['data']
84 width
= unpack_integer(ihdr
[:4])
85 height
= unpack_integer(ihdr
[4:8])
90 if chunk
['type'] == b
'IDAT':
94 raise OSError('Unable to read PNG data.')
96 decompressed_data
= bytearray(zlib
.decompress(idat
))
106 for y
in range(height
):
107 basePos
= y
* (1 + stride
)
108 filter_type
= decompressed_data
[basePos
]
112 pixels
.append(current_row
)
114 for x
in range(stride
):
115 color
= decompressed_data
[1 + basePos
+ x
]
116 basex
= y
* stride
+ x
121 left
= _get_pixel(basex
- 3)
123 up
= _get_pixel(basex
- stride
)
125 if filter_type
== 1: # Sub
126 color
= (color
+ left
) & 0xff
127 elif filter_type
== 2: # Up
128 color
= (color
+ up
) & 0xff
129 elif filter_type
== 3: # Average
130 color
= (color
+ ((left
+ up
) >> 1)) & 0xff
131 elif filter_type
== 4: # Paeth
137 c
= _get_pixel(basex
- stride
- 3)
145 if pa
<= pb
and pa
<= pc
:
146 color
= (color
+ a
) & 0xff
148 color
= (color
+ b
) & 0xff
150 color
= (color
+ c
) & 0xff
152 current_row
.append(color
)
154 return width
, height
, pixels
157 def register_socks_protocols():
158 # "Register" SOCKS protocols
159 # In Python < 2.6.5, urlsplit() suffers from bug https://bugs.python.org/issue7904
160 # URLs with protocols not in urlparse.uses_netloc are not handled correctly
161 for scheme
in ('socks', 'socks4', 'socks4a', 'socks5'):
162 if scheme
not in urllib
.parse
.uses_netloc
:
163 urllib
.parse
.uses_netloc
.append(scheme
)
166 def handle_youtubedl_headers(headers
):
167 filtered_headers
= headers
169 if 'Youtubedl-no-compression' in filtered_headers
:
170 filtered_headers
= {k: v for k, v in filtered_headers.items() if k.lower() != 'accept-encoding'}
171 del filtered_headers
['Youtubedl-no-compression']
173 return filtered_headers