4 from __future__
import unicode_literals
41 import xml
.etree
.ElementTree
46 compat_HTMLParseError
,
53 compat_ctypes_WINFUNCTYPE
,
54 compat_etree_fromstring
,
57 compat_html_entities_html5
,
71 compat_urllib_parse_urlencode
,
72 compat_urllib_parse_urlparse
,
73 compat_urllib_parse_urlunparse
,
74 compat_urllib_parse_quote
,
75 compat_urllib_parse_quote_plus
,
76 compat_urllib_parse_unquote_plus
,
77 compat_urllib_request
,
95 def register_socks_protocols():
96 # "Register" SOCKS protocols
97 # In Python < 2.6.5, urlsplit() suffers from bug https://bugs.python.org/issue7904
98 # URLs with protocols not in urlparse.uses_netloc are not handled correctly
99 for scheme
in ('socks', 'socks4', 'socks4a', 'socks5'):
100 if scheme
not in compat_urlparse
.uses_netloc
:
101 compat_urlparse
.uses_netloc
.append(scheme
)
104 # This is not clearly defined otherwise
105 compiled_regex_type
= type(re
.compile(''))
108 def random_user_agent():
109 _USER_AGENT_TPL
= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36'
150 return _USER_AGENT_TPL
% random
.choice(_CHROME_VERSIONS
)
153 SUPPORTED_ENCODINGS
= [
157 SUPPORTED_ENCODINGS
.append('br')
160 'User-Agent': random_user_agent(),
161 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
162 'Accept-Language': 'en-us,en;q=0.5',
163 'Sec-Fetch-Mode': 'navigate',
168 'Safari': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27',
172 NO_DEFAULT
= object()
174 ENGLISH_MONTH_NAMES
= [
175 'January', 'February', 'March', 'April', 'May', 'June',
176 'July', 'August', 'September', 'October', 'November', 'December']
179 'en': ENGLISH_MONTH_NAMES
,
181 'janvier', 'février', 'mars', 'avril', 'mai', 'juin',
182 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
186 'mp4', 'm4a', 'm4p', 'm4b', 'm4r', 'm4v', 'aac',
187 'flv', 'f4v', 'f4a', 'f4b',
188 'webm', 'ogg', 'ogv', 'oga', 'ogx', 'spx', 'opus',
189 'mkv', 'mka', 'mk3d',
198 'f4f', 'f4m', 'm3u8', 'smil')
200 # needed for sanitizing filenames in restricted mode
201 ACCENT_CHARS
= dict(zip('ÂÃÄÀÁÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖŐØŒÙÚÛÜŰÝÞßàáâãäåæçèéêëìíîïðñòóôõöőøœùúûüűýþÿ',
202 itertools
.chain('AAAAAA', ['AE'], 'CEEEEIIIIDNOOOOOOO', ['OE'], 'UUUUUY', ['TH', 'ss'],
203 'aaaaaa', ['ae'], 'ceeeeiiiionooooooo', ['oe'], 'uuuuuy', ['th'], 'y')))
233 '%Y-%m-%d %H:%M:%S.%f',
234 '%Y-%m-%d %H:%M:%S:%f',
237 '%Y-%m-%dT%H:%M:%SZ',
238 '%Y-%m-%dT%H:%M:%S.%fZ',
239 '%Y-%m-%dT%H:%M:%S.%f0Z',
241 '%Y-%m-%dT%H:%M:%S.%f',
244 '%b %d %Y at %H:%M:%S',
246 '%B %d %Y at %H:%M:%S',
250 DATE_FORMATS_DAY_FIRST
= list(DATE_FORMATS
)
251 DATE_FORMATS_DAY_FIRST
.extend([
260 DATE_FORMATS_MONTH_FIRST
= list(DATE_FORMATS
)
261 DATE_FORMATS_MONTH_FIRST
.extend([
269 PACKED_CODES_RE
= r
"}\('(.+)',(\d+),(\d+),'([^']+)'\.split\('\|'\)"
270 JSON_LD_RE
= r
'(?is)<script[^>]+type=(["\']?
)application
/ld\
+json\
1[^
>]*>(?P
<json_ld
>.+?
)</script
>'
273 def preferredencoding():
274 """Get preferred encoding.
276 Returns the best encoding scheme for the system, based on
277 locale.getpreferredencoding() and some further tweaks.
280 pref = locale.getpreferredencoding()
288 def write_json_file(obj, fn):
289 """ Encode obj as JSON and write it to fn, atomically if possible """
291 fn = encodeFilename(fn)
292 if sys.version_info < (3, 0) and sys.platform != 'win32
':
293 encoding = get_filesystem_encoding()
294 # os.path.basename returns a bytes object, but NamedTemporaryFile
295 # will fail if the filename contains non ascii characters unless we
296 # use a unicode object
297 path_basename = lambda f: os.path.basename(fn).decode(encoding)
298 # the same for os.path.dirname
299 path_dirname = lambda f: os.path.dirname(fn).decode(encoding)
301 path_basename = os.path.basename
302 path_dirname = os.path.dirname
306 'prefix
': path_basename(fn) + '.',
307 'dir': path_dirname(fn),
311 # In Python 2.x, json.dump expects a bytestream.
312 # In Python 3.x, it writes to a character stream
313 if sys.version_info < (3, 0):
321 tf = tempfile.NamedTemporaryFile(**compat_kwargs(args))
325 json.dump(obj, tf, ensure_ascii=False)
326 if sys.platform == 'win32
':
327 # Need to remove existing file on Windows, else os.rename raises
328 # WindowsError or FileExistsError.
336 os.chmod(tf.name, 0o666 & ~mask)
339 os.rename(tf.name, fn)
348 if sys.version_info >= (2, 7):
349 def find_xpath_attr(node, xpath, key, val=None):
350 """ Find the xpath xpath[@key=val] """
351 assert re.match(r'^
[a
-zA
-Z_
-]+$
', key)
352 expr = xpath + ('[@%s]' % key if val is None else "[@%s='%s']" % (key, val))
353 return node.find(expr)
355 def find_xpath_attr(node, xpath, key, val=None):
356 for f in node.findall(compat_xpath(xpath)):
357 if key not in f.attrib:
359 if val is None or f.attrib.get(key) == val:
363 # On python2.6 the xml.etree.ElementTree.Element methods don't support
364 # the namespace parameter
367 def xpath_with_ns(path
, ns_map
):
368 components
= [c
.split(':') for c
in path
.split('/')]
372 replaced
.append(c
[0])
375 replaced
.append('{%s}%s' % (ns_map
[ns
], tag
))
376 return '/'.join(replaced
)
379 def xpath_element(node
, xpath
, name
=None, fatal
=False, default
=NO_DEFAULT
):
380 def _find_xpath(xpath
):
381 return node
.find(compat_xpath(xpath
))
383 if isinstance(xpath
, (str, compat_str
)):
384 n
= _find_xpath(xpath
)
392 if default
is not NO_DEFAULT
:
395 name
= xpath
if name
is None else name
396 raise ExtractorError('Could not find XML element %s' % name
)
402 def xpath_text(node
, xpath
, name
=None, fatal
=False, default
=NO_DEFAULT
):
403 n
= xpath_element(node
, xpath
, name
, fatal
=fatal
, default
=default
)
404 if n
is None or n
== default
:
407 if default
is not NO_DEFAULT
:
410 name
= xpath
if name
is None else name
411 raise ExtractorError('Could not find XML element\'s text %s' % name
)
417 def xpath_attr(node
, xpath
, key
, name
=None, fatal
=False, default
=NO_DEFAULT
):
418 n
= find_xpath_attr(node
, xpath
, key
)
420 if default
is not NO_DEFAULT
:
423 name
= '%s[@%s]' % (xpath
, key
) if name
is None else name
424 raise ExtractorError('Could not find XML attribute %s' % name
)
430 def get_element_by_id(id, html
):
431 """Return the content of the tag with the specified ID in the passed HTML document"""
432 return get_element_by_attribute('id', id, html
)
435 def get_element_html_by_id(id, html
):
436 """Return the html of the tag with the specified ID in the passed HTML document"""
437 return get_element_html_by_attribute('id', id, html
)
440 def get_element_by_class(class_name
, html
):
441 """Return the content of the first tag with the specified class in the passed HTML document"""
442 retval
= get_elements_by_class(class_name
, html
)
443 return retval
[0] if retval
else None
446 def get_element_html_by_class(class_name
, html
):
447 """Return the html of the first tag with the specified class in the passed HTML document"""
448 retval
= get_elements_html_by_class(class_name
, html
)
449 return retval
[0] if retval
else None
452 def get_element_by_attribute(attribute
, value
, html
, escape_value
=True):
453 retval
= get_elements_by_attribute(attribute
, value
, html
, escape_value
)
454 return retval
[0] if retval
else None
457 def get_element_html_by_attribute(attribute
, value
, html
, escape_value
=True):
458 retval
= get_elements_html_by_attribute(attribute
, value
, html
, escape_value
)
459 return retval
[0] if retval
else None
462 def get_elements_by_class(class_name
, html
):
463 """Return the content of all tags with the specified class in the passed HTML document as a list"""
464 return get_elements_by_attribute(
465 'class', r
'[^\'"]*\b%s\b[^\'"]*' % re.escape(class_name),
466 html, escape_value=False)
469 def get_elements_html_by_class(class_name, html):
470 """Return the html of all tags with the specified class in the passed HTML document as a list"""
471 return get_elements_html_by_attribute(
472 'class', r'[^
\'"]*\b%s\b[^\'"]*' % re.escape(class_name),
473 html, escape_value=False)
476 def get_elements_by_attribute(*args, **kwargs):
477 """Return the content of the tag with the specified attribute in the passed HTML document"""
478 return [content for content, _ in get_elements_text_and_html_by_attribute(*args, **kwargs)]
481 def get_elements_html_by_attribute(*args, **kwargs):
482 """Return the html of the tag with the specified attribute in the passed HTML document"""
483 return [whole for _, whole in get_elements_text_and_html_by_attribute(*args, **kwargs)]
486 def get_elements_text_and_html_by_attribute(attribute, value, html, escape_value=True):
488 Return the text (content) and the html (whole) of the tag with the specified
489 attribute in the passed HTML document
492 value_quote_optional = '' if re.match(r'''[\s"'`
=<>]''', value) else '?'
494 value = re.escape(value) if escape_value else value
496 partial_element_re = r'''(?x
)
497 <(?P
<tag
>[a
-zA
-Z0
-9:._-]+)
498 (?
:\
s(?
:[^
>"']|"[^
"]*"|
'[^']*')*)?
499 \s%(attribute)s\s*=\s*(?P<_q>['"]%(vqo)s)(?-x:%(value)s)(?P=_q)
500 ''' % {'attribute': re.escape(attribute), 'value': value, 'vqo': value_quote_optional}
502 for m in re.finditer(partial_element_re, html):
503 content, whole = get_element_text_and_html_by_tag(m.group('tag'), html[m.start():])
506 unescapeHTML(re.sub(r'^(?P<q>["\'])(?P
<content
>.*)(?P
=q
)$
', r'\g
<content
>', content, flags=re.DOTALL)),
511 class HTMLBreakOnClosingTagParser(compat_HTMLParser):
513 HTML parser which raises HTMLBreakOnClosingTagException upon reaching the
514 closing tag for the first opening tag it has encountered, and can be used
518 class HTMLBreakOnClosingTagException(Exception):
522 self.tagstack = collections.deque()
523 compat_HTMLParser.__init__(self)
528 def __exit__(self, *_):
532 # handle_endtag does not return upon raising HTMLBreakOnClosingTagException,
533 # so data remains buffered; we no longer have any interest in it, thus
534 # override this method to discard it
537 def handle_starttag(self, tag, _):
538 self.tagstack.append(tag)
540 def handle_endtag(self, tag):
541 if not self.tagstack:
542 raise compat_HTMLParseError('no tags
in the stack
')
544 inner_tag = self.tagstack.pop()
548 raise compat_HTMLParseError(f'matching opening tag
for closing {tag} tag
not found
')
549 if not self.tagstack:
550 raise self.HTMLBreakOnClosingTagException()
553 def get_element_text_and_html_by_tag(tag, html):
555 For the first element with the specified tag in the passed HTML document
556 return its' content (text
) and the whole
element (html
)
558 def find_or_raise(haystack, needle, exc):
560 return haystack.index(needle)
563 closing_tag = f'</{tag}>'
564 whole_start = find_or_raise(
565 html, f'<{tag}', compat_HTMLParseError(f'opening {tag} tag not found'))
566 content_start = find_or_raise(
567 html[whole_start:], '>', compat_HTMLParseError(f'malformed opening {tag} tag'))
568 content_start += whole_start + 1
569 with HTMLBreakOnClosingTagParser() as parser:
570 parser.feed(html[whole_start:content_start])
571 if not parser.tagstack or parser.tagstack[0] != tag:
572 raise compat_HTMLParseError(f'parser did not match opening {tag} tag')
573 offset = content_start
574 while offset < len(html):
575 next_closing_tag_start = find_or_raise(
576 html[offset:], closing_tag,
577 compat_HTMLParseError(f'closing {tag} tag not found'))
578 next_closing_tag_end = next_closing_tag_start + len(closing_tag)
580 parser.feed(html[offset:offset + next_closing_tag_end])
581 offset += next_closing_tag_end
582 except HTMLBreakOnClosingTagParser.HTMLBreakOnClosingTagException:
583 return html[content_start:offset + next_closing_tag_start], \
584 html[whole_start:offset + next_closing_tag_end]
585 raise compat_HTMLParseError('unexpected end of html')
588 class HTMLAttributeParser(compat_HTMLParser):
589 """Trivial HTML parser to gather the attributes
for a single element
"""
593 compat_HTMLParser.__init__(self)
595 def handle_starttag(self, tag, attrs):
596 self.attrs = dict(attrs)
599 class HTMLListAttrsParser(compat_HTMLParser):
600 """HTML parser to gather the attributes
for the elements of a
list"""
603 compat_HTMLParser.__init__(self)
607 def handle_starttag(self, tag, attrs):
608 if tag == 'li' and self._level == 0:
609 self.items.append(dict(attrs))
612 def handle_endtag(self, tag):
616 def extract_attributes(html_element):
617 """Given a string
for an HTML element such
as
619 a
="foo" B
="bar" c
="&98;az" d
=boz
620 empty
= noval entity
="&"
623 Decode
and return a dictionary of attributes
.
625 'a': 'foo', 'b': 'bar', c
: 'baz', d
: 'boz',
626 'empty': '', 'noval': None, 'entity': '&',
627 'sq': '"', 'dq': '\''
629 NB HTMLParser
is stricter
in Python
2.6 & 3.2 than
in later versions
,
630 but the cases
in the unit test will work
for all of
2.6, 2.7, 3.2-3.5.
632 parser = HTMLAttributeParser()
634 parser.feed(html_element)
636 # Older Python may throw HTMLParseError in case of malformed HTML
637 except compat_HTMLParseError:
642 def parse_list(webpage):
643 """Given a string
for an series of HTML
<li
> elements
,
644 return a dictionary of their attributes
"""
645 parser = HTMLListAttrsParser()
651 def clean_html(html):
652 """Clean an HTML snippet into a readable string
"""
654 if html is None: # Convenience for sanitizing descriptions etc.
657 html = re.sub(r'\s+', ' ', html)
658 html = re.sub(r'(?u)\s?<\s?br\s?/?\s?>\s?', '\n', html)
659 html = re.sub(r'(?u)<\s?/\s?p\s?>\s?<\s?p[^>]*>', '\n', html)
661 html = re.sub('<.*?>', '', html)
662 # Replace html entities
663 html = unescapeHTML(html)
667 def sanitize_open(filename, open_mode):
668 """Try to
open the given filename
, and slightly tweak it
if this fails
.
670 Attempts to
open the given filename
. If this fails
, it tries to change
671 the filename slightly
, step by step
, until it
's either able to open it
672 or it fails and raises a final exception, like the standard open()
675 It returns the tuple (stream, definitive_file_name).
679 if sys.platform == 'win32
':
681 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
682 return (sys.stdout.buffer if hasattr(sys.stdout, 'buffer') else sys.stdout, filename)
683 stream = locked_file(filename, open_mode, block=False).open()
684 return (stream, filename)
685 except (IOError, OSError) as err:
686 if err.errno in (errno.EACCES,):
689 # In case of error, try to remove win32 forbidden chars
690 alt_filename = sanitize_path(filename)
691 if alt_filename == filename:
694 # An exception here should be caught in the caller
695 stream = locked_file(filename, open_mode, block=False).open()
696 return (stream, alt_filename)
699 def timeconvert(timestr):
700 """Convert RFC 2822 defined time string into system timestamp"""
702 timetuple = email.utils.parsedate_tz(timestr)
703 if timetuple is not None:
704 timestamp = email.utils.mktime_tz(timetuple)
708 def sanitize_filename(s, restricted=False, is_id=False):
709 """Sanitizes a string so it could be used as part of a filename.
710 If restricted is set, use a stricter subset of allowed characters.
711 Set is_id if this is not an arbitrary string, but an ID that should be kept
714 def replace_insane(char):
715 if restricted and char in ACCENT_CHARS:
716 return ACCENT_CHARS[char]
717 elif not restricted and char == '\n':
719 elif char == '?
' or ord(char) < 32 or ord(char) == 127:
722 return '' if restricted else '\''
724 return '_-' if restricted else ' -'
725 elif char in '\\/|*<>':
727 if restricted and (char in '!&\'()[]{}$;`^,#' or char.isspace()):
729 if restricted and ord(char) > 127:
736 s = re.sub(r'[0-9]+(?::[0-9]+)+', lambda m: m.group(0).replace(':', '_'), s)
737 result = ''.join(map(replace_insane, s))
739 while '__' in result:
740 result = result.replace('__', '_')
741 result = result.strip('_')
742 # Common case of "Foreign band name
- English song title
"
743 if restricted and result.startswith('-_'):
745 if result.startswith('-'):
746 result = '_' + result[len('-'):]
747 result = result.lstrip('.')
753 def sanitize_path(s, force=False):
754 """Sanitizes and normalizes path on Windows"""
755 if sys.platform == 'win32':
757 drive_or_unc, _ = os.path.splitdrive(s)
758 if sys.version_info < (2, 7) and not drive_or_unc:
759 drive_or_unc, _ = os.path.splitunc(s)
765 norm_path = os.path.normpath(remove_start(s, drive_or_unc)).split(os.path.sep)
769 path_part if path_part in ['.', '..'] else re.sub(r'(?:[/<>:"\|
\\?\
*]|
[\s
.]$
)', '#', path_part)
770 for path_part
in norm_path
]
772 sanitized_path
.insert(0, drive_or_unc
+ os
.path
.sep
)
773 elif force
and s
[0] == os
.path
.sep
:
774 sanitized_path
.insert(0, os
.path
.sep
)
775 return os
.path
.join(*sanitized_path
)
778 def sanitize_url(url
):
779 # Prepend protocol-less URLs with `http:` scheme in order to mitigate
780 # the number of unwanted failures due to missing protocol
781 if url
.startswith('//'):
782 return 'http:%s' % url
783 # Fix some common typos seen so far
785 # https://github.com/ytdl-org/youtube-dl/issues/15649
786 (r
'^httpss://', r
'https://'),
787 # https://bx1.be/lives/direct-tv/
788 (r
'^rmtp([es]?)://', r
'rtmp\1://'),
790 for mistake
, fixup
in COMMON_TYPOS
:
791 if re
.match(mistake
, url
):
792 return re
.sub(mistake
, fixup
, url
)
796 def extract_basic_auth(url
):
797 parts
= compat_urlparse
.urlsplit(url
)
798 if parts
.username
is None:
800 url
= compat_urlparse
.urlunsplit(parts
._replace
(netloc
=(
801 parts
.hostname
if parts
.port
is None
802 else '%s:%d' % (parts
.hostname
, parts
.port
))))
803 auth_payload
= base64
.b64encode(
804 ('%s:%s' % (parts
.username
, parts
.password
or '')).encode('utf-8'))
805 return url
, 'Basic ' + auth_payload
.decode('utf-8')
808 def sanitized_Request(url
, *args
, **kwargs
):
809 url
, auth_header
= extract_basic_auth(escape_url(sanitize_url(url
)))
810 if auth_header
is not None:
811 headers
= args
[1] if len(args
) >= 2 else kwargs
.setdefault('headers', {})
812 headers
['Authorization'] = auth_header
813 return compat_urllib_request
.Request(url
, *args
, **kwargs
)
817 """Expand shell variables and ~"""
818 return os
.path
.expandvars(compat_expanduser(s
))
821 def orderedSet(iterable
):
822 """ Remove all duplicates from the input iterable """
830 def _htmlentity_transform(entity_with_semicolon
):
831 """Transforms an HTML entity to a character."""
832 entity
= entity_with_semicolon
[:-1]
834 # Known non-numeric HTML entity
835 if entity
in compat_html_entities
.name2codepoint
:
836 return compat_chr(compat_html_entities
.name2codepoint
[entity
])
838 # TODO: HTML5 allows entities without a semicolon. For example,
839 # 'Éric' should be decoded as 'Éric'.
840 if entity_with_semicolon
in compat_html_entities_html5
:
841 return compat_html_entities_html5
[entity_with_semicolon
]
843 mobj
= re
.match(r
'#(x[0-9a-fA-F]+|[0-9]+)', entity
)
845 numstr
= mobj
.group(1)
846 if numstr
.startswith('x'):
848 numstr
= '0%s' % numstr
851 # See https://github.com/ytdl-org/youtube-dl/issues/7518
853 return compat_chr(int(numstr
, base
))
857 # Unknown entity in name, return its literal representation
858 return '&%s;' % entity
864 assert type(s
) == compat_str
867 r
'&([^&;]+;)', lambda m
: _htmlentity_transform(m
.group(1)), s
)
870 def escapeHTML(text
):
873 .replace('&', '&')
874 .replace('<', '<')
875 .replace('>', '>')
876 .replace('"', '"')
877 .replace("'", ''')
881 def process_communicate_or_kill(p
, *args
, **kwargs
):
883 return p
.communicate(*args
, **kwargs
)
884 except BaseException
: # Including KeyboardInterrupt
890 class Popen(subprocess
.Popen
):
891 if sys
.platform
== 'win32':
892 _startupinfo
= subprocess
.STARTUPINFO()
893 _startupinfo
.dwFlags |
= subprocess
.STARTF_USESHOWWINDOW
897 def __init__(self
, *args
, **kwargs
):
898 super(Popen
, self
).__init
__(*args
, **kwargs
, startupinfo
=self
._startupinfo
)
900 def communicate_or_kill(self
, *args
, **kwargs
):
901 return process_communicate_or_kill(self
, *args
, **kwargs
)
904 def get_subprocess_encoding():
905 if sys
.platform
== 'win32' and sys
.getwindowsversion()[0] >= 5:
906 # For subprocess calls, encode with locale encoding
907 # Refer to http://stackoverflow.com/a/9951851/35070
908 encoding
= preferredencoding()
910 encoding
= sys
.getfilesystemencoding()
916 def encodeFilename(s
, for_subprocess
=False):
918 @param s The name of the file
921 assert type(s
) == compat_str
923 # Python 3 has a Unicode API
924 if sys
.version_info
>= (3, 0):
927 # Pass '' directly to use Unicode APIs on Windows 2000 and up
928 # (Detecting Windows NT 4 is tricky because 'major >= 4' would
929 # match Windows 9x series as well. Besides, NT 4 is obsolete.)
930 if not for_subprocess
and sys
.platform
== 'win32' and sys
.getwindowsversion()[0] >= 5:
933 # Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
934 if sys
.platform
.startswith('java'):
937 return s
.encode(get_subprocess_encoding(), 'ignore')
940 def decodeFilename(b
, for_subprocess
=False):
942 if sys
.version_info
>= (3, 0):
945 if not isinstance(b
, bytes):
948 return b
.decode(get_subprocess_encoding(), 'ignore')
951 def encodeArgument(s
):
952 if not isinstance(s
, compat_str
):
953 # Legacy code that uses byte strings
954 # Uncomment the following line after fixing all post processors
955 # assert False, 'Internal error: %r should be of type %r, is %r' % (s, compat_str, type(s))
956 s
= s
.decode('ascii')
957 return encodeFilename(s
, True)
960 def decodeArgument(b
):
961 return decodeFilename(b
, True)
964 def decodeOption(optval
):
967 if isinstance(optval
, bytes):
968 optval
= optval
.decode(preferredencoding())
970 assert isinstance(optval
, compat_str
)
974 _timetuple
= collections
.namedtuple('Time', ('hours', 'minutes', 'seconds', 'milliseconds'))
977 def timetuple_from_msec(msec
):
978 secs
, msec
= divmod(msec
, 1000)
979 mins
, secs
= divmod(secs
, 60)
980 hrs
, mins
= divmod(mins
, 60)
981 return _timetuple(hrs
, mins
, secs
, msec
)
984 def formatSeconds(secs
, delim
=':', msec
=False):
985 time
= timetuple_from_msec(secs
* 1000)
987 ret
= '%d%s%02d%s%02d' % (time
.hours
, delim
, time
.minutes
, delim
, time
.seconds
)
989 ret
= '%d%s%02d' % (time
.minutes
, delim
, time
.seconds
)
991 ret
= '%d' % time
.seconds
992 return '%s.%03d' % (ret
, time
.milliseconds
) if msec
else ret
995 def _ssl_load_windows_store_certs(ssl_context
, storename
):
996 # Code adapted from _load_windows_store_certs in https://github.com/python/cpython/blob/main/Lib/ssl.py
998 certs
= [cert
for cert
, encoding
, trust
in ssl
.enum_certificates(storename
)
999 if encoding
== 'x509_asn' and (
1000 trust
is True or ssl
.Purpose
.SERVER_AUTH
.oid
in trust
)]
1001 except PermissionError
:
1005 ssl_context
.load_verify_locations(cadata
=cert
)
1006 except ssl
.SSLError
:
1010 def make_HTTPS_handler(params
, **kwargs
):
1011 opts_check_certificate
= not params
.get('nocheckcertificate')
1012 context
= ssl
.SSLContext(ssl
.PROTOCOL_TLS_CLIENT
)
1013 context
.check_hostname
= opts_check_certificate
1014 if params
.get('legacyserverconnect'):
1015 context
.options |
= 4 # SSL_OP_LEGACY_SERVER_CONNECT
1016 context
.verify_mode
= ssl
.CERT_REQUIRED
if opts_check_certificate
else ssl
.CERT_NONE
1017 if opts_check_certificate
:
1018 if has_certifi
and 'no-certifi' not in params
.get('compat_opts', []):
1019 context
.load_verify_locations(cafile
=certifi
.where())
1022 context
.load_default_certs()
1023 # Work around the issue in load_default_certs when there are bad certificates. See:
1024 # https://github.com/yt-dlp/yt-dlp/issues/1060,
1025 # https://bugs.python.org/issue35665, https://bugs.python.org/issue45312
1026 except ssl
.SSLError
:
1027 # enum_certificates is not present in mingw python. See https://github.com/yt-dlp/yt-dlp/issues/1151
1028 if sys
.platform
== 'win32' and hasattr(ssl
, 'enum_certificates'):
1029 # Create a new context to discard any certificates that were already loaded
1030 context
= ssl
.SSLContext(ssl
.PROTOCOL_TLS_CLIENT
)
1031 context
.check_hostname
, context
.verify_mode
= True, ssl
.CERT_REQUIRED
1032 for storename
in ('CA', 'ROOT'):
1033 _ssl_load_windows_store_certs(context
, storename
)
1034 context
.set_default_verify_paths()
1035 return YoutubeDLHTTPSHandler(params
, context
=context
, **kwargs
)
1038 def bug_reports_message(before
=';'):
1039 msg
= ('please report this issue on https://github.com/yt-dlp/yt-dlp , '
1040 'filling out the appropriate issue template. '
1041 'Confirm you are on the latest version using yt-dlp -U')
1043 before
= before
.rstrip()
1044 if not before
or before
.endswith(('.', '!', '?')):
1045 msg
= msg
[0].title() + msg
[1:]
1047 return (before
+ ' ' if before
else '') + msg
1050 class YoutubeDLError(Exception):
1051 """Base exception for YoutubeDL errors."""
1054 def __init__(self
, msg
=None):
1057 elif self
.msg
is None:
1058 self
.msg
= type(self
).__name
__
1059 super().__init
__(self
.msg
)
1062 network_exceptions
= [compat_urllib_error
.URLError
, compat_http_client
.HTTPException
, socket
.error
]
1063 if hasattr(ssl
, 'CertificateError'):
1064 network_exceptions
.append(ssl
.CertificateError
)
1065 network_exceptions
= tuple(network_exceptions
)
1068 class ExtractorError(YoutubeDLError
):
1069 """Error during info extraction."""
1071 def __init__(self
, msg
, tb
=None, expected
=False, cause
=None, video_id
=None, ie
=None):
1072 """ tb, if given, is the original traceback (so that it can be printed out).
1073 If expected is set, this is a normal error message and most likely not a bug in yt-dlp.
1075 if sys
.exc_info()[0] in network_exceptions
:
1078 self
.orig_msg
= str(msg
)
1080 self
.expected
= expected
1082 self
.video_id
= video_id
1084 self
.exc_info
= sys
.exc_info() # preserve original exception
1086 super(ExtractorError
, self
).__init
__(''.join((
1087 format_field(ie
, template
='[%s] '),
1088 format_field(video_id
, template
='%s: '),
1090 format_field(cause
, template
=' (caused by %r)'),
1091 '' if expected
else bug_reports_message())))
1093 def format_traceback(self
):
1094 return join_nonempty(
1095 self
.traceback
and ''.join(traceback
.format_tb(self
.traceback
)),
1096 self
.cause
and ''.join(traceback
.format_exception(None, self
.cause
, self
.cause
.__traceback
__)[1:]),
1100 class UnsupportedError(ExtractorError
):
1101 def __init__(self
, url
):
1102 super(UnsupportedError
, self
).__init
__(
1103 'Unsupported URL: %s' % url
, expected
=True)
1107 class RegexNotFoundError(ExtractorError
):
1108 """Error when a regex didn't match"""
1112 class GeoRestrictedError(ExtractorError
):
1113 """Geographic restriction Error exception.
1115 This exception may be thrown when a video is not available from your
1116 geographic location due to geographic restrictions imposed by a website.
1119 def __init__(self
, msg
, countries
=None, **kwargs
):
1120 kwargs
['expected'] = True
1121 super(GeoRestrictedError
, self
).__init
__(msg
, **kwargs
)
1122 self
.countries
= countries
1125 class DownloadError(YoutubeDLError
):
1126 """Download Error exception.
1128 This exception may be thrown by FileDownloader objects if they are not
1129 configured to continue on errors. They will contain the appropriate
1133 def __init__(self
, msg
, exc_info
=None):
1134 """ exc_info, if given, is the original exception that caused the trouble (as returned by sys.exc_info()). """
1135 super(DownloadError
, self
).__init
__(msg
)
1136 self
.exc_info
= exc_info
1139 class EntryNotInPlaylist(YoutubeDLError
):
1140 """Entry not in playlist exception.
1142 This exception will be thrown by YoutubeDL when a requested entry
1143 is not found in the playlist info_dict
1145 msg
= 'Entry not found in info'
1148 class SameFileError(YoutubeDLError
):
1149 """Same File exception.
1151 This exception will be thrown by FileDownloader objects if they detect
1152 multiple files would have to be downloaded to the same file on disk.
1154 msg
= 'Fixed output name but more than one file to download'
1156 def __init__(self
, filename
=None):
1157 if filename
is not None:
1158 self
.msg
+= f
': {filename}'
1159 super().__init
__(self
.msg
)
1162 class PostProcessingError(YoutubeDLError
):
1163 """Post Processing exception.
1165 This exception may be raised by PostProcessor's .run() method to
1166 indicate an error in the postprocessing task.
1170 class DownloadCancelled(YoutubeDLError
):
1171 """ Exception raised when the download queue should be interrupted """
1172 msg
= 'The download was cancelled'
1175 class ExistingVideoReached(DownloadCancelled
):
1176 """ --break-on-existing triggered """
1177 msg
= 'Encountered a video that is already in the archive, stopping due to --break-on-existing'
1180 class RejectedVideoReached(DownloadCancelled
):
1181 """ --break-on-reject triggered """
1182 msg
= 'Encountered a video that did not match filter, stopping due to --break-on-reject'
1185 class MaxDownloadsReached(DownloadCancelled
):
1186 """ --max-downloads limit has been reached. """
1187 msg
= 'Maximum number of downloads reached, stopping due to --max-downloads'
1190 class ReExtractInfo(YoutubeDLError
):
1191 """ Video info needs to be re-extracted. """
1193 def __init__(self
, msg
, expected
=False):
1194 super().__init
__(msg
)
1195 self
.expected
= expected
1198 class ThrottledDownload(ReExtractInfo
):
1199 """ Download speed below --throttled-rate. """
1200 msg
= 'The download speed is below throttle limit'
1203 super().__init
__(self
.msg
, expected
=False)
1206 class UnavailableVideoError(YoutubeDLError
):
1207 """Unavailable Format exception.
1209 This exception will be thrown when a video is requested
1210 in a format that is not available for that video.
1212 msg
= 'Unable to download video'
1214 def __init__(self
, err
=None):
1216 self
.msg
+= f
': {err}'
1217 super().__init
__(self
.msg
)
1220 class ContentTooShortError(YoutubeDLError
):
1221 """Content Too Short exception.
1223 This exception may be raised by FileDownloader objects when a file they
1224 download is too small for what the server announced first, indicating
1225 the connection was probably interrupted.
1228 def __init__(self
, downloaded
, expected
):
1229 super(ContentTooShortError
, self
).__init
__(
1230 'Downloaded {0} bytes, expected {1} bytes'.format(downloaded
, expected
)
1233 self
.downloaded
= downloaded
1234 self
.expected
= expected
1237 class XAttrMetadataError(YoutubeDLError
):
1238 def __init__(self
, code
=None, msg
='Unknown error'):
1239 super(XAttrMetadataError
, self
).__init
__(msg
)
1243 # Parsing code and msg
1244 if (self
.code
in (errno
.ENOSPC
, errno
.EDQUOT
)
1245 or 'No space left' in self
.msg
or 'Disk quota exceeded' in self
.msg
):
1246 self
.reason
= 'NO_SPACE'
1247 elif self
.code
== errno
.E2BIG
or 'Argument list too long' in self
.msg
:
1248 self
.reason
= 'VALUE_TOO_LONG'
1250 self
.reason
= 'NOT_SUPPORTED'
1253 class XAttrUnavailableError(YoutubeDLError
):
1257 def _create_http_connection(ydl_handler
, http_class
, is_https
, *args
, **kwargs
):
1258 # Working around python 2 bug (see http://bugs.python.org/issue17849) by limiting
1259 # expected HTTP responses to meet HTTP/1.0 or later (see also
1260 # https://github.com/ytdl-org/youtube-dl/issues/6727)
1261 if sys
.version_info
< (3, 0):
1262 kwargs
['strict'] = True
1263 hc
= http_class(*args
, **compat_kwargs(kwargs
))
1264 source_address
= ydl_handler
._params
.get('source_address')
1266 if source_address
is not None:
1267 # This is to workaround _create_connection() from socket where it will try all
1268 # address data from getaddrinfo() including IPv6. This filters the result from
1269 # getaddrinfo() based on the source_address value.
1270 # This is based on the cpython socket.create_connection() function.
1271 # https://github.com/python/cpython/blob/master/Lib/socket.py#L691
1272 def _create_connection(address
, timeout
=socket
._GLOBAL
_DEFAULT
_TIMEOUT
, source_address
=None):
1273 host
, port
= address
1275 addrs
= socket
.getaddrinfo(host
, port
, 0, socket
.SOCK_STREAM
)
1276 af
= socket
.AF_INET
if '.' in source_address
[0] else socket
.AF_INET6
1277 ip_addrs
= [addr
for addr
in addrs
if addr
[0] == af
]
1278 if addrs
and not ip_addrs
:
1279 ip_version
= 'v4' if af
== socket
.AF_INET
else 'v6'
1281 "No remote IP%s addresses available for connect, can't use '%s' as source address"
1282 % (ip_version
, source_address
[0]))
1283 for res
in ip_addrs
:
1284 af
, socktype
, proto
, canonname
, sa
= res
1287 sock
= socket
.socket(af
, socktype
, proto
)
1288 if timeout
is not socket
._GLOBAL
_DEFAULT
_TIMEOUT
:
1289 sock
.settimeout(timeout
)
1290 sock
.bind(source_address
)
1292 err
= None # Explicitly break reference cycle
1294 except socket
.error
as _
:
1296 if sock
is not None:
1301 raise socket
.error('getaddrinfo returns an empty list')
1302 if hasattr(hc
, '_create_connection'):
1303 hc
._create
_connection
= _create_connection
1304 sa
= (source_address
, 0)
1305 if hasattr(hc
, 'source_address'): # Python 2.7+
1306 hc
.source_address
= sa
1308 def _hc_connect(self
, *args
, **kwargs
):
1309 sock
= _create_connection(
1310 (self
.host
, self
.port
), self
.timeout
, sa
)
1312 self
.sock
= ssl
.wrap_socket(
1313 sock
, self
.key_file
, self
.cert_file
,
1314 ssl_version
=ssl
.PROTOCOL_TLSv1
)
1317 hc
.connect
= functools
.partial(_hc_connect
, hc
)
1322 def handle_youtubedl_headers(headers
):
1323 filtered_headers
= headers
1325 if 'Youtubedl-no-compression' in filtered_headers
:
1326 filtered_headers
= dict((k
, v
) for k
, v
in filtered_headers
.items() if k
.lower() != 'accept-encoding')
1327 del filtered_headers
['Youtubedl-no-compression']
1329 return filtered_headers
1332 class YoutubeDLHandler(compat_urllib_request
.HTTPHandler
):
1333 """Handler for HTTP requests and responses.
1335 This class, when installed with an OpenerDirector, automatically adds
1336 the standard headers to every HTTP request and handles gzipped and
1337 deflated responses from web servers. If compression is to be avoided in
1338 a particular request, the original request in the program code only has
1339 to include the HTTP header "Youtubedl-no-compression", which will be
1340 removed before making the real request.
1342 Part of this code was copied from:
1344 http://techknack.net/python-urllib2-handlers/
1346 Andrew Rowls, the author of that code, agreed to release it to the
1350 def __init__(self
, params
, *args
, **kwargs
):
1351 compat_urllib_request
.HTTPHandler
.__init
__(self
, *args
, **kwargs
)
1352 self
._params
= params
1354 def http_open(self
, req
):
1355 conn_class
= compat_http_client
.HTTPConnection
1357 socks_proxy
= req
.headers
.get('Ytdl-socks-proxy')
1359 conn_class
= make_socks_conn_class(conn_class
, socks_proxy
)
1360 del req
.headers
['Ytdl-socks-proxy']
1362 return self
.do_open(functools
.partial(
1363 _create_http_connection
, self
, conn_class
, False),
1371 return zlib
.decompress(data
, -zlib
.MAX_WBITS
)
1373 return zlib
.decompress(data
)
1379 return compat_brotli
.decompress(data
)
1381 def http_request(self
, req
):
1382 # According to RFC 3986, URLs can not contain non-ASCII characters, however this is not
1383 # always respected by websites, some tend to give out URLs with non percent-encoded
1384 # non-ASCII characters (see telemb.py, ard.py [#3412])
1385 # urllib chokes on URLs with non-ASCII characters (see http://bugs.python.org/issue3991)
1386 # To work around aforementioned issue we will replace request's original URL with
1387 # percent-encoded one
1388 # Since redirects are also affected (e.g. http://www.southpark.de/alle-episoden/s18e09)
1389 # the code of this workaround has been moved here from YoutubeDL.urlopen()
1390 url
= req
.get_full_url()
1391 url_escaped
= escape_url(url
)
1393 # Substitute URL if any change after escaping
1394 if url
!= url_escaped
:
1395 req
= update_Request(req
, url
=url_escaped
)
1397 for h
, v
in self
._params
.get('http_headers', std_headers
).items():
1398 # Capitalize is needed because of Python bug 2275: http://bugs.python.org/issue2275
1399 # The dict keys are capitalized because of this bug by urllib
1400 if h
.capitalize() not in req
.headers
:
1401 req
.add_header(h
, v
)
1403 if 'Accept-encoding' not in req
.headers
:
1404 req
.add_header('Accept-encoding', ', '.join(SUPPORTED_ENCODINGS
))
1406 req
.headers
= handle_youtubedl_headers(req
.headers
)
1408 if sys
.version_info
< (2, 7) and '#' in req
.get_full_url():
1409 # Python 2.6 is brain-dead when it comes to fragments
1410 req
._Request
__original
= req
._Request
__original
.partition('#')[0]
1411 req
._Request
__r
_type
= req
._Request
__r
_type
.partition('#')[0]
1415 def http_response(self
, req
, resp
):
1418 if resp
.headers
.get('Content-encoding', '') == 'gzip':
1419 content
= resp
.read()
1420 gz
= gzip
.GzipFile(fileobj
=io
.BytesIO(content
), mode
='rb')
1422 uncompressed
= io
.BytesIO(gz
.read())
1423 except IOError as original_ioerror
:
1424 # There may be junk add the end of the file
1425 # See http://stackoverflow.com/q/4928560/35070 for details
1426 for i
in range(1, 1024):
1428 gz
= gzip
.GzipFile(fileobj
=io
.BytesIO(content
[:-i
]), mode
='rb')
1429 uncompressed
= io
.BytesIO(gz
.read())
1434 raise original_ioerror
1435 resp
= compat_urllib_request
.addinfourl(uncompressed
, old_resp
.headers
, old_resp
.url
, old_resp
.code
)
1436 resp
.msg
= old_resp
.msg
1437 del resp
.headers
['Content-encoding']
1439 if resp
.headers
.get('Content-encoding', '') == 'deflate':
1440 gz
= io
.BytesIO(self
.deflate(resp
.read()))
1441 resp
= compat_urllib_request
.addinfourl(gz
, old_resp
.headers
, old_resp
.url
, old_resp
.code
)
1442 resp
.msg
= old_resp
.msg
1443 del resp
.headers
['Content-encoding']
1445 if resp
.headers
.get('Content-encoding', '') == 'br':
1446 resp
= compat_urllib_request
.addinfourl(
1447 io
.BytesIO(self
.brotli(resp
.read())), old_resp
.headers
, old_resp
.url
, old_resp
.code
)
1448 resp
.msg
= old_resp
.msg
1449 del resp
.headers
['Content-encoding']
1450 # Percent-encode redirect URL of Location HTTP header to satisfy RFC 3986 (see
1451 # https://github.com/ytdl-org/youtube-dl/issues/6457).
1452 if 300 <= resp
.code
< 400:
1453 location
= resp
.headers
.get('Location')
1455 # As of RFC 2616 default charset is iso-8859-1 that is respected by python 3
1456 if sys
.version_info
>= (3, 0):
1457 location
= location
.encode('iso-8859-1').decode('utf-8')
1459 location
= location
.decode('utf-8')
1460 location_escaped
= escape_url(location
)
1461 if location
!= location_escaped
:
1462 del resp
.headers
['Location']
1463 if sys
.version_info
< (3, 0):
1464 location_escaped
= location_escaped
.encode('utf-8')
1465 resp
.headers
['Location'] = location_escaped
1468 https_request
= http_request
1469 https_response
= http_response
1472 def make_socks_conn_class(base_class
, socks_proxy
):
1473 assert issubclass(base_class
, (
1474 compat_http_client
.HTTPConnection
, compat_http_client
.HTTPSConnection
))
1476 url_components
= compat_urlparse
.urlparse(socks_proxy
)
1477 if url_components
.scheme
.lower() == 'socks5':
1478 socks_type
= ProxyType
.SOCKS5
1479 elif url_components
.scheme
.lower() in ('socks', 'socks4'):
1480 socks_type
= ProxyType
.SOCKS4
1481 elif url_components
.scheme
.lower() == 'socks4a':
1482 socks_type
= ProxyType
.SOCKS4A
1484 def unquote_if_non_empty(s
):
1487 return compat_urllib_parse_unquote_plus(s
)
1491 url_components
.hostname
, url_components
.port
or 1080,
1493 unquote_if_non_empty(url_components
.username
),
1494 unquote_if_non_empty(url_components
.password
),
1497 class SocksConnection(base_class
):
1499 self
.sock
= sockssocket()
1500 self
.sock
.setproxy(*proxy_args
)
1501 if type(self
.timeout
) in (int, float):
1502 self
.sock
.settimeout(self
.timeout
)
1503 self
.sock
.connect((self
.host
, self
.port
))
1505 if isinstance(self
, compat_http_client
.HTTPSConnection
):
1506 if hasattr(self
, '_context'): # Python > 2.6
1507 self
.sock
= self
._context
.wrap_socket(
1508 self
.sock
, server_hostname
=self
.host
)
1510 self
.sock
= ssl
.wrap_socket(self
.sock
)
1512 return SocksConnection
1515 class YoutubeDLHTTPSHandler(compat_urllib_request
.HTTPSHandler
):
1516 def __init__(self
, params
, https_conn_class
=None, *args
, **kwargs
):
1517 compat_urllib_request
.HTTPSHandler
.__init
__(self
, *args
, **kwargs
)
1518 self
._https
_conn
_class
= https_conn_class
or compat_http_client
.HTTPSConnection
1519 self
._params
= params
1521 def https_open(self
, req
):
1523 conn_class
= self
._https
_conn
_class
1525 if hasattr(self
, '_context'): # python > 2.6
1526 kwargs
['context'] = self
._context
1527 if hasattr(self
, '_check_hostname'): # python 3.x
1528 kwargs
['check_hostname'] = self
._check
_hostname
1530 socks_proxy
= req
.headers
.get('Ytdl-socks-proxy')
1532 conn_class
= make_socks_conn_class(conn_class
, socks_proxy
)
1533 del req
.headers
['Ytdl-socks-proxy']
1535 return self
.do_open(functools
.partial(
1536 _create_http_connection
, self
, conn_class
, True),
1540 class YoutubeDLCookieJar(compat_cookiejar
.MozillaCookieJar
):
1542 See [1] for cookie file format.
1544 1. https://curl.haxx.se/docs/http-cookies.html
1546 _HTTPONLY_PREFIX
= '#HttpOnly_'
1548 _HEADER
= '''# Netscape HTTP Cookie File
1549 # This file is generated by yt-dlp. Do not edit.
1552 _CookieFileEntry
= collections
.namedtuple(
1554 ('domain_name', 'include_subdomains', 'path', 'https_only', 'expires_at', 'name', 'value'))
1556 def save(self
, filename
=None, ignore_discard
=False, ignore_expires
=False):
1558 Save cookies to a file.
1560 Most of the code is taken from CPython 3.8 and slightly adapted
1561 to support cookie files with UTF-8 in both python 2 and 3.
1563 if filename
is None:
1564 if self
.filename
is not None:
1565 filename
= self
.filename
1567 raise ValueError(compat_cookiejar
.MISSING_FILENAME_TEXT
)
1569 # Store session cookies with `expires` set to 0 instead of an empty
1572 if cookie
.expires
is None:
1575 with io
.open(filename
, 'w', encoding
='utf-8') as f
:
1576 f
.write(self
._HEADER
)
1579 if not ignore_discard
and cookie
.discard
:
1581 if not ignore_expires
and cookie
.is_expired(now
):
1587 if cookie
.domain
.startswith('.'):
1588 initial_dot
= 'TRUE'
1590 initial_dot
= 'FALSE'
1591 if cookie
.expires
is not None:
1592 expires
= compat_str(cookie
.expires
)
1595 if cookie
.value
is None:
1596 # cookies.txt regards 'Set-Cookie: foo' as a cookie
1597 # with no name, whereas http.cookiejar regards it as a
1598 # cookie with no value.
1603 value
= cookie
.value
1605 '\t'.join([cookie
.domain
, initial_dot
, cookie
.path
,
1606 secure
, expires
, name
, value
]) + '\n')
1608 def load(self
, filename
=None, ignore_discard
=False, ignore_expires
=False):
1609 """Load cookies from a file."""
1610 if filename
is None:
1611 if self
.filename
is not None:
1612 filename
= self
.filename
1614 raise ValueError(compat_cookiejar
.MISSING_FILENAME_TEXT
)
1616 def prepare_line(line
):
1617 if line
.startswith(self
._HTTPONLY
_PREFIX
):
1618 line
= line
[len(self
._HTTPONLY
_PREFIX
):]
1619 # comments and empty lines are fine
1620 if line
.startswith('#') or not line
.strip():
1622 cookie_list
= line
.split('\t')
1623 if len(cookie_list
) != self
._ENTRY
_LEN
:
1624 raise compat_cookiejar
.LoadError('invalid length %d' % len(cookie_list
))
1625 cookie
= self
._CookieFileEntry
(*cookie_list
)
1626 if cookie
.expires_at
and not cookie
.expires_at
.isdigit():
1627 raise compat_cookiejar
.LoadError('invalid expires at %s' % cookie
.expires_at
)
1631 with io
.open(filename
, encoding
='utf-8') as f
:
1634 cf
.write(prepare_line(line
))
1635 except compat_cookiejar
.LoadError
as e
:
1637 'WARNING: skipping cookie file entry due to %s: %r\n'
1638 % (e
, line
), sys
.stderr
)
1641 self
._really
_load
(cf
, filename
, ignore_discard
, ignore_expires
)
1642 # Session cookies are denoted by either `expires` field set to
1643 # an empty string or 0. MozillaCookieJar only recognizes the former
1644 # (see [1]). So we need force the latter to be recognized as session
1645 # cookies on our own.
1646 # Session cookies may be important for cookies-based authentication,
1647 # e.g. usually, when user does not check 'Remember me' check box while
1648 # logging in on a site, some important cookies are stored as session
1649 # cookies so that not recognizing them will result in failed login.
1650 # 1. https://bugs.python.org/issue17164
1652 # Treat `expires=0` cookies as session cookies
1653 if cookie
.expires
== 0:
1654 cookie
.expires
= None
1655 cookie
.discard
= True
1658 class YoutubeDLCookieProcessor(compat_urllib_request
.HTTPCookieProcessor
):
1659 def __init__(self
, cookiejar
=None):
1660 compat_urllib_request
.HTTPCookieProcessor
.__init
__(self
, cookiejar
)
1662 def http_response(self
, request
, response
):
1663 # Python 2 will choke on next HTTP request in row if there are non-ASCII
1664 # characters in Set-Cookie HTTP header of last response (see
1665 # https://github.com/ytdl-org/youtube-dl/issues/6769).
1666 # In order to at least prevent crashing we will percent encode Set-Cookie
1667 # header before HTTPCookieProcessor starts processing it.
1668 # if sys.version_info < (3, 0) and response.headers:
1669 # for set_cookie_header in ('Set-Cookie', 'Set-Cookie2'):
1670 # set_cookie = response.headers.get(set_cookie_header)
1672 # set_cookie_escaped = compat_urllib_parse.quote(set_cookie, b"%/;:@&=+$,!~*'()?#[] ")
1673 # if set_cookie != set_cookie_escaped:
1674 # del response.headers[set_cookie_header]
1675 # response.headers[set_cookie_header] = set_cookie_escaped
1676 return compat_urllib_request
.HTTPCookieProcessor
.http_response(self
, request
, response
)
1678 https_request
= compat_urllib_request
.HTTPCookieProcessor
.http_request
1679 https_response
= http_response
1682 class YoutubeDLRedirectHandler(compat_urllib_request
.HTTPRedirectHandler
):
1683 """YoutubeDL redirect handler
1685 The code is based on HTTPRedirectHandler implementation from CPython [1].
1687 This redirect handler solves two issues:
1688 - ensures redirect URL is always unicode under python 2
1689 - introduces support for experimental HTTP response status code
1690 308 Permanent Redirect [2] used by some sites [3]
1692 1. https://github.com/python/cpython/blob/master/Lib/urllib/request.py
1693 2. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
1694 3. https://github.com/ytdl-org/youtube-dl/issues/28768
1697 http_error_301
= http_error_303
= http_error_307
= http_error_308
= compat_urllib_request
.HTTPRedirectHandler
.http_error_302
1699 def redirect_request(self
, req
, fp
, code
, msg
, headers
, newurl
):
1700 """Return a Request or None in response to a redirect.
1702 This is called by the http_error_30x methods when a
1703 redirection response is received. If a redirection should
1704 take place, return a new Request to allow http_error_30x to
1705 perform the redirect. Otherwise, raise HTTPError if no-one
1706 else should try to handle this url. Return None if you can't
1707 but another Handler might.
1709 m
= req
.get_method()
1710 if (not (code
in (301, 302, 303, 307, 308) and m
in ("GET", "HEAD")
1711 or code
in (301, 302, 303) and m
== "POST")):
1712 raise compat_HTTPError(req
.full_url
, code
, msg
, headers
, fp
)
1713 # Strictly (according to RFC 2616), 301 or 302 in response to
1714 # a POST MUST NOT cause a redirection without confirmation
1715 # from the user (of urllib.request, in this case). In practice,
1716 # essentially all clients do redirect in this case, so we do
1719 # On python 2 urlh.geturl() may sometimes return redirect URL
1720 # as byte string instead of unicode. This workaround allows
1721 # to force it always return unicode.
1722 if sys
.version_info
[0] < 3:
1723 newurl
= compat_str(newurl
)
1725 # Be conciliant with URIs containing a space. This is mainly
1726 # redundant with the more complete encoding done in http_error_302(),
1727 # but it is kept for compatibility with other callers.
1728 newurl
= newurl
.replace(' ', '%20')
1730 CONTENT_HEADERS
= ("content-length", "content-type")
1731 # NB: don't use dict comprehension for python 2.6 compatibility
1732 newheaders
= dict((k
, v
) for k
, v
in req
.headers
.items()
1733 if k
.lower() not in CONTENT_HEADERS
)
1734 return compat_urllib_request
.Request(
1735 newurl
, headers
=newheaders
, origin_req_host
=req
.origin_req_host
,
1739 def extract_timezone(date_str
):
1742 ^.{8,}? # >=8 char non-TZ prefix, if present
1743 (?P<tz>Z| # just the UTC Z, or
1744 (?:(?<=.\b\d{4}|\b\d{2}:\d\d)| # preceded by 4 digits or hh:mm or
1745 (?<!.\b[a-zA-Z]{3}|[a-zA-Z]{4}|..\b\d\d)) # not preceded by 3 alpha word or >= 4 alpha or 2 digits
1746 [ ]? # optional space
1747 (?P<sign>\+|-) # +/-
1748 (?P<hours>[0-9]{2}):?(?P<minutes>[0-9]{2}) # hh[:]mm
1752 timezone
= datetime
.timedelta()
1754 date_str
= date_str
[:-len(m
.group('tz'))]
1755 if not m
.group('sign'):
1756 timezone
= datetime
.timedelta()
1758 sign
= 1 if m
.group('sign') == '+' else -1
1759 timezone
= datetime
.timedelta(
1760 hours
=sign
* int(m
.group('hours')),
1761 minutes
=sign
* int(m
.group('minutes')))
1762 return timezone
, date_str
1765 def parse_iso8601(date_str
, delimiter
='T', timezone
=None):
1766 """ Return a UNIX timestamp from the given date """
1768 if date_str
is None:
1771 date_str
= re
.sub(r
'\.[0-9]+', '', date_str
)
1773 if timezone
is None:
1774 timezone
, date_str
= extract_timezone(date_str
)
1777 date_format
= '%Y-%m-%d{0}%H:%M:%S'.format(delimiter
)
1778 dt
= datetime
.datetime
.strptime(date_str
, date_format
) - timezone
1779 return calendar
.timegm(dt
.timetuple())
1784 def date_formats(day_first
=True):
1785 return DATE_FORMATS_DAY_FIRST
if day_first
else DATE_FORMATS_MONTH_FIRST
1788 def unified_strdate(date_str
, day_first
=True):
1789 """Return a string with the date in the format YYYYMMDD"""
1791 if date_str
is None:
1795 date_str
= date_str
.replace(',', ' ')
1796 # Remove AM/PM + timezone
1797 date_str
= re
.sub(r
'(?i)\s*(?:AM|PM)(?:\s+[A-Z]+)?', '', date_str
)
1798 _
, date_str
= extract_timezone(date_str
)
1800 for expression
in date_formats(day_first
):
1802 upload_date
= datetime
.datetime
.strptime(date_str
, expression
).strftime('%Y%m%d')
1805 if upload_date
is None:
1806 timetuple
= email
.utils
.parsedate_tz(date_str
)
1809 upload_date
= datetime
.datetime(*timetuple
[:6]).strftime('%Y%m%d')
1812 if upload_date
is not None:
1813 return compat_str(upload_date
)
1816 def unified_timestamp(date_str
, day_first
=True):
1817 if date_str
is None:
1820 date_str
= re
.sub(r
'[,|]', '', date_str
)
1822 pm_delta
= 12 if re
.search(r
'(?i)PM', date_str
) else 0
1823 timezone
, date_str
= extract_timezone(date_str
)
1825 # Remove AM/PM + timezone
1826 date_str
= re
.sub(r
'(?i)\s*(?:AM|PM)(?:\s+[A-Z]+)?', '', date_str
)
1828 # Remove unrecognized timezones from ISO 8601 alike timestamps
1829 m
= re
.search(r
'\d{1,2}:\d{1,2}(?:\.\d+)?(?P<tz>\s*[A-Z]+)$', date_str
)
1831 date_str
= date_str
[:-len(m
.group('tz'))]
1833 # Python only supports microseconds, so remove nanoseconds
1834 m
= re
.search(r
'^([0-9]{4,}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}\.[0-9]{6})[0-9]+$', date_str
)
1836 date_str
= m
.group(1)
1838 for expression
in date_formats(day_first
):
1840 dt
= datetime
.datetime
.strptime(date_str
, expression
) - timezone
+ datetime
.timedelta(hours
=pm_delta
)
1841 return calendar
.timegm(dt
.timetuple())
1844 timetuple
= email
.utils
.parsedate_tz(date_str
)
1846 return calendar
.timegm(timetuple
) + pm_delta
* 3600
1849 def determine_ext(url
, default_ext
='unknown_video'):
1850 if url
is None or '.' not in url
:
1852 guess
= url
.partition('?')[0].rpartition('.')[2]
1853 if re
.match(r
'^[A-Za-z0-9]+$', guess
):
1855 # Try extract ext from URLs like http://example.com/foo/bar.mp4/?download
1856 elif guess
.rstrip('/') in KNOWN_EXTENSIONS
:
1857 return guess
.rstrip('/')
1862 def subtitles_filename(filename
, sub_lang
, sub_format
, expected_real_ext
=None):
1863 return replace_extension(filename
, sub_lang
+ '.' + sub_format
, expected_real_ext
)
1866 def datetime_from_str(date_str
, precision
='auto', format
='%Y%m%d'):
1868 Return a datetime object from a string in the format YYYYMMDD or
1869 (now|today|yesterday|date)[+-][0-9](microsecond|second|minute|hour|day|week|month|year)(s)?
1871 format: string date format used to return datetime object from
1872 precision: round the time portion of a datetime object.
1873 auto|microsecond|second|minute|hour|day.
1874 auto: round to the unit provided in date_str (if applicable).
1876 auto_precision
= False
1877 if precision
== 'auto':
1878 auto_precision
= True
1879 precision
= 'microsecond'
1880 today
= datetime_round(datetime
.datetime
.utcnow(), precision
)
1881 if date_str
in ('now', 'today'):
1883 if date_str
== 'yesterday':
1884 return today
- datetime
.timedelta(days
=1)
1886 r
'(?P<start>.+)(?P<sign>[+-])(?P<time>\d+)(?P<unit>microsecond|second|minute|hour|day|week|month|year)(s)?',
1888 if match
is not None:
1889 start_time
= datetime_from_str(match
.group('start'), precision
, format
)
1890 time
= int(match
.group('time')) * (-1 if match
.group('sign') == '-' else 1)
1891 unit
= match
.group('unit')
1892 if unit
== 'month' or unit
== 'year':
1893 new_date
= datetime_add_months(start_time
, time
* 12 if unit
== 'year' else time
)
1899 delta
= datetime
.timedelta(**{unit + 's': time}
)
1900 new_date
= start_time
+ delta
1902 return datetime_round(new_date
, unit
)
1905 return datetime_round(datetime
.datetime
.strptime(date_str
, format
), precision
)
1908 def date_from_str(date_str
, format
='%Y%m%d', strict
=False):
1910 Return a datetime object from a string in the format YYYYMMDD or
1911 (now|today|yesterday|date)[+-][0-9](microsecond|second|minute|hour|day|week|month|year)(s)?
1913 If "strict", only (now|today)[+-][0-9](day|week|month|year)(s)? is allowed
1915 format: string date format used to return datetime object from
1917 if strict
and not re
.fullmatch(r
'\d{8}|(now|today)[+-]\d+(day|week|month|year)(s)?', date_str
):
1918 raise ValueError(f
'Invalid date format {date_str}')
1919 return datetime_from_str(date_str
, precision
='microsecond', format
=format
).date()
1922 def datetime_add_months(dt
, months
):
1923 """Increment/Decrement a datetime object by months."""
1924 month
= dt
.month
+ months
- 1
1925 year
= dt
.year
+ month
// 12
1926 month
= month
% 12 + 1
1927 day
= min(dt
.day
, calendar
.monthrange(year
, month
)[1])
1928 return dt
.replace(year
, month
, day
)
1931 def datetime_round(dt
, precision
='day'):
1933 Round a datetime object's time to a specific precision
1935 if precision
== 'microsecond':
1944 roundto
= lambda x
, n
: ((x
+ n
/ 2) // n
) * n
1945 timestamp
= calendar
.timegm(dt
.timetuple())
1946 return datetime
.datetime
.utcfromtimestamp(roundto(timestamp
, unit_seconds
[precision
]))
1949 def hyphenate_date(date_str
):
1951 Convert a date in 'YYYYMMDD' format to 'YYYY-MM-DD' format"""
1952 match
= re
.match(r
'^(\d\d\d\d)(\d\d)(\d\d)$', date_str
)
1953 if match
is not None:
1954 return '-'.join(match
.groups())
1959 class DateRange(object):
1960 """Represents a time interval between two dates"""
1962 def __init__(self
, start
=None, end
=None):
1963 """start and end must be strings in the format accepted by date"""
1964 if start
is not None:
1965 self
.start
= date_from_str(start
, strict
=True)
1967 self
.start
= datetime
.datetime
.min.date()
1969 self
.end
= date_from_str(end
, strict
=True)
1971 self
.end
= datetime
.datetime
.max.date()
1972 if self
.start
> self
.end
:
1973 raise ValueError('Date range: "%s" , the start date must be before the end date' % self
)
1977 """Returns a range that only contains the given day"""
1978 return cls(day
, day
)
1980 def __contains__(self
, date
):
1981 """Check if the date is in the range"""
1982 if not isinstance(date
, datetime
.date
):
1983 date
= date_from_str(date
)
1984 return self
.start
<= date
<= self
.end
1987 return '%s - %s' % (self
.start
.isoformat(), self
.end
.isoformat())
1990 def platform_name():
1991 """ Returns the platform name as a compat_str """
1992 res
= platform
.platform()
1993 if isinstance(res
, bytes):
1994 res
= res
.decode(preferredencoding())
1996 assert isinstance(res
, compat_str
)
2000 def get_windows_version():
2001 ''' Get Windows version. None if it's not running on Windows '''
2002 if compat_os_name
== 'nt':
2003 return version_tuple(platform
.win32_ver()[1])
2008 def _windows_write_string(s
, out
):
2009 """ Returns True if the string was written using special methods,
2010 False if it has yet to be written out."""
2011 # Adapted from http://stackoverflow.com/a/3259271/35070
2013 import ctypes
.wintypes
2021 fileno
= out
.fileno()
2022 except AttributeError:
2023 # If the output stream doesn't have a fileno, it's virtual
2025 except io
.UnsupportedOperation
:
2026 # Some strange Windows pseudo files?
2028 if fileno
not in WIN_OUTPUT_IDS
:
2031 GetStdHandle
= compat_ctypes_WINFUNCTYPE(
2032 ctypes
.wintypes
.HANDLE
, ctypes
.wintypes
.DWORD
)(
2033 ('GetStdHandle', ctypes
.windll
.kernel32
))
2034 h
= GetStdHandle(WIN_OUTPUT_IDS
[fileno
])
2036 WriteConsoleW
= compat_ctypes_WINFUNCTYPE(
2037 ctypes
.wintypes
.BOOL
, ctypes
.wintypes
.HANDLE
, ctypes
.wintypes
.LPWSTR
,
2038 ctypes
.wintypes
.DWORD
, ctypes
.POINTER(ctypes
.wintypes
.DWORD
),
2039 ctypes
.wintypes
.LPVOID
)(('WriteConsoleW', ctypes
.windll
.kernel32
))
2040 written
= ctypes
.wintypes
.DWORD(0)
2042 GetFileType
= compat_ctypes_WINFUNCTYPE(ctypes
.wintypes
.DWORD
, ctypes
.wintypes
.DWORD
)(('GetFileType', ctypes
.windll
.kernel32
))
2043 FILE_TYPE_CHAR
= 0x0002
2044 FILE_TYPE_REMOTE
= 0x8000
2045 GetConsoleMode
= compat_ctypes_WINFUNCTYPE(
2046 ctypes
.wintypes
.BOOL
, ctypes
.wintypes
.HANDLE
,
2047 ctypes
.POINTER(ctypes
.wintypes
.DWORD
))(
2048 ('GetConsoleMode', ctypes
.windll
.kernel32
))
2049 INVALID_HANDLE_VALUE
= ctypes
.wintypes
.DWORD(-1).value
2051 def not_a_console(handle
):
2052 if handle
== INVALID_HANDLE_VALUE
or handle
is None:
2054 return ((GetFileType(handle
) & ~FILE_TYPE_REMOTE
) != FILE_TYPE_CHAR
2055 or GetConsoleMode(handle
, ctypes
.byref(ctypes
.wintypes
.DWORD())) == 0)
2057 if not_a_console(h
):
2060 def next_nonbmp_pos(s
):
2062 return next(i
for i
, c
in enumerate(s
) if ord(c
) > 0xffff)
2063 except StopIteration:
2067 count
= min(next_nonbmp_pos(s
), 1024)
2069 ret
= WriteConsoleW(
2070 h
, s
, count
if count
else 2, ctypes
.byref(written
), None)
2072 raise OSError('Failed to write string')
2073 if not count
: # We just wrote a non-BMP character
2074 assert written
.value
== 2
2077 assert written
.value
> 0
2078 s
= s
[written
.value
:]
2082 def write_string(s
, out
=None, encoding
=None):
2085 assert type(s
) == compat_str
2087 if sys
.platform
== 'win32' and encoding
is None and hasattr(out
, 'fileno'):
2088 if _windows_write_string(s
, out
):
2091 if ('b' in getattr(out
, 'mode', '')
2092 or sys
.version_info
[0] < 3): # Python 2 lies about mode of sys.stderr
2093 byt
= s
.encode(encoding
or preferredencoding(), 'ignore')
2095 elif hasattr(out
, 'buffer'):
2096 enc
= encoding
or getattr(out
, 'encoding', None) or preferredencoding()
2097 byt
= s
.encode(enc
, 'ignore')
2098 out
.buffer.write(byt
)
2104 def bytes_to_intlist(bs
):
2107 if isinstance(bs
[0], int): # Python 3
2110 return [ord(c
) for c
in bs
]
2113 def intlist_to_bytes(xs
):
2116 return compat_struct_pack('%dB' % len(xs
), *xs
)
2119 # Cross-platform file locking
2120 if sys
.platform
== 'win32':
2121 import ctypes
.wintypes
2124 class OVERLAPPED(ctypes
.Structure
):
2126 ('Internal', ctypes
.wintypes
.LPVOID
),
2127 ('InternalHigh', ctypes
.wintypes
.LPVOID
),
2128 ('Offset', ctypes
.wintypes
.DWORD
),
2129 ('OffsetHigh', ctypes
.wintypes
.DWORD
),
2130 ('hEvent', ctypes
.wintypes
.HANDLE
),
2133 kernel32
= ctypes
.windll
.kernel32
2134 LockFileEx
= kernel32
.LockFileEx
2135 LockFileEx
.argtypes
= [
2136 ctypes
.wintypes
.HANDLE
, # hFile
2137 ctypes
.wintypes
.DWORD
, # dwFlags
2138 ctypes
.wintypes
.DWORD
, # dwReserved
2139 ctypes
.wintypes
.DWORD
, # nNumberOfBytesToLockLow
2140 ctypes
.wintypes
.DWORD
, # nNumberOfBytesToLockHigh
2141 ctypes
.POINTER(OVERLAPPED
) # Overlapped
2143 LockFileEx
.restype
= ctypes
.wintypes
.BOOL
2144 UnlockFileEx
= kernel32
.UnlockFileEx
2145 UnlockFileEx
.argtypes
= [
2146 ctypes
.wintypes
.HANDLE
, # hFile
2147 ctypes
.wintypes
.DWORD
, # dwReserved
2148 ctypes
.wintypes
.DWORD
, # nNumberOfBytesToLockLow
2149 ctypes
.wintypes
.DWORD
, # nNumberOfBytesToLockHigh
2150 ctypes
.POINTER(OVERLAPPED
) # Overlapped
2152 UnlockFileEx
.restype
= ctypes
.wintypes
.BOOL
2153 whole_low
= 0xffffffff
2154 whole_high
= 0x7fffffff
2156 def _lock_file(f
, exclusive
, block
):
2157 overlapped
= OVERLAPPED()
2158 overlapped
.Offset
= 0
2159 overlapped
.OffsetHigh
= 0
2160 overlapped
.hEvent
= 0
2161 f
._lock
_file
_overlapped
_p
= ctypes
.pointer(overlapped
)
2163 if not LockFileEx(msvcrt
.get_osfhandle(f
.fileno()),
2164 (0x2 if exclusive
else 0x0) |
(0x0 if block
else 0x1),
2165 0, whole_low
, whole_high
, f
._lock
_file
_overlapped
_p
):
2166 raise BlockingIOError('Locking file failed: %r' % ctypes
.FormatError())
2168 def _unlock_file(f
):
2169 assert f
._lock
_file
_overlapped
_p
2170 handle
= msvcrt
.get_osfhandle(f
.fileno())
2171 if not UnlockFileEx(handle
, 0, whole_low
, whole_high
, f
._lock
_file
_overlapped
_p
):
2172 raise OSError('Unlocking file failed: %r' % ctypes
.FormatError())
2178 def _lock_file(f
, exclusive
, block
):
2181 fcntl
.LOCK_SH
if not exclusive
2182 else fcntl
.LOCK_EX
if block
2183 else fcntl
.LOCK_EX | fcntl
.LOCK_NB
)
2184 except BlockingIOError
:
2186 except OSError: # AOSP does not have flock()
2188 fcntl
.LOCK_SH
if not exclusive
2189 else fcntl
.LOCK_EX
if block
2190 else fcntl
.LOCK_EX | fcntl
.LOCK_NB
)
2192 def _unlock_file(f
):
2194 fcntl
.flock(f
, fcntl
.LOCK_UN
)
2196 fcntl
.lockf(f
, fcntl
.LOCK_UN
)
2199 UNSUPPORTED_MSG
= 'file locking is not supported on this platform'
2201 def _lock_file(f
, exclusive
, block
):
2202 raise IOError(UNSUPPORTED_MSG
)
2204 def _unlock_file(f
):
2205 raise IOError(UNSUPPORTED_MSG
)
2208 class locked_file(object):
2211 def __init__(self
, filename
, mode
, block
=True, encoding
=None):
2212 assert mode
in ['r', 'rb', 'a', 'ab', 'w', 'wb']
2213 self
.f
= io
.open(filename
, mode
, encoding
=encoding
)
2217 def __enter__(self
):
2218 exclusive
= 'r' not in self
.mode
2220 _lock_file(self
.f
, exclusive
, self
.block
)
2226 def __exit__(self
, etype
, value
, traceback
):
2228 if not self
._closed
:
2229 _unlock_file(self
.f
)
2237 def write(self
, *args
):
2238 return self
.f
.write(*args
)
2240 def read(self
, *args
):
2241 return self
.f
.read(*args
)
2247 return self
.__enter
__()
2249 def close(self
, *args
):
2250 self
.__exit
__(self
, *args
, value
=False, traceback
=False)
2253 def get_filesystem_encoding():
2254 encoding
= sys
.getfilesystemencoding()
2255 return encoding
if encoding
is not None else 'utf-8'
2258 def shell_quote(args
):
2260 encoding
= get_filesystem_encoding()
2262 if isinstance(a
, bytes):
2263 # We may get a filename encoded with 'encodeFilename'
2264 a
= a
.decode(encoding
)
2265 quoted_args
.append(compat_shlex_quote(a
))
2266 return ' '.join(quoted_args
)
2269 def smuggle_url(url
, data
):
2270 """ Pass additional data in a URL for internal use. """
2272 url
, idata
= unsmuggle_url(url
, {})
2274 sdata
= compat_urllib_parse_urlencode(
2275 {'__youtubedl_smuggle': json.dumps(data)}
)
2276 return url
+ '#' + sdata
2279 def unsmuggle_url(smug_url
, default
=None):
2280 if '#__youtubedl_smuggle' not in smug_url
:
2281 return smug_url
, default
2282 url
, _
, sdata
= smug_url
.rpartition('#')
2283 jsond
= compat_parse_qs(sdata
)['__youtubedl_smuggle'][0]
2284 data
= json
.loads(jsond
)
2288 def format_decimal_suffix(num
, fmt
='%d%s', *, factor
=1000):
2289 """ Formats numbers with decimal sufixes like K, M, etc """
2290 num
, factor
= float_or_none(num
), float(factor
)
2291 if num
is None or num
< 0:
2293 POSSIBLE_SUFFIXES
= 'kMGTPEZY'
2294 exponent
= 0 if num
== 0 else min(int(math
.log(num
, factor
)), len(POSSIBLE_SUFFIXES
))
2295 suffix
= ['', *POSSIBLE_SUFFIXES
][exponent
]
2297 suffix
= {'k': 'Ki', '': ''}
.get(suffix
, f
'{suffix}i')
2298 converted
= num
/ (factor
** exponent
)
2299 return fmt
% (converted
, suffix
)
2302 def format_bytes(bytes):
2303 return format_decimal_suffix(bytes, '%.2f%sB', factor
=1024) or 'N/A'
2306 def lookup_unit_table(unit_table
, s
):
2307 units_re
= '|'.join(re
.escape(u
) for u
in unit_table
)
2309 r
'(?P<num>[0-9]+(?:[,.][0-9]*)?)\s*(?P<unit>%s)\b' % units_re
, s
)
2312 num_str
= m
.group('num').replace(',', '.')
2313 mult
= unit_table
[m
.group('unit')]
2314 return int(float(num_str
) * mult
)
2317 def parse_filesize(s
):
2321 # The lower-case forms are of course incorrect and unofficial,
2322 # but we support those too
2339 'megabytes': 1000 ** 2,
2340 'mebibytes': 1024 ** 2,
2346 'gigabytes': 1000 ** 3,
2347 'gibibytes': 1024 ** 3,
2353 'terabytes': 1000 ** 4,
2354 'tebibytes': 1024 ** 4,
2360 'petabytes': 1000 ** 5,
2361 'pebibytes': 1024 ** 5,
2367 'exabytes': 1000 ** 6,
2368 'exbibytes': 1024 ** 6,
2374 'zettabytes': 1000 ** 7,
2375 'zebibytes': 1024 ** 7,
2381 'yottabytes': 1000 ** 8,
2382 'yobibytes': 1024 ** 8,
2385 return lookup_unit_table(_UNIT_TABLE
, s
)
2392 s
= re
.sub(r
'^[^\d]+\s', '', s
).strip()
2394 if re
.match(r
'^[\d,.]+$', s
):
2395 return str_to_int(s
)
2408 ret
= lookup_unit_table(_UNIT_TABLE
, s
)
2412 mobj
= re
.match(r
'([\d,.]+)(?:$|\s)', s
)
2414 return str_to_int(mobj
.group(1))
2417 def parse_resolution(s
):
2421 mobj
= re
.search(r
'(?<![a-zA-Z0-9])(?P<w>\d+)\s*[xX×,]\s*(?P<h>\d+)(?![a-zA-Z0-9])', s
)
2424 'width': int(mobj
.group('w')),
2425 'height': int(mobj
.group('h')),
2428 mobj
= re
.search(r
'(?<![a-zA-Z0-9])(\d+)[pPiI](?![a-zA-Z0-9])', s
)
2430 return {'height': int(mobj.group(1))}
2432 mobj
= re
.search(r
'\b([48])[kK]\b', s
)
2434 return {'height': int(mobj.group(1)) * 540}
2439 def parse_bitrate(s
):
2440 if not isinstance(s
, compat_str
):
2442 mobj
= re
.search(r
'\b(\d+)\s*kbps', s
)
2444 return int(mobj
.group(1))
2447 def month_by_name(name
, lang
='en'):
2448 """ Return the number of a month by (locale-independently) English name """
2450 month_names
= MONTH_NAMES
.get(lang
, MONTH_NAMES
['en'])
2453 return month_names
.index(name
) + 1
2458 def month_by_abbreviation(abbrev
):
2459 """ Return the number of a month by (locale-independently) English
2463 return [s
[:3] for s
in ENGLISH_MONTH_NAMES
].index(abbrev
) + 1
2468 def fix_xml_ampersands(xml_str
):
2469 """Replace all the '&' by '&' in XML"""
2471 r
'&(?!amp;|lt;|gt;|apos;|quot;|#x[0-9a-fA-F]{,4};|#[0-9]{,4};)',
2476 def setproctitle(title
):
2477 assert isinstance(title
, compat_str
)
2479 # ctypes in Jython is not complete
2480 # http://bugs.jython.org/issue2148
2481 if sys
.platform
.startswith('java'):
2485 libc
= ctypes
.cdll
.LoadLibrary('libc.so.6')
2489 # LoadLibrary in Windows Python 2.7.13 only expects
2490 # a bytestring, but since unicode_literals turns
2491 # every string into a unicode string, it fails.
2493 title_bytes
= title
.encode('utf-8')
2494 buf
= ctypes
.create_string_buffer(len(title_bytes
))
2495 buf
.value
= title_bytes
2497 libc
.prctl(15, buf
, 0, 0, 0)
2498 except AttributeError:
2499 return # Strange libc, just skip this
2502 def remove_start(s
, start
):
2503 return s
[len(start
):] if s
is not None and s
.startswith(start
) else s
2506 def remove_end(s
, end
):
2507 return s
[:-len(end
)] if s
is not None and s
.endswith(end
) else s
2510 def remove_quotes(s
):
2511 if s
is None or len(s
) < 2:
2513 for quote
in ('"', "'", ):
2514 if s
[0] == quote
and s
[-1] == quote
:
2519 def get_domain(url
):
2520 domain
= re
.match(r
'(?:https?:\/\/)?(?:www\.)?(?P<domain>[^\n\/]+\.[^\n\/]+)(?:\/(.*))?', url
)
2521 return domain
.group('domain') if domain
else None
2524 def url_basename(url
):
2525 path
= compat_urlparse
.urlparse(url
).path
2526 return path
.strip('/').split('/')[-1]
2530 return re
.match(r
'https?://[^?#&]+/', url
).group()
2533 def urljoin(base
, path
):
2534 if isinstance(path
, bytes):
2535 path
= path
.decode('utf-8')
2536 if not isinstance(path
, compat_str
) or not path
:
2538 if re
.match(r
'^(?:[a-zA-Z][a-zA-Z0-9+-.]*:)?//', path
):
2540 if isinstance(base
, bytes):
2541 base
= base
.decode('utf-8')
2542 if not isinstance(base
, compat_str
) or not re
.match(
2543 r
'^(?:https?:)?//', base
):
2545 return compat_urlparse
.urljoin(base
, path
)
2548 class HEADRequest(compat_urllib_request
.Request
):
2549 def get_method(self
):
2553 class PUTRequest(compat_urllib_request
.Request
):
2554 def get_method(self
):
2558 def int_or_none(v
, scale
=1, default
=None, get_attr
=None, invscale
=1):
2559 if get_attr
and v
is not None:
2560 v
= getattr(v
, get_attr
, None)
2562 return int(v
) * invscale
// scale
2563 except (ValueError, TypeError, OverflowError):
2567 def str_or_none(v
, default
=None):
2568 return default
if v
is None else compat_str(v
)
2571 def str_to_int(int_str
):
2572 """ A more relaxed version of int_or_none """
2573 if isinstance(int_str
, compat_integer_types
):
2575 elif isinstance(int_str
, compat_str
):
2576 int_str
= re
.sub(r
'[,\.\+]', '', int_str
)
2577 return int_or_none(int_str
)
2580 def float_or_none(v
, scale
=1, invscale
=1, default
=None):
2584 return float(v
) * invscale
/ scale
2585 except (ValueError, TypeError):
2589 def bool_or_none(v
, default
=None):
2590 return v
if isinstance(v
, bool) else default
2593 def strip_or_none(v
, default
=None):
2594 return v
.strip() if isinstance(v
, compat_str
) else default
2597 def url_or_none(url
):
2598 if not url
or not isinstance(url
, compat_str
):
2601 return url
if re
.match(r
'^(?:(?:https?|rt(?:m(?:pt?[es]?|fp)|sp[su]?)|mms|ftps?):)?//', url
) else None
2604 def request_to_url(req
):
2605 if isinstance(req
, compat_urllib_request
.Request
):
2606 return req
.get_full_url()
2611 def strftime_or_none(timestamp
, date_format
, default
=None):
2612 datetime_object
= None
2614 if isinstance(timestamp
, compat_numeric_types
): # unix timestamp
2615 datetime_object
= datetime
.datetime
.utcfromtimestamp(timestamp
)
2616 elif isinstance(timestamp
, compat_str
): # assume YYYYMMDD
2617 datetime_object
= datetime
.datetime
.strptime(timestamp
, '%Y%m%d')
2618 return datetime_object
.strftime(date_format
)
2619 except (ValueError, TypeError, AttributeError):
2623 def parse_duration(s
):
2624 if not isinstance(s
, compat_basestring
):
2630 days
, hours
, mins
, secs
, ms
= [None] * 5
2631 m
= re
.match(r
'''(?x)
2633 (?:(?:(?P<days>[0-9]+):)?(?P<hours>[0-9]+):)?(?P<mins>[0-9]+):)?
2634 (?P<secs>(?(before_secs)[0-9]{1,2}|[0-9]+))
2635 (?P<ms>[.:][0-9]+)?Z?$
2638 days
, hours
, mins
, secs
, ms
= m
.group('days', 'hours', 'mins', 'secs', 'ms')
2643 [0-9]+\s*y(?:ears?)?\s*
2646 [0-9]+\s*m(?:onths?)?\s*
2649 [0-9]+\s*w(?:eeks?)?\s*
2652 (?P<days>[0-9]+)\s*d(?:ays?)?\s*
2656 (?P<hours>[0-9]+)\s*h(?:ours?)?\s*
2659 (?P<mins>[0-9]+)\s*m(?:in(?:ute)?s?)?\s*
2662 (?P<secs>[0-9]+)(?P<ms>\.[0-9]+)?\s*s(?:ec(?:ond)?s?)?\s*
2665 days
, hours
, mins
, secs
, ms
= m
.groups()
2667 m
= re
.match(r
'(?i)(?:(?P<hours>[0-9.]+)\s*(?:hours?)|(?P<mins>[0-9.]+)\s*(?:mins?\.?|minutes?)\s*)Z?$', s
)
2669 hours
, mins
= m
.groups()
2675 duration
+= float(secs
)
2677 duration
+= float(mins
) * 60
2679 duration
+= float(hours
) * 60 * 60
2681 duration
+= float(days
) * 24 * 60 * 60
2683 duration
+= float(ms
.replace(':', '.'))
2687 def prepend_extension(filename
, ext
, expected_real_ext
=None):
2688 name
, real_ext
= os
.path
.splitext(filename
)
2690 '{0}.{1}{2}'.format(name
, ext
, real_ext
)
2691 if not expected_real_ext
or real_ext
[1:] == expected_real_ext
2692 else '{0}.{1}'.format(filename
, ext
))
2695 def replace_extension(filename
, ext
, expected_real_ext
=None):
2696 name
, real_ext
= os
.path
.splitext(filename
)
2697 return '{0}.{1}'.format(
2698 name
if not expected_real_ext
or real_ext
[1:] == expected_real_ext
else filename
,
2702 def check_executable(exe
, args
=[]):
2703 """ Checks if the given binary is installed somewhere in PATH, and returns its name.
2704 args can be a list of arguments for a short output (like -version) """
2706 Popen([exe
] + args
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
).communicate_or_kill()
2712 def _get_exe_version_output(exe
, args
):
2714 # STDIN should be redirected too. On UNIX-like systems, ffmpeg triggers
2715 # SIGTTOU if yt-dlp is run in the background.
2716 # See https://github.com/ytdl-org/youtube-dl/issues/955#issuecomment-209789656
2718 [encodeArgument(exe
)] + args
, stdin
=subprocess
.PIPE
,
2719 stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
).communicate_or_kill()
2722 if isinstance(out
, bytes): # Python 2.x
2723 out
= out
.decode('ascii', 'ignore')
2727 def detect_exe_version(output
, version_re
=None, unrecognized
='present'):
2728 assert isinstance(output
, compat_str
)
2729 if version_re
is None:
2730 version_re
= r
'version\s+([-0-9._a-zA-Z]+)'
2731 m
= re
.search(version_re
, output
)
2738 def get_exe_version(exe
, args
=['--version'],
2739 version_re
=None, unrecognized
='present'):
2740 """ Returns the version of the specified executable,
2741 or False if the executable is not present """
2742 out
= _get_exe_version_output(exe
, args
)
2743 return detect_exe_version(out
, version_re
, unrecognized
) if out
else False
2746 class LazyList(collections
.abc
.Sequence
):
2747 ''' Lazy immutable list from an iterable
2748 Note that slices of a LazyList are lists and not LazyList'''
2750 class IndexError(IndexError):
2753 def __init__(self
, iterable
, *, reverse
=False, _cache
=None):
2754 self
.__iterable
= iter(iterable
)
2755 self
.__cache
= [] if _cache
is None else _cache
2756 self
.__reversed
= reverse
2760 # We need to consume the entire iterable to iterate in reverse
2761 yield from self
.exhaust()
2763 yield from self
.__cache
2764 for item
in self
.__iterable
:
2765 self
.__cache
.append(item
)
2768 def __exhaust(self
):
2769 self
.__cache
.extend(self
.__iterable
)
2770 # Discard the emptied iterable to make it pickle-able
2771 self
.__iterable
= []
2775 ''' Evaluate the entire iterable '''
2776 return self
.__exhaust
()[::-1 if self
.__reversed
else 1]
2779 def __reverse_index(x
):
2780 return None if x
is None else -(x
+ 1)
2782 def __getitem__(self
, idx
):
2783 if isinstance(idx
, slice):
2785 idx
= slice(self
.__reverse
_index
(idx
.start
), self
.__reverse
_index
(idx
.stop
), -(idx
.step
or 1))
2786 start
, stop
, step
= idx
.start
, idx
.stop
, idx
.step
or 1
2787 elif isinstance(idx
, int):
2789 idx
= self
.__reverse
_index
(idx
)
2790 start
, stop
, step
= idx
, idx
, 0
2792 raise TypeError('indices must be integers or slices')
2793 if ((start
or 0) < 0 or (stop
or 0) < 0
2794 or (start
is None and step
< 0)
2795 or (stop
is None and step
> 0)):
2796 # We need to consume the entire iterable to be able to slice from the end
2797 # Obviously, never use this with infinite iterables
2800 return self
.__cache
[idx
]
2801 except IndexError as e
:
2802 raise self
.IndexError(e
) from e
2803 n
= max(start
or 0, stop
or 0) - len(self
.__cache
) + 1
2805 self
.__cache
.extend(itertools
.islice(self
.__iterable
, n
))
2807 return self
.__cache
[idx
]
2808 except IndexError as e
:
2809 raise self
.IndexError(e
) from e
2813 self
[-1] if self
.__reversed
else self
[0]
2814 except self
.IndexError:
2820 return len(self
.__cache
)
2822 def __reversed__(self
):
2823 return type(self
)(self
.__iterable
, reverse
=not self
.__reversed
, _cache
=self
.__cache
)
2826 return type(self
)(self
.__iterable
, reverse
=self
.__reversed
, _cache
=self
.__cache
)
2829 # repr and str should mimic a list. So we exhaust the iterable
2830 return repr(self
.exhaust())
2833 return repr(self
.exhaust())
2838 class IndexError(IndexError):
2842 # This is only useful for tests
2843 return len(self
.getslice())
2845 def __init__(self
, pagefunc
, pagesize
, use_cache
=True):
2846 self
._pagefunc
= pagefunc
2847 self
._pagesize
= pagesize
2848 self
._pagecount
= float('inf')
2849 self
._use
_cache
= use_cache
2852 def getpage(self
, pagenum
):
2853 page_results
= self
._cache
.get(pagenum
)
2854 if page_results
is None:
2855 page_results
= [] if pagenum
> self
._pagecount
else list(self
._pagefunc
(pagenum
))
2857 self
._cache
[pagenum
] = page_results
2860 def getslice(self
, start
=0, end
=None):
2861 return list(self
._getslice
(start
, end
))
2863 def _getslice(self
, start
, end
):
2864 raise NotImplementedError('This method must be implemented by subclasses')
2866 def __getitem__(self
, idx
):
2867 assert self
._use
_cache
, 'Indexing PagedList requires cache'
2868 if not isinstance(idx
, int) or idx
< 0:
2869 raise TypeError('indices must be non-negative integers')
2870 entries
= self
.getslice(idx
, idx
+ 1)
2872 raise self
.IndexError()
2876 class OnDemandPagedList(PagedList
):
2877 def _getslice(self
, start
, end
):
2878 for pagenum
in itertools
.count(start
// self
._pagesize
):
2879 firstid
= pagenum
* self
._pagesize
2880 nextfirstid
= pagenum
* self
._pagesize
+ self
._pagesize
2881 if start
>= nextfirstid
:
2885 start
% self
._pagesize
2886 if firstid
<= start
< nextfirstid
2889 ((end
- 1) % self
._pagesize
) + 1
2890 if (end
is not None and firstid
<= end
<= nextfirstid
)
2894 page_results
= self
.getpage(pagenum
)
2896 self
._pagecount
= pagenum
- 1
2898 if startv
!= 0 or endv
is not None:
2899 page_results
= page_results
[startv
:endv
]
2900 yield from page_results
2902 # A little optimization - if current page is not "full", ie. does
2903 # not contain page_size videos then we can assume that this page
2904 # is the last one - there are no more ids on further pages -
2905 # i.e. no need to query again.
2906 if len(page_results
) + startv
< self
._pagesize
:
2909 # If we got the whole page, but the next page is not interesting,
2910 # break out early as well
2911 if end
== nextfirstid
:
2915 class InAdvancePagedList(PagedList
):
2916 def __init__(self
, pagefunc
, pagecount
, pagesize
):
2917 PagedList
.__init
__(self
, pagefunc
, pagesize
, True)
2918 self
._pagecount
= pagecount
2920 def _getslice(self
, start
, end
):
2921 start_page
= start
// self
._pagesize
2922 end_page
= self
._pagecount
if end
is None else min(self
._pagecount
, end
// self
._pagesize
+ 1)
2923 skip_elems
= start
- start_page
* self
._pagesize
2924 only_more
= None if end
is None else end
- start
2925 for pagenum
in range(start_page
, end_page
):
2926 page_results
= self
.getpage(pagenum
)
2928 page_results
= page_results
[skip_elems
:]
2930 if only_more
is not None:
2931 if len(page_results
) < only_more
:
2932 only_more
-= len(page_results
)
2934 yield from page_results
[:only_more
]
2936 yield from page_results
2939 def uppercase_escape(s
):
2940 unicode_escape
= codecs
.getdecoder('unicode_escape')
2942 r
'\\U[0-9a-fA-F]{8}',
2943 lambda m
: unicode_escape(m
.group(0))[0],
2947 def lowercase_escape(s
):
2948 unicode_escape
= codecs
.getdecoder('unicode_escape')
2950 r
'\\u[0-9a-fA-F]{4}',
2951 lambda m
: unicode_escape(m
.group(0))[0],
2955 def escape_rfc3986(s
):
2956 """Escape non-ASCII characters as suggested by RFC 3986"""
2957 if sys
.version_info
< (3, 0) and isinstance(s
, compat_str
):
2958 s
= s
.encode('utf-8')
2959 return compat_urllib_parse
.quote(s
, b
"%/;:@&=+$,!~*'()?#[]")
2962 def escape_url(url
):
2963 """Escape URL as suggested by RFC 3986"""
2964 url_parsed
= compat_urllib_parse_urlparse(url
)
2965 return url_parsed
._replace
(
2966 netloc
=url_parsed
.netloc
.encode('idna').decode('ascii'),
2967 path
=escape_rfc3986(url_parsed
.path
),
2968 params
=escape_rfc3986(url_parsed
.params
),
2969 query
=escape_rfc3986(url_parsed
.query
),
2970 fragment
=escape_rfc3986(url_parsed
.fragment
)
2975 return compat_parse_qs(compat_urllib_parse_urlparse(url
).query
)
2978 def read_batch_urls(batch_fd
):
2980 if not isinstance(url
, compat_str
):
2981 url
= url
.decode('utf-8', 'replace')
2982 BOM_UTF8
= ('\xef\xbb\xbf', '\ufeff')
2983 for bom
in BOM_UTF8
:
2984 if url
.startswith(bom
):
2985 url
= url
[len(bom
):]
2987 if not url
or url
.startswith(('#', ';', ']')):
2989 # "#" cannot be stripped out since it is part of the URI
2990 # However, it can be safely stipped out if follwing a whitespace
2991 return re
.split(r
'\s#', url
, 1)[0].rstrip()
2993 with contextlib
.closing(batch_fd
) as fd
:
2994 return [url
for url
in map(fixup
, fd
) if url
]
2997 def urlencode_postdata(*args
, **kargs
):
2998 return compat_urllib_parse_urlencode(*args
, **kargs
).encode('ascii')
3001 def update_url_query(url
, query
):
3004 parsed_url
= compat_urlparse
.urlparse(url
)
3005 qs
= compat_parse_qs(parsed_url
.query
)
3007 return compat_urlparse
.urlunparse(parsed_url
._replace
(
3008 query
=compat_urllib_parse_urlencode(qs
, True)))
3011 def update_Request(req
, url
=None, data
=None, headers
={}, query={}
):
3012 req_headers
= req
.headers
.copy()
3013 req_headers
.update(headers
)
3014 req_data
= data
or req
.data
3015 req_url
= update_url_query(url
or req
.get_full_url(), query
)
3016 req_get_method
= req
.get_method()
3017 if req_get_method
== 'HEAD':
3018 req_type
= HEADRequest
3019 elif req_get_method
== 'PUT':
3020 req_type
= PUTRequest
3022 req_type
= compat_urllib_request
.Request
3024 req_url
, data
=req_data
, headers
=req_headers
,
3025 origin_req_host
=req
.origin_req_host
, unverifiable
=req
.unverifiable
)
3026 if hasattr(req
, 'timeout'):
3027 new_req
.timeout
= req
.timeout
3031 def _multipart_encode_impl(data
, boundary
):
3032 content_type
= 'multipart/form-data; boundary=%s' % boundary
3035 for k
, v
in data
.items():
3036 out
+= b
'--' + boundary
.encode('ascii') + b
'\r\n'
3037 if isinstance(k
, compat_str
):
3038 k
= k
.encode('utf-8')
3039 if isinstance(v
, compat_str
):
3040 v
= v
.encode('utf-8')
3041 # RFC 2047 requires non-ASCII field names to be encoded, while RFC 7578
3042 # suggests sending UTF-8 directly. Firefox sends UTF-8, too
3043 content
= b
'Content-Disposition: form-data; name="' + k
+ b
'"\r\n\r\n' + v
+ b
'\r\n'
3044 if boundary
.encode('ascii') in content
:
3045 raise ValueError('Boundary overlaps with data')
3048 out
+= b
'--' + boundary
.encode('ascii') + b
'--\r\n'
3050 return out
, content_type
3053 def multipart_encode(data
, boundary
=None):
3055 Encode a dict to RFC 7578-compliant form-data
3058 A dict where keys and values can be either Unicode or bytes-like
3061 If specified a Unicode object, it's used as the boundary. Otherwise
3062 a random boundary is generated.
3064 Reference: https://tools.ietf.org/html/rfc7578
3066 has_specified_boundary
= boundary
is not None
3069 if boundary
is None:
3070 boundary
= '---------------' + str(random
.randrange(0x0fffffff, 0xffffffff))
3073 out
, content_type
= _multipart_encode_impl(data
, boundary
)
3076 if has_specified_boundary
:
3080 return out
, content_type
3083 def dict_get(d
, key_or_keys
, default
=None, skip_false_values
=True):
3084 if isinstance(key_or_keys
, (list, tuple)):
3085 for key
in key_or_keys
:
3086 if key
not in d
or d
[key
] is None or skip_false_values
and not d
[key
]:
3090 return d
.get(key_or_keys
, default
)
3093 def try_get(src
, getter
, expected_type
=None):
3094 for get
in variadic(getter
):
3097 except (AttributeError, KeyError, TypeError, IndexError):
3100 if expected_type
is None or isinstance(v
, expected_type
):
3104 def merge_dicts(*dicts
):
3106 for a_dict
in dicts
:
3107 for k
, v
in a_dict
.items():
3111 or (isinstance(v
, compat_str
) and v
3112 and isinstance(merged
[k
], compat_str
)
3113 and not merged
[k
])):
3118 def encode_compat_str(string
, encoding
=preferredencoding(), errors
='strict'):
3119 return string
if isinstance(string
, compat_str
) else compat_str(string
, encoding
, errors
)
3131 TV_PARENTAL_GUIDELINES
= {
3141 def parse_age_limit(s
):
3143 return s
if 0 <= s
<= 21 else None
3144 if not isinstance(s
, compat_basestring
):
3146 m
= re
.match(r
'^(?P<age>\d{1,2})\+?$', s
)
3148 return int(m
.group('age'))
3151 return US_RATINGS
[s
]
3152 m
= re
.match(r
'^TV[_-]?(%s)$' % '|'.join(k
[3:] for k
in TV_PARENTAL_GUIDELINES
), s
)
3154 return TV_PARENTAL_GUIDELINES
['TV-' + m
.group(1)]
3158 def strip_jsonp(code
):
3161 (?:window\.)?(?P<func_name>[a-zA-Z0-9_.$]*)
3162 (?:\s*&&\s*(?P=func_name))?
3163 \s*\(\s*(?P<callback_data>.*)\);?
3164 \s*?(?://[^\n]*)*$''',
3165 r
'\g<callback_data>', code
)
3168 def js_to_json(code
, vars={}):
3169 # vars is a dict of var, val pairs to substitute
3170 COMMENT_RE
= r
'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n'
3171 SKIP_RE
= r
'\s*(?:{comment})?\s*'.format(comment
=COMMENT_RE
)
3173 (r
'(?s)^(0[xX][0-9a-fA-F]+){skip}:?$'.format(skip
=SKIP_RE
), 16),
3174 (r
'(?s)^(0+[0-7]+){skip}:?$'.format(skip
=SKIP_RE
), 8),
3179 if v
in ('true', 'false', 'null'):
3181 elif v
in ('undefined', 'void 0'):
3183 elif v
.startswith('/*') or v
.startswith('//') or v
.startswith('!') or v
== ',':
3186 if v
[0] in ("'", '"'):
3187 v
= re
.sub(r
'(?s)\\.|"', lambda m
: {
3192 }.get(m
.group(0), m
.group(0)), v
[1:-1])
3194 for regex
, base
in INTEGER_TABLE
:
3195 im
= re
.match(regex
, v
)
3197 i
= int(im
.group(1), base
)
3198 return '"%d":' % i
if v
.endswith(':') else '%d' % i
3205 code
= re
.sub(r
'new Date\((".+")\)', r
'\g<1>', code
)
3207 return re
.sub(r
'''(?sx)
3208 "(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"|
3209 '(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'|
3210 {comment}|,(?={skip}[\]}}])|
3211 void\s0|(?:(?<![0-9])[eE]|[a-df-zA-DF-Z_$])[.a-zA-Z_$0-9]*|
3212 \b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:{skip}:)?|
3215 '''.format(comment
=COMMENT_RE
, skip
=SKIP_RE
), fix_kv
, code
)
3218 def qualities(quality_ids
):
3219 """ Get a numeric quality value out of a list of possible values """
3222 return quality_ids
.index(qid
)
3228 POSTPROCESS_WHEN
= {'pre_process', 'after_filter', 'before_dl', 'after_move', 'post_process', 'after_video', 'playlist'}
3232 'default': '%(title)s [%(id)s].%(ext)s',
3233 'chapter': '%(title)s - %(section_number)03d %(section_title)s [%(id)s].%(ext)s',
3239 'description': 'description',
3240 'annotation': 'annotations.xml',
3241 'infojson': 'info.json',
3244 'pl_thumbnail': None,
3245 'pl_description': 'description',
3246 'pl_infojson': 'info.json',
3249 # As of [1] format syntax is:
3250 # %[mapping_key][conversion_flags][minimum_width][.precision][length_modifier]type
3251 # 1. https://docs.python.org/2/library/stdtypes.html#string-formatting
3252 STR_FORMAT_RE_TMPL
= r
'''(?x)
3253 (?<!%)(?P<prefix>(?:%%)*)
3255 (?P<has_key>\((?P<key>{0})\))?
3257 (?P<conversion>[#0\-+ ]+)?
3259 (?P<precision>\.\d+)?
3260 (?P<len_mod>[hlL])? # unused in python
3261 {1} # conversion type
3266 STR_FORMAT_TYPES
= 'diouxXeEfFgGcrs'
3269 def limit_length(s
, length
):
3270 """ Add ellipses to overly long strings """
3275 return s
[:length
- len(ELLIPSES
)] + ELLIPSES
3279 def version_tuple(v
):
3280 return tuple(int(e
) for e
in re
.split(r
'[-.]', v
))
3283 def is_outdated_version(version
, limit
, assume_new
=True):
3285 return not assume_new
3287 return version_tuple(version
) < version_tuple(limit
)
3289 return not assume_new
3292 def ytdl_is_updateable():
3293 """ Returns if yt-dlp can be updated with -U """
3295 from .update
import is_non_updateable
3297 return not is_non_updateable()
3300 def args_to_str(args
):
3301 # Get a short string representation for a subprocess command
3302 return ' '.join(compat_shlex_quote(a
) for a
in args
)
3305 def error_to_compat_str(err
):
3307 # On python 2 error byte string must be decoded with proper
3308 # encoding rather than ascii
3309 if sys
.version_info
[0] < 3:
3310 err_str
= err_str
.decode(preferredencoding())
3314 def mimetype2ext(mt
):
3318 mt
, _
, params
= mt
.partition(';')
3323 # Per RFC 3003, audio/mpeg can be .mp1, .mp2 or .mp3. Here use .mp3 as
3324 # it's the most popular one
3325 'audio/mpeg': 'mp3',
3326 'audio/x-wav': 'wav',
3328 'audio/wave': 'wav',
3331 ext
= FULL_MAP
.get(mt
)
3337 'smptett+xml': 'tt',
3341 'x-mp4-fragmented': 'mp4',
3342 'x-ms-sami': 'sami',
3345 'x-mpegurl': 'm3u8',
3346 'vnd.apple.mpegurl': 'm3u8',
3350 'vnd.ms-sstr+xml': 'ism',
3354 'filmstrip+json': 'fs',
3358 _
, _
, subtype
= mt
.rpartition('/')
3359 ext
= SUBTYPE_MAP
.get(subtype
.lower())
3370 _
, _
, suffix
= subtype
.partition('+')
3371 ext
= SUFFIX_MAP
.get(suffix
)
3375 return subtype
.replace('+', '.')
3378 def ext2mimetype(ext_or_url
):
3381 if '.' not in ext_or_url
:
3382 ext_or_url
= f
'file.{ext_or_url}'
3383 return mimetypes
.guess_type(ext_or_url
)[0]
3386 def parse_codecs(codecs_str
):
3387 # http://tools.ietf.org/html/rfc6381
3390 split_codecs
= list(filter(None, map(
3391 str.strip
, codecs_str
.strip().strip(',').split(','))))
3392 vcodec
, acodec
, tcodec
, hdr
= None, None, None, None
3393 for full_codec
in split_codecs
:
3394 parts
= full_codec
.split('.')
3395 codec
= parts
[0].replace('0', '')
3396 if codec
in ('avc1', 'avc2', 'avc3', 'avc4', 'vp9', 'vp8', 'hev1', 'hev2',
3397 'h263', 'h264', 'mp4v', 'hvc1', 'av1', 'theora', 'dvh1', 'dvhe'):
3399 vcodec
= '.'.join(parts
[:4]) if codec
in ('vp9', 'av1', 'hvc1') else full_codec
3400 if codec
in ('dvh1', 'dvhe'):
3402 elif codec
== 'av1' and len(parts
) > 3 and parts
[3] == '10':
3404 elif full_codec
.replace('0', '').startswith('vp9.2'):
3406 elif codec
in ('flac', 'mp4a', 'opus', 'vorbis', 'mp3', 'aac', 'ac-3', 'ec-3', 'eac3', 'dtsc', 'dtse', 'dtsh', 'dtsl'):
3409 elif codec
in ('stpp', 'wvtt',):
3413 write_string('WARNING: Unknown codec %s\n' % full_codec
, sys
.stderr
)
3414 if vcodec
or acodec
or tcodec
:
3416 'vcodec': vcodec
or 'none',
3417 'acodec': acodec
or 'none',
3418 'dynamic_range': hdr
,
3419 **({'tcodec': tcodec}
if tcodec
is not None else {}),
3421 elif len(split_codecs
) == 2:
3423 'vcodec': split_codecs
[0],
3424 'acodec': split_codecs
[1],
3429 def urlhandle_detect_ext(url_handle
):
3430 getheader
= url_handle
.headers
.get
3432 cd
= getheader('Content-Disposition')
3434 m
= re
.match(r
'attachment;\s*filename="(?P<filename>[^"]+)"', cd
)
3436 e
= determine_ext(m
.group('filename'), default_ext
=None)
3440 return mimetype2ext(getheader('Content-Type'))
3443 def encode_data_uri(data
, mime_type
):
3444 return 'data:%s;base64,%s' % (mime_type
, base64
.b64encode(data
).decode('ascii'))
3447 def age_restricted(content_limit
, age_limit
):
3448 """ Returns True iff the content should be blocked """
3450 if age_limit
is None: # No limit set
3452 if content_limit
is None:
3453 return False # Content available for everyone
3454 return age_limit
< content_limit
3457 def is_html(first_bytes
):
3458 """ Detect whether a file contains HTML by examining its first bytes. """
3461 (b
'\xef\xbb\xbf', 'utf-8'),
3462 (b
'\x00\x00\xfe\xff', 'utf-32-be'),
3463 (b
'\xff\xfe\x00\x00', 'utf-32-le'),
3464 (b
'\xff\xfe', 'utf-16-le'),
3465 (b
'\xfe\xff', 'utf-16-be'),
3467 for bom
, enc
in BOMS
:
3468 if first_bytes
.startswith(bom
):
3469 s
= first_bytes
[len(bom
):].decode(enc
, 'replace')
3472 s
= first_bytes
.decode('utf-8', 'replace')
3474 return re
.match(r
'^\s*<', s
)
3477 def determine_protocol(info_dict
):
3478 protocol
= info_dict
.get('protocol')
3479 if protocol
is not None:
3482 url
= sanitize_url(info_dict
['url'])
3483 if url
.startswith('rtmp'):
3485 elif url
.startswith('mms'):
3487 elif url
.startswith('rtsp'):
3490 ext
= determine_ext(url
)
3496 return compat_urllib_parse_urlparse(url
).scheme
3499 def render_table(header_row
, data
, delim
=False, extra_gap
=0, hide_empty
=False):
3500 """ Render a list of rows, each as a list of values.
3501 Text after a \t will be right aligned """
3503 return len(remove_terminal_sequences(string
).replace('\t', ''))
3505 def get_max_lens(table
):
3506 return [max(width(str(v
)) for v
in col
) for col
in zip(*table
)]
3508 def filter_using_list(row
, filterArray
):
3509 return [col
for take
, col
in itertools
.zip_longest(filterArray
, row
, fillvalue
=True) if take
]
3511 max_lens
= get_max_lens(data
) if hide_empty
else []
3512 header_row
= filter_using_list(header_row
, max_lens
)
3513 data
= [filter_using_list(row
, max_lens
) for row
in data
]
3515 table
= [header_row
] + data
3516 max_lens
= get_max_lens(table
)
3519 table
= [header_row
, [delim
* (ml
+ extra_gap
) for ml
in max_lens
]] + data
3520 table
[1][-1] = table
[1][-1][:-extra_gap
* len(delim
)] # Remove extra_gap from end of delimiter
3522 for pos
, text
in enumerate(map(str, row
)):
3524 row
[pos
] = text
.replace('\t', ' ' * (max_lens
[pos
] - width(text
))) + ' ' * extra_gap
3526 row
[pos
] = text
+ ' ' * (max_lens
[pos
] - width(text
) + extra_gap
)
3527 ret
= '\n'.join(''.join(row
).rstrip() for row
in table
)
3531 def _match_one(filter_part
, dct
, incomplete
):
3532 # TODO: Generalize code with YoutubeDL._build_format_filter
3533 STRING_OPERATORS
= {
3534 '*=': operator
.contains
,
3535 '^=': lambda attr
, value
: attr
.startswith(value
),
3536 '$=': lambda attr
, value
: attr
.endswith(value
),
3537 '~=': lambda attr
, value
: re
.search(value
, attr
),
3539 COMPARISON_OPERATORS
= {
3541 '<=': operator
.le
, # "<=" must be defined above "<"
3548 if isinstance(incomplete
, bool):
3549 is_incomplete
= lambda _
: incomplete
3551 is_incomplete
= lambda k
: k
in incomplete
3553 operator_rex
= re
.compile(r
'''(?x)\s*
3555 \s*(?P<negation>!\s*)?(?P<op>%s)(?P<none_inclusive>\s*\?)?\s*
3557 (?P<quote>["\'])(?P
<quotedstrval
>.+?
)(?P
=quote
)|
3561 ''' % '|'.join(map(re.escape, COMPARISON_OPERATORS.keys())))
3562 m = operator_rex.search(filter_part)
3565 unnegated_op = COMPARISON_OPERATORS[m['op']]
3567 op = lambda attr, value: not unnegated_op(attr, value)
3570 comparison_value = m['quotedstrval'] or m['strval'] or m['intval']
3572 comparison_value = comparison_value.replace(r'\%s' % m['quote'], m['quote'])
3573 actual_value = dct.get(m['key'])
3574 numeric_comparison = None
3575 if isinstance(actual_value, compat_numeric_types):
3576 # If the original field is a string and matching comparisonvalue is
3577 # a number we should respect the origin of the original field
3578 # and process comparison value as a string (see
3579 # https://github.com/ytdl-org/youtube-dl/issues/11082)
3581 numeric_comparison = int(comparison_value)
3583 numeric_comparison = parse_filesize(comparison_value)
3584 if numeric_comparison is None:
3585 numeric_comparison = parse_filesize(f'{comparison_value}B')
3586 if numeric_comparison is None:
3587 numeric_comparison = parse_duration(comparison_value)
3588 if numeric_comparison is not None and m['op'] in STRING_OPERATORS:
3589 raise ValueError('Operator %s only supports string values!' % m['op'])
3590 if actual_value is None:
3591 return is_incomplete(m['key']) or m['none_inclusive']
3592 return op(actual_value, comparison_value if numeric_comparison is None else numeric_comparison)
3595 '': lambda v: (v is True) if isinstance(v, bool) else (v is not None),
3596 '!': lambda v: (v is False) if isinstance(v, bool) else (v is None),
3598 operator_rex = re.compile(r'''(?x
)\s
*
3599 (?P
<op
>%s)\s
*(?P
<key
>[a
-z_
]+)
3601 ''' % '|'.join(map(re.escape, UNARY_OPERATORS.keys())))
3602 m = operator_rex.search(filter_part)
3604 op = UNARY_OPERATORS[m.group('op')]
3605 actual_value = dct.get(m.group('key'))
3606 if is_incomplete(m.group('key')) and actual_value is None:
3608 return op(actual_value)
3610 raise ValueError('Invalid filter part %r' % filter_part)
3613 def match_str(filter_str, dct, incomplete=False):
3614 """ Filter a dictionary with a simple string syntax.
3615 @returns Whether the filter passes
3616 @param incomplete Set of keys that is expected to be missing from dct.
3617 Can be True/False to indicate all/none of the keys may be missing.
3618 All conditions on incomplete keys pass if the key is missing
3621 _match_one(filter_part.replace(r'\&', '&'), dct, incomplete)
3622 for filter_part in re.split(r'(?<!\\)&', filter_str))
3625 def match_filter_func(filters):
3628 filters = variadic(filters)
3630 def _match_func(info_dict, *args, **kwargs):
3631 if any(match_str(f, info_dict, *args, **kwargs) for f in filters):
3634 video_title = info_dict.get('title') or info_dict.get('id') or 'video'
3635 filter_str = ') | ('.join(map(str.strip, filters))
3636 return f'{video_title} does not pass filter ({filter_str}), skipping ..'
3640 def parse_dfxp_time_expr(time_expr):
3644 mobj = re.match(r'^(?P<time_offset>\d+(?:\.\d+)?)s?$', time_expr)
3646 return float(mobj.group('time_offset'))
3648 mobj = re.match(r'^(\d+):(\d\d):(\d\d(?:(?:\.|:)\d+)?)$', time_expr)
3650 return 3600 * int(mobj.group(1)) + 60 * int(mobj.group(2)) + float(mobj.group(3).replace(':', '.'))
3653 def srt_subtitles_timecode(seconds):
3654 return '%02d:%02d:%02d,%03d' % timetuple_from_msec(seconds * 1000)
3657 def ass_subtitles_timecode(seconds):
3658 time = timetuple_from_msec(seconds * 1000)
3659 return '%01d:%02d:%02d.%02d' % (*time[:-1], time.milliseconds / 10)
3662 def dfxp2srt(dfxp_data):
3664 @param dfxp_data A
bytes-like
object containing DFXP data
3665 @returns A
unicode object containing converted SRT data
3667 LEGACY_NAMESPACES = (
3668 (b'http://www.w3.org/ns/ttml', [
3669 b'http://www.w3.org/2004/11/ttaf1',
3670 b'http://www.w3.org/2006/04/ttaf1',
3671 b'http://www.w3.org/2006/10/ttaf1',
3673 (b'http://www.w3.org/ns/ttml#styling', [
3674 b'http://www.w3.org/ns/ttml#style',
3678 SUPPORTED_STYLING = [
3687 _x = functools.partial(xpath_with_ns, ns_map={
3688 'xml': 'http://www.w3.org/XML/1998/namespace',
3689 'ttml': 'http://www.w3.org/ns/ttml',
3690 'tts': 'http://www.w3.org/ns/ttml#styling',
3696 class TTMLPElementParser(object):
3698 _unclosed_elements = []
3699 _applied_styles = []
3701 def start(self, tag, attrib):
3702 if tag in (_x('ttml:br'), 'br'):
3705 unclosed_elements = []
3707 element_style_id = attrib.get('style')
3709 style.update(default_style)
3710 if element_style_id:
3711 style.update(styles.get(element_style_id, {}))
3712 for prop in SUPPORTED_STYLING:
3713 prop_val = attrib.get(_x('tts:' + prop))
3715 style[prop] = prop_val
3718 for k, v in sorted(style.items()):
3719 if self._applied_styles and self._applied_styles[-1].get(k) == v:
3722 font += ' color="%s"' % v
3723 elif k == 'fontSize':
3724 font += ' size="%s"' % v
3725 elif k == 'fontFamily':
3726 font += ' face="%s"' % v
3727 elif k == 'fontWeight' and v == 'bold':
3729 unclosed_elements.append('b')
3730 elif k == 'fontStyle' and v == 'italic':
3732 unclosed_elements.append('i')
3733 elif k == 'textDecoration' and v == 'underline':
3735 unclosed_elements.append('u')
3737 self._out += '<font' + font + '>'
3738 unclosed_elements.append('font')
3740 if self._applied_styles:
3741 applied_style.update(self._applied_styles[-1])
3742 applied_style.update(style)
3743 self._applied_styles.append(applied_style)
3744 self._unclosed_elements.append(unclosed_elements)
3747 if tag not in (_x('ttml:br'), 'br'):
3748 unclosed_elements = self._unclosed_elements.pop()
3749 for element in reversed(unclosed_elements):
3750 self._out += '</%s>' % element
3751 if unclosed_elements and self._applied_styles:
3752 self._applied_styles.pop()
3754 def data(self, data):
3758 return self._out.strip()
3760 def parse_node(node):
3761 target = TTMLPElementParser()
3762 parser = xml.etree.ElementTree.XMLParser(target=target)
3763 parser.feed(xml.etree.ElementTree.tostring(node))
3764 return parser.close()
3766 for k, v in LEGACY_NAMESPACES:
3768 dfxp_data = dfxp_data.replace(ns, k)
3770 dfxp = compat_etree_fromstring(dfxp_data)
3772 paras = dfxp.findall(_x('.//ttml:p')) or dfxp.findall('.//p')
3775 raise ValueError('Invalid dfxp/TTML subtitle')
3779 for style in dfxp.findall(_x('.//ttml:style')):
3780 style_id = style.get('id') or style.get(_x('xml:id'))
3783 parent_style_id = style.get('style')
3785 if parent_style_id not in styles:
3788 styles[style_id] = styles[parent_style_id].copy()
3789 for prop in SUPPORTED_STYLING:
3790 prop_val = style.get(_x('tts:' + prop))
3792 styles.setdefault(style_id, {})[prop] = prop_val
3798 for p in ('body', 'div'):
3799 ele = xpath_element(dfxp, [_x('.//ttml:' + p), './/' + p])
3802 style = styles.get(ele.get('style'))
3805 default_style.update(style)
3807 for para, index in zip(paras, itertools.count(1)):
3808 begin_time = parse_dfxp_time_expr(para.attrib.get('begin'))
3809 end_time = parse_dfxp_time_expr(para.attrib.get('end'))
3810 dur = parse_dfxp_time_expr(para.attrib.get('dur'))
3811 if begin_time is None:
3816 end_time = begin_time + dur
3817 out.append('%d\n%s --> %s\n%s\n\n' % (
3819 srt_subtitles_timecode(begin_time),
3820 srt_subtitles_timecode(end_time),
3826 def cli_option(params, command_option, param):
3827 param = params.get(param)
3829 param = compat_str(param)
3830 return [command_option, param] if param is not None else []
3833 def cli_bool_option(params, command_option, param, true_value='true', false_value='false', separator=None):
3834 param = params.get(param)
3837 assert isinstance(param, bool)
3839 return [command_option + separator + (true_value if param else false_value)]
3840 return [command_option, true_value if param else false_value]
3843 def cli_valueless_option(params, command_option, param, expected_value=True):
3844 param = params.get(param)
3845 return [command_option] if param == expected_value else []
3848 def cli_configuration_args(argdict, keys, default=[], use_compat=True):
3849 if isinstance(argdict, (list, tuple)): # for backward compatibility
3856 assert isinstance(argdict, dict)
3858 assert isinstance(keys, (list, tuple))
3859 for key_list in keys:
3860 arg_list = list(filter(
3861 lambda x: x is not None,
3862 [argdict.get(key.lower()) for key in variadic(key_list)]))
3864 return [arg for args in arg_list for arg in args]
3868 def _configuration_args(main_key, argdict, exe, keys=None, default=[], use_compat=True):
3869 main_key, exe = main_key.lower(), exe.lower()
3870 root_key = exe if main_key == exe else f'{main_key}+{exe}'
3871 keys = [f'{root_key}{k}' for k in (keys or [''])]
3872 if root_key in keys:
3874 keys.append((main_key, exe))
3875 keys.append('default')
3878 return cli_configuration_args(argdict, keys, default, use_compat)
3881 class ISO639Utils(object):
3882 # See http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
3941 'iw': 'heb', # Replaced by he in 1989 revision
3951 'in': 'ind', # Replaced by id in 1989 revision
4066 'ji': 'yid', # Replaced by yi in 1989 revision
4074 def short2long(cls, code):
4075 """Convert language code from ISO 639-1 to ISO 639-2/T"""
4076 return cls._lang_map.get(code[:2])
4079 def long2short(cls, code):
4080 """Convert language code from ISO 639-2/T to ISO 639-1"""
4081 for short_name, long_name in cls._lang_map.items():
4082 if long_name == code:
4086 class ISO3166Utils(object):
4087 # From http://data.okfn.org/data/core/country-list
4089 'AF': 'Afghanistan',
4090 'AX': 'Åland Islands',
4093 'AS': 'American Samoa',
4098 'AG': 'Antigua and Barbuda',
4115 'BO': 'Bolivia, Plurinational State of',
4116 'BQ': 'Bonaire, Sint Eustatius and Saba',
4117 'BA': 'Bosnia and Herzegovina',
4119 'BV': 'Bouvet Island',
4121 'IO': 'British Indian Ocean Territory',
4122 'BN': 'Brunei Darussalam',
4124 'BF': 'Burkina Faso',
4130 'KY': 'Cayman Islands',
4131 'CF': 'Central African Republic',
4135 'CX': 'Christmas Island',
4136 'CC': 'Cocos (Keeling) Islands',
4140 'CD': 'Congo, the Democratic Republic of the',
4141 'CK': 'Cook Islands',
4143 'CI': 'Côte d\'Ivoire',
4148 'CZ': 'Czech Republic',
4152 'DO': 'Dominican Republic',
4155 'SV': 'El Salvador',
4156 'GQ': 'Equatorial Guinea',
4160 'FK': 'Falkland Islands (Malvinas)',
4161 'FO': 'Faroe Islands',
4165 'GF': 'French Guiana',
4166 'PF': 'French Polynesia',
4167 'TF': 'French Southern Territories',
4182 'GW': 'Guinea-Bissau',
4185 'HM': 'Heard Island and McDonald Islands',
4186 'VA': 'Holy See (Vatican City State)',
4193 'IR': 'Iran, Islamic Republic of',
4196 'IM': 'Isle of Man',
4206 'KP': 'Korea, Democratic People\'s Republic of',
4207 'KR': 'Korea, Republic of',
4210 'LA': 'Lao People\'s Democratic Republic',
4216 'LI': 'Liechtenstein',
4220 'MK': 'Macedonia, the Former Yugoslav Republic of',
4227 'MH': 'Marshall Islands',
4233 'FM': 'Micronesia, Federated States of',
4234 'MD': 'Moldova, Republic of',
4245 'NL': 'Netherlands',
4246 'NC': 'New Caledonia',
4247 'NZ': 'New Zealand',
4252 'NF': 'Norfolk Island',
4253 'MP': 'Northern Mariana Islands',
4258 'PS': 'Palestine, State of',
4260 'PG': 'Papua New Guinea',
4263 'PH': 'Philippines',
4267 'PR': 'Puerto Rico',
4271 'RU': 'Russian Federation',
4273 'BL': 'Saint Barthélemy',
4274 'SH': 'Saint Helena, Ascension and Tristan da Cunha',
4275 'KN': 'Saint Kitts and Nevis',
4276 'LC': 'Saint Lucia',
4277 'MF': 'Saint Martin (French part)',
4278 'PM': 'Saint Pierre and Miquelon',
4279 'VC': 'Saint Vincent and the Grenadines',
4282 'ST': 'Sao Tome and Principe',
4283 'SA': 'Saudi Arabia',
4287 'SL': 'Sierra Leone',
4289 'SX': 'Sint Maarten (Dutch part)',
4292 'SB': 'Solomon Islands',
4294 'ZA': 'South Africa',
4295 'GS': 'South Georgia and the South Sandwich Islands',
4296 'SS': 'South Sudan',
4301 'SJ': 'Svalbard and Jan Mayen',
4304 'CH': 'Switzerland',
4305 'SY': 'Syrian Arab Republic',
4306 'TW': 'Taiwan, Province of China',
4308 'TZ': 'Tanzania, United Republic of',
4310 'TL': 'Timor-Leste',
4314 'TT': 'Trinidad and Tobago',
4317 'TM': 'Turkmenistan',
4318 'TC': 'Turks and Caicos Islands',
4322 'AE': 'United Arab Emirates',
4323 'GB': 'United Kingdom',
4324 'US': 'United States',
4325 'UM': 'United States Minor Outlying Islands',
4329 'VE': 'Venezuela, Bolivarian Republic of',
4331 'VG': 'Virgin Islands, British',
4332 'VI': 'Virgin Islands, U.S.',
4333 'WF': 'Wallis and Futuna',
4334 'EH': 'Western Sahara',
4341 def short2full(cls, code):
4342 """Convert an ISO 3166-2 country code to the corresponding full name"""
4343 return cls._country_map.get(code.upper())
4346 class GeoUtils(object):
4347 # Major IPv4 address blocks per country
4349 'AD': '46.172.224.0/19',
4350 'AE': '94.200.0.0/13',
4351 'AF': '149.54.0.0/17',
4352 'AG': '209.59.64.0/18',
4353 'AI': '204.14.248.0/21',
4354 'AL': '46.99.0.0/16',
4355 'AM': '46.70.0.0/15',
4356 'AO': '105.168.0.0/13',
4357 'AP': '182.50.184.0/21',
4358 'AQ': '23.154.160.0/24',
4359 'AR': '181.0.0.0/12',
4360 'AS': '202.70.112.0/20',
4361 'AT': '77.116.0.0/14',
4362 'AU': '1.128.0.0/11',
4363 'AW': '181.41.0.0/18',
4364 'AX': '185.217.4.0/22',
4365 'AZ': '5.197.0.0/16',
4366 'BA': '31.176.128.0/17',
4367 'BB': '65.48.128.0/17',
4368 'BD': '114.130.0.0/16',
4370 'BF': '102.178.0.0/15',
4371 'BG': '95.42.0.0/15',
4372 'BH': '37.131.0.0/17',
4373 'BI': '154.117.192.0/18',
4374 'BJ': '137.255.0.0/16',
4375 'BL': '185.212.72.0/23',
4376 'BM': '196.12.64.0/18',
4377 'BN': '156.31.0.0/16',
4378 'BO': '161.56.0.0/16',
4379 'BQ': '161.0.80.0/20',
4380 'BR': '191.128.0.0/12',
4381 'BS': '24.51.64.0/18',
4382 'BT': '119.2.96.0/19',
4383 'BW': '168.167.0.0/16',
4384 'BY': '178.120.0.0/13',
4385 'BZ': '179.42.192.0/18',
4386 'CA': '99.224.0.0/11',
4387 'CD': '41.243.0.0/16',
4388 'CF': '197.242.176.0/21',
4389 'CG': '160.113.0.0/16',
4390 'CH': '85.0.0.0/13',
4391 'CI': '102.136.0.0/14',
4392 'CK': '202.65.32.0/19',
4393 'CL': '152.172.0.0/14',
4394 'CM': '102.244.0.0/14',
4395 'CN': '36.128.0.0/10',
4396 'CO': '181.240.0.0/12',
4397 'CR': '201.192.0.0/12',
4398 'CU': '152.206.0.0/15',
4399 'CV': '165.90.96.0/19',
4400 'CW': '190.88.128.0/17',
4401 'CY': '31.153.0.0/16',
4402 'CZ': '88.100.0.0/14',
4404 'DJ': '197.241.0.0/17',
4405 'DK': '87.48.0.0/12',
4406 'DM': '192.243.48.0/20',
4407 'DO': '152.166.0.0/15',
4408 'DZ': '41.96.0.0/12',
4409 'EC': '186.68.0.0/15',
4410 'EE': '90.190.0.0/15',
4411 'EG': '156.160.0.0/11',
4412 'ER': '196.200.96.0/20',
4413 'ES': '88.0.0.0/11',
4414 'ET': '196.188.0.0/14',
4415 'EU': '2.16.0.0/13',
4416 'FI': '91.152.0.0/13',
4417 'FJ': '144.120.0.0/16',
4418 'FK': '80.73.208.0/21',
4419 'FM': '119.252.112.0/20',
4420 'FO': '88.85.32.0/19',
4422 'GA': '41.158.0.0/15',
4424 'GD': '74.122.88.0/21',
4425 'GE': '31.146.0.0/16',
4426 'GF': '161.22.64.0/18',
4427 'GG': '62.68.160.0/19',
4428 'GH': '154.160.0.0/12',
4429 'GI': '95.164.0.0/16',
4430 'GL': '88.83.0.0/19',
4431 'GM': '160.182.0.0/15',
4432 'GN': '197.149.192.0/18',
4433 'GP': '104.250.0.0/19',
4434 'GQ': '105.235.224.0/20',
4435 'GR': '94.64.0.0/13',
4436 'GT': '168.234.0.0/16',
4437 'GU': '168.123.0.0/16',
4438 'GW': '197.214.80.0/20',
4439 'GY': '181.41.64.0/18',
4440 'HK': '113.252.0.0/14',
4441 'HN': '181.210.0.0/16',
4442 'HR': '93.136.0.0/13',
4443 'HT': '148.102.128.0/17',
4444 'HU': '84.0.0.0/14',
4445 'ID': '39.192.0.0/10',
4446 'IE': '87.32.0.0/12',
4447 'IL': '79.176.0.0/13',
4448 'IM': '5.62.80.0/20',
4449 'IN': '117.192.0.0/10',
4450 'IO': '203.83.48.0/21',
4451 'IQ': '37.236.0.0/14',
4452 'IR': '2.176.0.0/12',
4453 'IS': '82.221.0.0/16',
4454 'IT': '79.0.0.0/10',
4455 'JE': '87.244.64.0/18',
4456 'JM': '72.27.0.0/17',
4457 'JO': '176.29.0.0/16',
4458 'JP': '133.0.0.0/8',
4459 'KE': '105.48.0.0/12',
4460 'KG': '158.181.128.0/17',
4461 'KH': '36.37.128.0/17',
4462 'KI': '103.25.140.0/22',
4463 'KM': '197.255.224.0/20',
4464 'KN': '198.167.192.0/19',
4465 'KP': '175.45.176.0/22',
4466 'KR': '175.192.0.0/10',
4467 'KW': '37.36.0.0/14',
4468 'KY': '64.96.0.0/15',
4469 'KZ': '2.72.0.0/13',
4470 'LA': '115.84.64.0/18',
4471 'LB': '178.135.0.0/16',
4472 'LC': '24.92.144.0/20',
4473 'LI': '82.117.0.0/19',
4474 'LK': '112.134.0.0/15',
4475 'LR': '102.183.0.0/16',
4476 'LS': '129.232.0.0/17',
4477 'LT': '78.56.0.0/13',
4478 'LU': '188.42.0.0/16',
4479 'LV': '46.109.0.0/16',
4480 'LY': '41.252.0.0/14',
4481 'MA': '105.128.0.0/11',
4482 'MC': '88.209.64.0/18',
4483 'MD': '37.246.0.0/16',
4484 'ME': '178.175.0.0/17',
4485 'MF': '74.112.232.0/21',
4486 'MG': '154.126.0.0/17',
4487 'MH': '117.103.88.0/21',
4488 'MK': '77.28.0.0/15',
4489 'ML': '154.118.128.0/18',
4490 'MM': '37.111.0.0/17',
4491 'MN': '49.0.128.0/17',
4492 'MO': '60.246.0.0/16',
4493 'MP': '202.88.64.0/20',
4494 'MQ': '109.203.224.0/19',
4495 'MR': '41.188.64.0/18',
4496 'MS': '208.90.112.0/22',
4497 'MT': '46.11.0.0/16',
4498 'MU': '105.16.0.0/12',
4499 'MV': '27.114.128.0/18',
4500 'MW': '102.70.0.0/15',
4501 'MX': '187.192.0.0/11',
4502 'MY': '175.136.0.0/13',
4503 'MZ': '197.218.0.0/15',
4504 'NA': '41.182.0.0/16',
4505 'NC': '101.101.0.0/18',
4506 'NE': '197.214.0.0/18',
4507 'NF': '203.17.240.0/22',
4508 'NG': '105.112.0.0/12',
4509 'NI': '186.76.0.0/15',
4510 'NL': '145.96.0.0/11',
4511 'NO': '84.208.0.0/13',
4512 'NP': '36.252.0.0/15',
4513 'NR': '203.98.224.0/19',
4514 'NU': '49.156.48.0/22',
4515 'NZ': '49.224.0.0/14',
4516 'OM': '5.36.0.0/15',
4517 'PA': '186.72.0.0/15',
4518 'PE': '186.160.0.0/14',
4519 'PF': '123.50.64.0/18',
4520 'PG': '124.240.192.0/19',
4521 'PH': '49.144.0.0/13',
4522 'PK': '39.32.0.0/11',
4523 'PL': '83.0.0.0/11',
4524 'PM': '70.36.0.0/20',
4525 'PR': '66.50.0.0/16',
4526 'PS': '188.161.0.0/16',
4527 'PT': '85.240.0.0/13',
4528 'PW': '202.124.224.0/20',
4529 'PY': '181.120.0.0/14',
4530 'QA': '37.210.0.0/15',
4531 'RE': '102.35.0.0/16',
4532 'RO': '79.112.0.0/13',
4533 'RS': '93.86.0.0/15',
4534 'RU': '5.136.0.0/13',
4535 'RW': '41.186.0.0/16',
4536 'SA': '188.48.0.0/13',
4537 'SB': '202.1.160.0/19',
4538 'SC': '154.192.0.0/11',
4539 'SD': '102.120.0.0/13',
4540 'SE': '78.64.0.0/12',
4541 'SG': '8.128.0.0/10',
4542 'SI': '188.196.0.0/14',
4543 'SK': '78.98.0.0/15',
4544 'SL': '102.143.0.0/17',
4545 'SM': '89.186.32.0/19',
4546 'SN': '41.82.0.0/15',
4547 'SO': '154.115.192.0/18',
4548 'SR': '186.179.128.0/17',
4549 'SS': '105.235.208.0/21',
4550 'ST': '197.159.160.0/19',
4551 'SV': '168.243.0.0/16',
4552 'SX': '190.102.0.0/20',
4554 'SZ': '41.84.224.0/19',
4555 'TC': '65.255.48.0/20',
4556 'TD': '154.68.128.0/19',
4557 'TG': '196.168.0.0/14',
4558 'TH': '171.96.0.0/13',
4559 'TJ': '85.9.128.0/18',
4560 'TK': '27.96.24.0/21',
4561 'TL': '180.189.160.0/20',
4562 'TM': '95.85.96.0/19',
4563 'TN': '197.0.0.0/11',
4564 'TO': '175.176.144.0/21',
4565 'TR': '78.160.0.0/11',
4566 'TT': '186.44.0.0/15',
4567 'TV': '202.2.96.0/19',
4568 'TW': '120.96.0.0/11',
4569 'TZ': '156.156.0.0/14',
4570 'UA': '37.52.0.0/14',
4571 'UG': '102.80.0.0/13',
4573 'UY': '167.56.0.0/13',
4574 'UZ': '84.54.64.0/18',
4575 'VA': '212.77.0.0/19',
4576 'VC': '207.191.240.0/21',
4577 'VE': '186.88.0.0/13',
4578 'VG': '66.81.192.0/20',
4579 'VI': '146.226.0.0/16',
4580 'VN': '14.160.0.0/11',
4581 'VU': '202.80.32.0/20',
4582 'WF': '117.20.32.0/21',
4583 'WS': '202.4.32.0/19',
4584 'YE': '134.35.0.0/16',
4585 'YT': '41.242.116.0/22',
4586 'ZA': '41.0.0.0/11',
4587 'ZM': '102.144.0.0/13',
4588 'ZW': '102.177.192.0/18',
4592 def random_ipv4(cls, code_or_block):
4593 if len(code_or_block) == 2:
4594 block = cls._country_ip_map.get(code_or_block.upper())
4598 block = code_or_block
4599 addr, preflen = block.split('/')
4600 addr_min = compat_struct_unpack('!L', socket.inet_aton(addr))[0]
4601 addr_max = addr_min | (0xffffffff >> int(preflen))
4602 return compat_str(socket.inet_ntoa(
4603 compat_struct_pack('!L', random.randint(addr_min, addr_max))))
4606 class PerRequestProxyHandler(compat_urllib_request.ProxyHandler):
4607 def __init__(self, proxies=None):
4608 # Set default handlers
4609 for type in ('http', 'https'):
4610 setattr(self, '%s_open' % type,
4611 lambda r, proxy='__noproxy__', type=type, meth=self.proxy_open:
4612 meth(r, proxy, type))
4613 compat_urllib_request.ProxyHandler.__init__(self, proxies)
4615 def proxy_open(self, req, proxy, type):
4616 req_proxy = req.headers.get('Ytdl-request-proxy')
4617 if req_proxy is not None:
4619 del req.headers['Ytdl-request-proxy']
4621 if proxy == '__noproxy__':
4622 return None # No Proxy
4623 if compat_urlparse.urlparse(proxy).scheme.lower() in ('socks', 'socks4', 'socks4a', 'socks5'):
4624 req.add_header('Ytdl-socks-proxy', proxy)
4625 # yt-dlp's http/https handlers do wrapping the socket with socks
4627 return compat_urllib_request.ProxyHandler.proxy_open(
4628 self, req, proxy, type)
4631 # Both long_to_bytes and bytes_to_long are adapted from PyCrypto, which is
4632 # released into Public Domain
4633 # https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Util/number.py#L387
4635 def long_to_bytes(n, blocksize=0):
4636 """long_to_bytes(n:long, blocksize:int) : string
4637 Convert a long integer to a byte string.
4639 If optional blocksize is given and greater than zero, pad the front of the
4640 byte string with binary zeros so that the length is a multiple of
4643 # after much testing, this algorithm was deemed to be the fastest
4647 s = compat_struct_pack('>I', n & 0xffffffff) + s
4649 # strip off leading zeros
4650 for i in range(len(s)):
4651 if s[i] != b'\000'[0]:
4654 # only happens when n == 0
4658 # add back some pad bytes. this could be done more efficiently w.r.t. the
4659 # de-padding being done above, but sigh...
4660 if blocksize > 0 and len(s) % blocksize:
4661 s = (blocksize - len(s) % blocksize) * b'\000' + s
4665 def bytes_to_long(s):
4666 """bytes_to_long(string) : long
4667 Convert a byte string to a long integer.
4669 This is (essentially) the inverse of long_to_bytes().
4674 extra = (4 - length % 4)
4675 s = b'\000' * extra + s
4676 length = length + extra
4677 for i in range(0, length, 4):
4678 acc = (acc << 32) + compat_struct_unpack('>I', s[i:i + 4])[0]
4682 def ohdave_rsa_encrypt(data, exponent, modulus):
4684 Implement OHDave
's RSA algorithm. See http://www.ohdave.com/rsa/
4687 data: data to encrypt, bytes-like object
4688 exponent, modulus: parameter e and N of RSA algorithm, both integer
4689 Output: hex string of encrypted data
4691 Limitation: supports one block encryption only
4694 payload = int(binascii.hexlify(data[::-1]), 16)
4695 encrypted = pow(payload, exponent, modulus)
4696 return '%x' % encrypted
4699 def pkcs1pad(data, length):
4701 Padding input data with PKCS#1 scheme
4703 @param {int[]} data input data
4704 @param {int} length target length
4705 @returns {int[]} padded data
4707 if len(data) > length - 11:
4708 raise ValueError('Input data too
long for PKCS
#1 padding')
4710 pseudo_random
= [random
.randint(0, 254) for _
in range(length
- len(data
) - 3)]
4711 return [0, 2] + pseudo_random
+ [0] + data
4714 def encode_base_n(num
, n
, table
=None):
4715 FULL_TABLE
= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
4717 table
= FULL_TABLE
[:n
]
4720 raise ValueError('base %d exceeds table length %d' % (n
, len(table
)))
4727 ret
= table
[num
% n
] + ret
4732 def decode_packed_codes(code
):
4733 mobj
= re
.search(PACKED_CODES_RE
, code
)
4734 obfuscated_code
, base
, count
, symbols
= mobj
.groups()
4737 symbols
= symbols
.split('|')
4742 base_n_count
= encode_base_n(count
, base
)
4743 symbol_table
[base_n_count
] = symbols
[count
] or base_n_count
4746 r
'\b(\w+)\b', lambda mobj
: symbol_table
[mobj
.group(0)],
4750 def caesar(s
, alphabet
, shift
):
4755 alphabet
[(alphabet
.index(c
) + shift
) % l
] if c
in alphabet
else c
4760 return caesar(s
, r
'''!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~''', 47)
4763 def parse_m3u8_attributes(attrib
):
4765 for (key
, val
) in re
.findall(r
'(?P<key>[A-Z0-9-]+)=(?P<val>"[^"]+"|[^",]+)(?:,|$)', attrib
):
4766 if val
.startswith('"'):
4772 def urshift(val
, n
):
4773 return val
>> n
if val
>= 0 else (val
+ 0x100000000) >> n
4776 # Based on png2str() written by @gdkchan and improved by @yokrysty
4777 # Originally posted at https://github.com/ytdl-org/youtube-dl/issues/9706
4778 def decode_png(png_data
):
4779 # Reference: https://www.w3.org/TR/PNG/
4780 header
= png_data
[8:]
4782 if png_data
[:8] != b
'\x89PNG\x0d\x0a\x1a\x0a' or header
[4:8] != b
'IHDR':
4783 raise IOError('Not a valid PNG file.')
4785 int_map
= {1: '>B', 2: '>H', 4: '>I'}
4786 unpack_integer
= lambda x
: compat_struct_unpack(int_map
[len(x
)], x
)[0]
4791 length
= unpack_integer(header
[:4])
4794 chunk_type
= header
[:4]
4797 chunk_data
= header
[:length
]
4798 header
= header
[length
:]
4800 header
= header
[4:] # Skip CRC
4808 ihdr
= chunks
[0]['data']
4810 width
= unpack_integer(ihdr
[:4])
4811 height
= unpack_integer(ihdr
[4:8])
4815 for chunk
in chunks
:
4816 if chunk
['type'] == b
'IDAT':
4817 idat
+= chunk
['data']
4820 raise IOError('Unable to read PNG data.')
4822 decompressed_data
= bytearray(zlib
.decompress(idat
))
4827 def _get_pixel(idx
):
4832 for y
in range(height
):
4833 basePos
= y
* (1 + stride
)
4834 filter_type
= decompressed_data
[basePos
]
4838 pixels
.append(current_row
)
4840 for x
in range(stride
):
4841 color
= decompressed_data
[1 + basePos
+ x
]
4842 basex
= y
* stride
+ x
4847 left
= _get_pixel(basex
- 3)
4849 up
= _get_pixel(basex
- stride
)
4851 if filter_type
== 1: # Sub
4852 color
= (color
+ left
) & 0xff
4853 elif filter_type
== 2: # Up
4854 color
= (color
+ up
) & 0xff
4855 elif filter_type
== 3: # Average
4856 color
= (color
+ ((left
+ up
) >> 1)) & 0xff
4857 elif filter_type
== 4: # Paeth
4863 c
= _get_pixel(basex
- stride
- 3)
4871 if pa
<= pb
and pa
<= pc
:
4872 color
= (color
+ a
) & 0xff
4874 color
= (color
+ b
) & 0xff
4876 color
= (color
+ c
) & 0xff
4878 current_row
.append(color
)
4880 return width
, height
, pixels
4883 def write_xattr(path
, key
, value
):
4884 # This mess below finds the best xattr tool for the job
4886 # try the pyxattr module...
4889 if hasattr(xattr
, 'set'): # pyxattr
4890 # Unicode arguments are not supported in python-pyxattr until
4892 # See https://github.com/ytdl-org/youtube-dl/issues/5498
4893 pyxattr_required_version
= '0.5.0'
4894 if version_tuple(xattr
.__version
__) < version_tuple(pyxattr_required_version
):
4895 # TODO: fallback to CLI tools
4896 raise XAttrUnavailableError(
4897 'python-pyxattr is detected but is too old. '
4898 'yt-dlp requires %s or above while your version is %s. '
4899 'Falling back to other xattr implementations' % (
4900 pyxattr_required_version
, xattr
.__version
__))
4902 setxattr
= xattr
.set
4904 setxattr
= xattr
.setxattr
4907 setxattr(path
, key
, value
)
4908 except EnvironmentError as e
:
4909 raise XAttrMetadataError(e
.errno
, e
.strerror
)
4912 if compat_os_name
== 'nt':
4913 # Write xattrs to NTFS Alternate Data Streams:
4914 # http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
4915 assert ':' not in key
4916 assert os
.path
.exists(path
)
4918 ads_fn
= path
+ ':' + key
4920 with open(ads_fn
, 'wb') as f
:
4922 except EnvironmentError as e
:
4923 raise XAttrMetadataError(e
.errno
, e
.strerror
)
4925 user_has_setfattr
= check_executable('setfattr', ['--version'])
4926 user_has_xattr
= check_executable('xattr', ['-h'])
4928 if user_has_setfattr
or user_has_xattr
:
4930 value
= value
.decode('utf-8')
4931 if user_has_setfattr
:
4932 executable
= 'setfattr'
4933 opts
= ['-n', key
, '-v', value
]
4934 elif user_has_xattr
:
4935 executable
= 'xattr'
4936 opts
= ['-w', key
, value
]
4938 cmd
= ([encodeFilename(executable
, True)]
4939 + [encodeArgument(o
) for o
in opts
]
4940 + [encodeFilename(path
, True)])
4944 cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
4945 except EnvironmentError as e
:
4946 raise XAttrMetadataError(e
.errno
, e
.strerror
)
4947 stdout
, stderr
= p
.communicate_or_kill()
4948 stderr
= stderr
.decode('utf-8', 'replace')
4949 if p
.returncode
!= 0:
4950 raise XAttrMetadataError(p
.returncode
, stderr
)
4953 # On Unix, and can't find pyxattr, setfattr, or xattr.
4954 if sys
.platform
.startswith('linux'):
4955 raise XAttrUnavailableError(
4956 "Couldn't find a tool to set the xattrs. "
4957 "Install either the python 'pyxattr' or 'xattr' "
4958 "modules, or the GNU 'attr' package "
4959 "(which contains the 'setfattr' tool).")
4961 raise XAttrUnavailableError(
4962 "Couldn't find a tool to set the xattrs. "
4963 "Install either the python 'xattr' module, "
4964 "or the 'xattr' binary.")
4967 def random_birthday(year_field
, month_field
, day_field
):
4968 start_date
= datetime
.date(1950, 1, 1)
4969 end_date
= datetime
.date(1995, 12, 31)
4970 offset
= random
.randint(0, (end_date
- start_date
).days
)
4971 random_date
= start_date
+ datetime
.timedelta(offset
)
4973 year_field
: str(random_date
.year
),
4974 month_field
: str(random_date
.month
),
4975 day_field
: str(random_date
.day
),
4979 # Templates for internet shortcut files, which are plain text files.
4980 DOT_URL_LINK_TEMPLATE
= '''
4985 DOT_WEBLOC_LINK_TEMPLATE
= '''
4986 <?xml version="1.0" encoding="UTF-8"?>
4987 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
4988 <plist version="1.0">
4991 \t<string>%(url)s</string>
4996 DOT_DESKTOP_LINK_TEMPLATE
= '''
5006 'url': DOT_URL_LINK_TEMPLATE
,
5007 'desktop': DOT_DESKTOP_LINK_TEMPLATE
,
5008 'webloc': DOT_WEBLOC_LINK_TEMPLATE
,
5012 def iri_to_uri(iri
):
5014 Converts an IRI (Internationalized Resource Identifier, allowing Unicode characters) to a URI (Uniform Resource Identifier, ASCII-only).
5016 The function doesn't add an additional layer of escaping; e.g., it doesn't escape `%3C` as `%253C`. Instead, it percent-escapes characters with an underlying UTF-8 encoding *besides* those already escaped, leaving the URI intact.
5019 iri_parts
= compat_urllib_parse_urlparse(iri
)
5021 if '[' in iri_parts
.netloc
:
5022 raise ValueError('IPv6 URIs are not, yet, supported.')
5023 # Querying `.netloc`, when there's only one bracket, also raises a ValueError.
5025 # The `safe` argument values, that the following code uses, contain the characters that should not be percent-encoded. Everything else but letters, digits and '_.-' will be percent-encoded with an underlying UTF-8 encoding. Everything already percent-encoded will be left as is.
5028 if iri_parts
.username
:
5029 net_location
+= compat_urllib_parse_quote(iri_parts
.username
, safe
=r
"!$%&'()*+,~")
5030 if iri_parts
.password
is not None:
5031 net_location
+= ':' + compat_urllib_parse_quote(iri_parts
.password
, safe
=r
"!$%&'()*+,~")
5034 net_location
+= iri_parts
.hostname
.encode('idna').decode('utf-8') # Punycode for Unicode hostnames.
5035 # The 'idna' encoding produces ASCII text.
5036 if iri_parts
.port
is not None and iri_parts
.port
!= 80:
5037 net_location
+= ':' + str(iri_parts
.port
)
5039 return compat_urllib_parse_urlunparse(
5043 compat_urllib_parse_quote_plus(iri_parts
.path
, safe
=r
"!$%&'()*+,/:;=@|~"),
5045 # Unsure about the `safe` argument, since this is a legacy way of handling parameters.
5046 compat_urllib_parse_quote_plus(iri_parts
.params
, safe
=r
"!$%&'()*+,/:;=@|~"),
5048 # Not totally sure about the `safe` argument, since the source does not explicitly mention the query URI component.
5049 compat_urllib_parse_quote_plus(iri_parts
.query
, safe
=r
"!$%&'()*+,/:;=?@{|}~"),
5051 compat_urllib_parse_quote_plus(iri_parts
.fragment
, safe
=r
"!#$%&'()*+,/:;=?@{|}~")))
5053 # Source for `safe` arguments: https://url.spec.whatwg.org/#percent-encoded-bytes.
5056 def to_high_limit_path(path
):
5057 if sys
.platform
in ['win32', 'cygwin']:
5058 # Work around MAX_PATH limitation on Windows. The maximum allowed length for the individual path segments may still be quite limited.
5059 return r
'\\?\ '.rstrip() + os
.path
.abspath(path
)
5064 def format_field(obj
, field
=None, template
='%s', ignore
=(None, ''), default
='', func
=None):
5065 val
= traverse_obj(obj
, *variadic(field
))
5068 return template
% (func(val
) if func
else val
)
5071 def clean_podcast_url(url
):
5072 return re
.sub(r
'''(?x)
5076 media\.blubrry\.com| # https://create.blubrry.com/resources/podcast-media-download-statistics/getting-started/
5079 (?:dts|www)\.podtrac\.com/(?:pts/)?redirect\.[0-9a-z]{3,4}| # http://analytics.podtrac.com/how-to-measure
5082 cn\.co| # https://podcorn.com/analytics-prefix/
5083 st\.fm # https://podsights.com/docs/
5088 _HEX_TABLE
= '0123456789abcdef'
5091 def random_uuidv4():
5092 return re
.sub(r
'[xy]', lambda x
: _HEX_TABLE
[random
.randint(0, 15)], 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx')
5095 def make_dir(path
, to_screen
=None):
5097 dn
= os
.path
.dirname(path
)
5098 if dn
and not os
.path
.exists(dn
):
5101 except (OSError, IOError) as err
:
5102 if callable(to_screen
) is not None:
5103 to_screen('unable to create directory ' + error_to_compat_str(err
))
5107 def get_executable_path():
5108 from zipimport
import zipimporter
5109 if hasattr(sys
, 'frozen'): # Running from PyInstaller
5110 path
= os
.path
.dirname(sys
.executable
)
5111 elif isinstance(globals().get('__loader__'), zipimporter
): # Running from ZIP
5112 path
= os
.path
.join(os
.path
.dirname(__file__
), '../..')
5114 path
= os
.path
.join(os
.path
.dirname(__file__
), '..')
5115 return os
.path
.abspath(path
)
5118 def load_plugins(name
, suffix
, namespace
):
5121 plugins_spec
= importlib
.util
.spec_from_file_location(
5122 name
, os
.path
.join(get_executable_path(), 'ytdlp_plugins', name
, '__init__.py'))
5123 plugins
= importlib
.util
.module_from_spec(plugins_spec
)
5124 sys
.modules
[plugins_spec
.name
] = plugins
5125 plugins_spec
.loader
.exec_module(plugins
)
5126 for name
in dir(plugins
):
5127 if name
in namespace
:
5129 if not name
.endswith(suffix
):
5131 klass
= getattr(plugins
, name
)
5132 classes
[name
] = namespace
[name
] = klass
5133 except FileNotFoundError
:
5139 obj
, *path_list
, default
=None, expected_type
=None, get_all
=True,
5140 casesense
=True, is_user_input
=False, traverse_string
=False):
5141 ''' Traverse nested list/dict/tuple
5142 @param path_list A list of paths which are checked one by one.
5143 Each path is a list of keys where each key is a string,
5144 a function, a tuple of strings/None or "...".
5145 When a fuction is given, it takes the key as argument and
5146 returns whether the key matches or not. When a tuple is given,
5147 all the keys given in the tuple are traversed, and
5148 "..." traverses all the keys in the object
5149 "None" returns the object without traversal
5150 @param default Default value to return
5151 @param expected_type Only accept final value of this type (Can also be any callable)
5152 @param get_all Return all the values obtained from a path or only the first one
5153 @param casesense Whether to consider dictionary keys as case sensitive
5154 @param is_user_input Whether the keys are generated from user input. If True,
5155 strings are converted to int/slice if necessary
5156 @param traverse_string Whether to traverse inside strings. If True, any
5157 non-compatible object will also be converted into a string
5161 _lower
= lambda k
: (k
.lower() if isinstance(k
, str) else k
)
5162 path_list
= (map(_lower
, variadic(path
)) for path
in path_list
)
5164 def _traverse_obj(obj
, path
, _current_depth
=0):
5166 path
= tuple(variadic(path
))
5167 for i
, key
in enumerate(path
):
5168 if None in (key
, obj
):
5170 if isinstance(key
, (list, tuple)):
5171 obj
= [_traverse_obj(obj
, sub_key
, _current_depth
) for sub_key
in key
]
5174 obj
= (obj
.values() if isinstance(obj
, dict)
5175 else obj
if isinstance(obj
, (list, tuple, LazyList
))
5176 else str(obj
) if traverse_string
else [])
5178 depth
= max(depth
, _current_depth
)
5179 return [_traverse_obj(inner_obj
, path
[i
+ 1:], _current_depth
) for inner_obj
in obj
]
5181 if isinstance(obj
, (list, tuple, LazyList
)):
5182 obj
= enumerate(obj
)
5183 elif isinstance(obj
, dict):
5186 if not traverse_string
:
5190 depth
= max(depth
, _current_depth
)
5191 return [_traverse_obj(v
, path
[i
+ 1:], _current_depth
) for k
, v
in obj
if key(k
)]
5192 elif isinstance(obj
, dict) and not (is_user_input
and key
== ':'):
5193 obj
= (obj
.get(key
) if casesense
or (key
in obj
)
5194 else next((v
for k
, v
in obj
.items() if _lower(k
) == key
), None))
5197 key
= (int_or_none(key
) if ':' not in key
5198 else slice(*map(int_or_none
, key
.split(':'))))
5199 if key
== slice(None):
5200 return _traverse_obj(obj
, (..., *path
[i
+ 1:]), _current_depth
)
5201 if not isinstance(key
, (int, slice)):
5203 if not isinstance(obj
, (list, tuple, LazyList
)):
5204 if not traverse_string
:
5213 if isinstance(expected_type
, type):
5214 type_test
= lambda val
: val
if isinstance(val
, expected_type
) else None
5215 elif expected_type
is not None:
5216 type_test
= expected_type
5218 type_test
= lambda val
: val
5220 for path
in path_list
:
5222 val
= _traverse_obj(obj
, path
)
5225 for _
in range(depth
- 1):
5226 val
= itertools
.chain
.from_iterable(v
for v
in val
if v
is not None)
5227 val
= [v
for v
in map(type_test
, val
) if v
is not None]
5229 return val
if get_all
else val
[0]
5231 val
= type_test(val
)
5237 def traverse_dict(dictn
, keys
, casesense
=True):
5238 write_string('DeprecationWarning: yt_dlp.utils.traverse_dict is deprecated '
5239 'and may be removed in a future version. Use yt_dlp.utils.traverse_obj instead')
5240 return traverse_obj(dictn
, keys
, casesense
=casesense
, is_user_input
=True, traverse_string
=True)
5243 def get_first(obj
, keys
, **kwargs
):
5244 return traverse_obj(obj
, (..., *variadic(keys
)), **kwargs
, get_all
=False)
5247 def variadic(x
, allowed_types
=(str, bytes, dict)):
5248 return x
if isinstance(x
, collections
.abc
.Iterable
) and not isinstance(x
, allowed_types
) else (x
,)
5251 def decode_base(value
, digits
):
5252 # This will convert given base-x string to scalar (long or int)
5253 table
= {char: index for index, char in enumerate(digits)}
5258 result
+= table
[chr]
5262 def time_seconds(**kwargs
):
5263 t
= datetime
.datetime
.now(datetime
.timezone(datetime
.timedelta(**kwargs
)))
5264 return t
.timestamp()
5267 # create a JSON Web Signature (jws) with HS256 algorithm
5268 # the resulting format is in JWS Compact Serialization
5269 # implemented following JWT https://www.rfc-editor.org/rfc/rfc7519.html
5270 # implemented following JWS https://www.rfc-editor.org/rfc/rfc7515.html
5271 def jwt_encode_hs256(payload_data
, key
, headers
={}):
5277 header_data
.update(headers
)
5278 header_b64
= base64
.b64encode(json
.dumps(header_data
).encode('utf-8'))
5279 payload_b64
= base64
.b64encode(json
.dumps(payload_data
).encode('utf-8'))
5280 h
= hmac
.new(key
.encode('utf-8'), header_b64
+ b
'.' + payload_b64
, hashlib
.sha256
)
5281 signature_b64
= base64
.b64encode(h
.digest())
5282 token
= header_b64
+ b
'.' + payload_b64
+ b
'.' + signature_b64
5286 # can be extended in future to verify the signature and parse header and return the algorithm used if it's not HS256
5287 def jwt_decode_hs256(jwt
):
5288 header_b64
, payload_b64
, signature_b64
= jwt
.split('.')
5289 payload_data
= json
.loads(base64
.urlsafe_b64decode(payload_b64
))
5293 def supports_terminal_sequences(stream
):
5294 if compat_os_name
== 'nt':
5295 from .compat
import WINDOWS_VT_MODE
# Must be imported locally
5296 if not WINDOWS_VT_MODE
or get_windows_version() < (10, 0, 10586):
5298 elif not os
.getenv('TERM'):
5301 return stream
.isatty()
5302 except BaseException
:
5306 _terminal_sequences_re
= re
.compile('\033\\[[^m]+m')
5309 def remove_terminal_sequences(string
):
5310 return _terminal_sequences_re
.sub('', string
)
5313 def number_of_digits(number
):
5314 return len('%d' % number
)
5317 def join_nonempty(*values
, delim
='-', from_dict
=None):
5318 if from_dict
is not None:
5319 values
= map(from_dict
.get
, values
)
5320 return delim
.join(map(str, filter(None, values
)))
5323 def scale_thumbnails_to_max_format_width(formats
, thumbnails
, url_width_re
):
5325 Find the largest format dimensions in terms of video width and, for each thumbnail:
5326 * Modify the URL: Match the width with the provided regex and replace with the former width
5329 This function is useful with video services that scale the provided thumbnails on demand
5331 _keys
= ('width', 'height')
5332 max_dimensions
= max(
5333 [tuple(format
.get(k
) or 0 for k
in _keys
) for format
in formats
],
5335 if not max_dimensions
[0]:
5339 {'url': re.sub(url_width_re, str(max_dimensions[0]), thumbnail['url'])}
,
5340 dict(zip(_keys
, max_dimensions
)), thumbnail
)
5341 for thumbnail
in thumbnails
5345 def parse_http_range(range):
5346 """ Parse value of "Range" or "Content-Range" HTTP header into tuple. """
5348 return None, None, None
5349 crg
= re
.search(r
'bytes[ =](\d+)-(\d+)?(?:/(\d+))?', range)
5351 return None, None, None
5352 return int(crg
.group(1)), int_or_none(crg
.group(2)), int_or_none(crg
.group(3))
5358 __initialized
= False
5360 def __init__(self
, parser
, label
=None):
5361 self
._parser
, self
.label
= parser
, label
5362 self
._loaded
_paths
, self
.configs
= set(), []
5364 def init(self
, args
=None, filename
=None):
5365 assert not self
.__initialized
5368 location
= os
.path
.realpath(filename
)
5369 directory
= os
.path
.dirname(location
)
5370 if location
in self
._loaded
_paths
:
5372 self
._loaded
_paths
.add(location
)
5374 self
.__initialized
= True
5375 self
.own_args
, self
.filename
= args
, filename
5376 for location
in self
._parser
.parse_args(args
)[0].config_locations
or []:
5377 location
= os
.path
.join(directory
, expand_path(location
))
5378 if os
.path
.isdir(location
):
5379 location
= os
.path
.join(location
, 'yt-dlp.conf')
5380 if not os
.path
.exists(location
):
5381 self
._parser
.error(f
'config location {location} does not exist')
5382 self
.append_config(self
.read_file(location
), location
)
5386 label
= join_nonempty(
5387 self
.label
, 'config', f
'"{self.filename}"' if self
.filename
else '',
5389 return join_nonempty(
5390 self
.own_args
is not None and f
'{label[0].upper()}{label[1:]}: {self.hide_login_info(self.own_args)}',
5391 *(f
'\n{c}'.replace('\n', '\n| ')[1:] for c
in self
.configs
),
5395 def read_file(filename
, default
=[]):
5397 optionf
= open(filename
)
5399 return default
# silently skip if file is not present
5401 # FIXME: https://github.com/ytdl-org/youtube-dl/commit/dfe5fa49aed02cf36ba9f743b11b0903554b5e56
5402 contents
= optionf
.read()
5403 if sys
.version_info
< (3,):
5404 contents
= contents
.decode(preferredencoding())
5405 res
= compat_shlex_split(contents
, comments
=True)
5411 def hide_login_info(opts
):
5412 PRIVATE_OPTS
= set(['-p', '--password', '-u', '--username', '--video-password', '--ap-password', '--ap-username'])
5413 eqre
= re
.compile('^(?P<key>' + ('|'.join(re
.escape(po
) for po
in PRIVATE_OPTS
)) + ')=.+$')
5418 return m
.group('key') + '=PRIVATE'
5422 opts
= list(map(_scrub_eq
, opts
))
5423 for idx
, opt
in enumerate(opts
):
5424 if opt
in PRIVATE_OPTS
and idx
+ 1 < len(opts
):
5425 opts
[idx
+ 1] = 'PRIVATE'
5428 def append_config(self
, *args
, label
=None):
5429 config
= type(self
)(self
._parser
, label
)
5430 config
._loaded
_paths
= self
._loaded
_paths
5431 if config
.init(*args
):
5432 self
.configs
.append(config
)
5436 for config
in reversed(self
.configs
):
5437 yield from config
.all_args
5438 yield from self
.own_args
or []
5440 def parse_args(self
):
5441 return self
._parser
.parse_args(list(self
.all_args
))
5444 class WebSocketsWrapper():
5445 """Wraps websockets module to use in non-async scopes"""
5447 def __init__(self
, url
, headers
=None, connect
=True):
5448 self
.loop
= asyncio
.events
.new_event_loop()
5449 self
.conn
= compat_websockets
.connect(
5450 url
, extra_headers
=headers
, ping_interval
=None,
5451 close_timeout
=float('inf'), loop
=self
.loop
, ping_timeout
=float('inf'))
5454 atexit
.register(self
.__exit
__, None, None, None)
5456 def __enter__(self
):
5458 self
.pool
= self
.run_with_loop(self
.conn
.__aenter
__(), self
.loop
)
5461 def send(self
, *args
):
5462 self
.run_with_loop(self
.pool
.send(*args
), self
.loop
)
5464 def recv(self
, *args
):
5465 return self
.run_with_loop(self
.pool
.recv(*args
), self
.loop
)
5467 def __exit__(self
, type, value
, traceback
):
5469 return self
.run_with_loop(self
.conn
.__aexit
__(type, value
, traceback
), self
.loop
)
5472 self
._cancel
_all
_tasks
(self
.loop
)
5474 # taken from https://github.com/python/cpython/blob/3.9/Lib/asyncio/runners.py with modifications
5475 # for contributors: If there's any new library using asyncio needs to be run in non-async, move these function out of this class
5477 def run_with_loop(main
, loop
):
5478 if not asyncio
.coroutines
.iscoroutine(main
):
5479 raise ValueError(f
'a coroutine was expected, got {main!r}')
5482 return loop
.run_until_complete(main
)
5484 loop
.run_until_complete(loop
.shutdown_asyncgens())
5485 if hasattr(loop
, 'shutdown_default_executor'):
5486 loop
.run_until_complete(loop
.shutdown_default_executor())
5489 def _cancel_all_tasks(loop
):
5490 to_cancel
= asyncio
.tasks
.all_tasks(loop
)
5495 for task
in to_cancel
:
5498 loop
.run_until_complete(
5499 asyncio
.tasks
.gather(*to_cancel
, loop
=loop
, return_exceptions
=True))
5501 for task
in to_cancel
:
5502 if task
.cancelled():
5504 if task
.exception() is not None:
5505 loop
.call_exception_handler({
5506 'message': 'unhandled exception during asyncio.run() shutdown',
5507 'exception': task
.exception(),
5512 has_websockets
= bool(compat_websockets
)
5515 def merge_headers(*dicts
):
5516 """Merge dicts of http headers case insensitively, prioritizing the latter ones"""
5517 return {k.title(): v for k, v in itertools.chain.from_iterable(map(dict.items, dicts))}
5520 class classproperty
:
5521 def __init__(self
, f
):
5524 def __get__(self
, _
, cls
):