From f24bc9272e9b74efc4c4af87c862f5f78921d424 Mon Sep 17 00:00:00 2001 From: dirkf Date: Tue, 4 Jul 2023 16:06:21 +0100 Subject: [Misc] Fixes for 2.6 compatibility --- youtube_dl/YoutubeDL.py | 6 +++++- youtube_dl/compat.py | 12 ++++++++++++ youtube_dl/jsinterp.py | 13 ++++++++++++- youtube_dl/utils.py | 3 ++- 4 files changed, 31 insertions(+), 3 deletions(-) (limited to 'youtube_dl') diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 98b878fc1..068029d3e 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -25,7 +25,11 @@ import tokenize import traceback import random -from ssl import OPENSSL_VERSION +try: + from ssl import OPENSSL_VERSION +except ImportError: + # Must be Python 2.6, should be built against 1.0.2 + OPENSSL_VERSION = 'OpenSSL 1.0.2(?)' from string import ascii_letters from .compat import ( diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index 0f4d3756f..2554fd1c3 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -1,10 +1,12 @@ # coding: utf-8 from __future__ import unicode_literals +from __future__ import division import base64 import binascii import collections import ctypes +import datetime import email import getpass import io @@ -3150,6 +3152,15 @@ def compat_register_utf8(): lambda name: lookup('utf-8') if name == 'cp65001' else None) +# compat_datetime_timedelta_total_seconds +try: + compat_datetime_timedelta_total_seconds = datetime.timedelta.total_seconds +except AttributeError: + # Py 2.6 + def compat_datetime_timedelta_total_seconds(td): + return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 + + legacy = [ 'compat_HTMLParseError', 'compat_HTMLParser', @@ -3187,6 +3198,7 @@ __all__ = [ 'compat_chr', 'compat_collections_abc', 'compat_collections_chain_map', + 'compat_datetime_timedelta_total_seconds', 'compat_http_cookiejar', 'compat_http_cookiejar_Cookie', 'compat_http_cookies', diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py index 1ba9c3d67..882432b80 100644 --- a/youtube_dl/jsinterp.py +++ b/youtube_dl/jsinterp.py @@ -277,9 +277,20 @@ class JSInterpreter(object): def __getattr__(self, name): self.__instantiate() + # make Py 2.6 conform to its lying documentation + if name == 'flags': + self.flags = self.__flags + elif name == 'pattern': + self.pattern = self.__pattern_txt + elif name in ('groupindex', 'groups'): + # in case these get set after a match? + if hasattr(self.__self, name): + setattr(self, name, getattr(self.__self, name)) + else: + return 0 if name == 'groupindex' else {} if hasattr(self, name): return getattr(self, name) - return super(JSInterpreter.JS_RegExp, self).__getattr__(name) + raise AttributeError('{0} has no attribute named {1}'.format(self, name)) @classmethod def regex_flags(cls, expr): diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 584581b6a..83f67bd95 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -47,6 +47,7 @@ from .compat import ( compat_collections_abc, compat_cookiejar, compat_ctypes_WINFUNCTYPE, + compat_datetime_timedelta_total_seconds, compat_etree_fromstring, compat_expanduser, compat_html_entities, @@ -3102,7 +3103,7 @@ def unified_timestamp(date_str, day_first=True): pass timetuple = email.utils.parsedate_tz(date_str) if timetuple: - return calendar.timegm(timetuple) + pm_delta * 3600 - timezone.total_seconds() + return calendar.timegm(timetuple) + pm_delta * 3600 - compat_datetime_timedelta_total_seconds(timezone) def determine_ext(url, default_ext='unknown_video'): -- cgit v1.2.3