Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require urllib3>=2 #116

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- uses: cachix/install-nix-action@v13
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: nix-shell --command "mypy audiobookdl"
4 changes: 2 additions & 2 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- uses: cachix/install-nix-action@v13
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: nix-shell --command "python3 -m pytest"
29 changes: 15 additions & 14 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 @@ -177,19 +177,20 @@ def find_all_in_page(self, url: str, regex: str, **kwargs) -> list:
get_stream_files = networking.get_stream_files

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() # 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
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
3 changes: 2 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ with import <nixpkgs> {};
mkShell {
buildInputs = [
ffmpeg
(python3.withPackages(ps: with ps; [
(python312.withPackages(ps: with ps; [
mutagen
requests
rich
Expand All @@ -17,6 +17,7 @@ mkShell {
tomli
attrs
pycountry
urllib3

# Test
pytest
Expand Down