Skip to content

Commit

Permalink
require urllib3>=2
Browse files Browse the repository at this point in the history
  • Loading branch information
freddy36 committed Jun 28, 2024
1 parent 99dd796 commit b470d65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 15 additions & 12 deletions audiobookdl/sources/source/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Internal imports
from . import networking
from audiobookdl import logging, AudiobookFile, Chapter, AudiobookMetadata, Cover, Result, Audiobook, BookId
from audiobookdl.exceptions import DataNotPresent
from audiobookdl.exceptions import DataNotPresent, GenericAudiobookDLException
from audiobookdl.utils import CustomSSLContextHTTPAdapter

# External imports
Expand Down Expand Up @@ -179,17 +179,20 @@ def find_all_in_page(self, url: str, regex: str, **kwargs) -> list:
def create_ssl_context(self, options: Any) -> SSLContext:
# Custom SSLContext's are broken in requests version 2.32.0/2.32.1/2.32.2
# fixed in version 2.32.3: https://github.com/psf/requests/pull/6716
ssl_context: SSLContext = urllib3.util.create_urllib3_context() # type: ignore[attr-defined]

# Workaround for regression in requests version 2.32.3
# https://github.com/psf/requests/issues/6730
ssl_context.load_default_certs()

# Prevent the padding extension from appearing in the TLS ClientHello
# It's used by Cloudflare for bot detection
# See issue #106
ssl_context.options &= ~(1 << 4) # SSL_OP_TLSEXT_PADDING
return ssl_context
try:
ssl_context: SSLContext = urllib3.util.create_urllib3_context()

# Workaround for regression in requests version 2.32.3
# https://github.com/psf/requests/issues/6730
ssl_context.load_default_certs()

# Prevent the padding extension from appearing in the TLS ClientHello
# It's used by Cloudflare for bot detection
# See issue #106
ssl_context.options &= ~(1 << 4) # SSL_OP_TLSEXT_PADDING
return ssl_context
except AttributeError: # AttributeError: module 'urllib3.util' has no attribute 'create_urllib3_context'
raise GenericAudiobookDLException(f"Please update urllib3 to version >= 2 using the command 'pip install -U urllib3'")

def create_session(self, options: Any) -> requests.Session:
session = requests.Session()
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ dependencies = [
"m3u8",
"mutagen",
"pillow",
"pycountry",
"pycryptodome",
"requests",
"rich",
"tomli",
"pycountry",
"urllib3>=2",
]
dynamic = ["version"]

Expand Down

0 comments on commit b470d65

Please sign in to comment.