aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/utils
diff options
context:
space:
mode:
Diffstat (limited to 'yt_dlp/utils')
-rw-r--r--yt_dlp/utils/_deprecated.py19
-rw-r--r--yt_dlp/utils/_legacy.py62
-rw-r--r--yt_dlp/utils/_utils.py47
3 files changed, 63 insertions, 65 deletions
diff --git a/yt_dlp/utils/_deprecated.py b/yt_dlp/utils/_deprecated.py
index e55d42354..a8ae8ecb5 100644
--- a/yt_dlp/utils/_deprecated.py
+++ b/yt_dlp/utils/_deprecated.py
@@ -10,16 +10,6 @@ del passthrough_module
from ._utils import preferredencoding
-from ..networking._urllib import HTTPHandler
-
-# isort: split
-from .networking import random_user_agent, std_headers # noqa: F401
-from ..networking._urllib import PUTRequest # noqa: F401
-from ..networking._urllib import SUPPORTED_ENCODINGS, HEADRequest # noqa: F401
-from ..networking._urllib import ProxyHandler as PerRequestProxyHandler # noqa: F401
-from ..networking._urllib import RedirectHandler as YoutubeDLRedirectHandler # noqa: F401
-from ..networking._urllib import make_socks_conn_class, update_Request # noqa: F401
-from ..networking.exceptions import network_exceptions # noqa: F401
def encodeFilename(s, for_subprocess=False):
@@ -47,12 +37,3 @@ def decodeOption(optval):
def error_to_compat_str(err):
return str(err)
-
-
-class YoutubeDLHandler(HTTPHandler):
- def __init__(self, params, *args, **kwargs):
- self._params = params
- super().__init__(*args, **kwargs)
-
-
-YoutubeDLHTTPSHandler = YoutubeDLHandler
diff --git a/yt_dlp/utils/_legacy.py b/yt_dlp/utils/_legacy.py
index 96ac468b1..077000971 100644
--- a/yt_dlp/utils/_legacy.py
+++ b/yt_dlp/utils/_legacy.py
@@ -1,17 +1,30 @@
"""No longer used and new code should not use. Exists only for API compat."""
-
import platform
import struct
import sys
+import urllib.error
import urllib.parse
+import urllib.request
import zlib
from ._utils import Popen, decode_base_n, preferredencoding
from .traversal import traverse_obj
from ..dependencies import certifi, websockets
+from ..networking._helper import make_ssl_context
+from ..networking._urllib import HTTPHandler
# isort: split
+from .networking import random_user_agent, std_headers # noqa: F401
from ..cookies import YoutubeDLCookieJar # noqa: F401
+from ..networking._urllib import PUTRequest # noqa: F401
+from ..networking._urllib import SUPPORTED_ENCODINGS, HEADRequest # noqa: F401
+from ..networking._urllib import ProxyHandler as PerRequestProxyHandler # noqa: F401
+from ..networking._urllib import RedirectHandler as YoutubeDLRedirectHandler # noqa: F401
+from ..networking._urllib import ( # noqa: F401
+ make_socks_conn_class,
+ update_Request,
+)
+from ..networking.exceptions import HTTPError, network_exceptions # noqa: F401
has_certifi = bool(certifi)
has_websockets = bool(websockets)
@@ -176,5 +189,52 @@ def handle_youtubedl_headers(headers):
return filtered_headers
+def request_to_url(req):
+ if isinstance(req, urllib.request.Request):
+ return req.get_full_url()
+ else:
+ return req
+
+
+def sanitized_Request(url, *args, **kwargs):
+ from ..utils import escape_url, extract_basic_auth, sanitize_url
+ url, auth_header = extract_basic_auth(escape_url(sanitize_url(url)))
+ if auth_header is not None:
+ headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {})
+ headers['Authorization'] = auth_header
+ return urllib.request.Request(url, *args, **kwargs)
+
+
+class YoutubeDLHandler(HTTPHandler):
+ def __init__(self, params, *args, **kwargs):
+ self._params = params
+ super().__init__(*args, **kwargs)
+
+
+YoutubeDLHTTPSHandler = YoutubeDLHandler
+
+
+class YoutubeDLCookieProcessor(urllib.request.HTTPCookieProcessor):
+ def __init__(self, cookiejar=None):
+ urllib.request.HTTPCookieProcessor.__init__(self, cookiejar)
+
+ def http_response(self, request, response):
+ return urllib.request.HTTPCookieProcessor.http_response(self, request, response)
+
+ https_request = urllib.request.HTTPCookieProcessor.http_request
+ https_response = http_response
+
+
+def make_HTTPS_handler(params, **kwargs):
+ return YoutubeDLHTTPSHandler(params, context=make_ssl_context(
+ verify=not params.get('nocheckcertificate'),
+ client_certificate=params.get('client_certificate'),
+ client_certificate_key=params.get('client_certificate_key'),
+ client_certificate_password=params.get('client_certificate_password'),
+ legacy_support=params.get('legacyserverconnect'),
+ use_certifi='no-certifi' not in params.get('compat_opts', []),
+ ), **kwargs)
+
+
def process_communicate_or_kill(p, *args, **kwargs):
return Popen.communicate_or_kill(p, *args, **kwargs)
diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py
index d0e328716..2e619f9ea 100644
--- a/yt_dlp/utils/_utils.py
+++ b/yt_dlp/utils/_utils.py
@@ -62,11 +62,6 @@ __name__ = __name__.rsplit('.', 1)[0] # Pretend to be the parent module
compiled_regex_type = type(re.compile(''))
-USER_AGENTS = {
- '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',
-}
-
-
class NO_DEFAULT:
pass
@@ -727,14 +722,6 @@ def extract_basic_auth(url):
return url, f'Basic {auth_payload.decode()}'
-def sanitized_Request(url, *args, **kwargs):
- url, auth_header = extract_basic_auth(escape_url(sanitize_url(url)))
- if auth_header is not None:
- headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {})
- headers['Authorization'] = auth_header
- return urllib.request.Request(url, *args, **kwargs)
-
-
def expand_path(s):
"""Expand shell variables and ~"""
return os.path.expandvars(compat_expanduser(s))
@@ -894,19 +881,6 @@ def formatSeconds(secs, delim=':', msec=False):
return '%s.%03d' % (ret, time.milliseconds) if msec else ret
-def make_HTTPS_handler(params, **kwargs):
- from ._deprecated import YoutubeDLHTTPSHandler
- from ..networking._helper import make_ssl_context
- return YoutubeDLHTTPSHandler(params, context=make_ssl_context(
- verify=not params.get('nocheckcertificate'),
- client_certificate=params.get('client_certificate'),
- client_certificate_key=params.get('client_certificate_key'),
- client_certificate_password=params.get('client_certificate_password'),
- legacy_support=params.get('legacyserverconnect'),
- use_certifi='no-certifi' not in params.get('compat_opts', []),
- ), **kwargs)
-
-
def bug_reports_message(before=';'):
from ..update import REPOSITORY
@@ -1143,17 +1117,6 @@ def is_path_like(f):
return isinstance(f, (str, bytes, os.PathLike))
-class YoutubeDLCookieProcessor(urllib.request.HTTPCookieProcessor):
- def __init__(self, cookiejar=None):
- urllib.request.HTTPCookieProcessor.__init__(self, cookiejar)
-
- def http_response(self, request, response):
- return urllib.request.HTTPCookieProcessor.http_response(self, request, response)
-
- https_request = urllib.request.HTTPCookieProcessor.http_request
- https_response = http_response
-
-
def extract_timezone(date_str):
m = re.search(
r'''(?x)
@@ -1455,6 +1418,7 @@ def write_string(s, out=None, encoding=None):
out.flush()
+# TODO: Use global logger
def deprecation_warning(msg, *, printer=None, stacklevel=0, **kwargs):
from .. import _IN_CLI
if _IN_CLI:
@@ -2005,13 +1969,6 @@ def url_or_none(url):
return url if re.match(r'^(?:(?:https?|rt(?:m(?:pt?[es]?|fp)|sp[su]?)|mms|ftps?):)?//', url) else None
-def request_to_url(req):
- if isinstance(req, urllib.request.Request):
- return req.get_full_url()
- else:
- return req
-
-
def strftime_or_none(timestamp, date_format='%Y%m%d', default=None):
datetime_object = None
try:
@@ -5525,7 +5482,7 @@ class _YDLLogger:
def warning(self, message, *, once=False):
if self._ydl:
- self._ydl.report_warning(message, only_once=once)
+ self._ydl.report_warning(message, once)
def error(self, message, *, is_error=True):
if self._ydl: