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

Use curl_cffi for requests #142

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion audiobookdl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .sources.source import Source
from . import utils

__version__ = "0.7.3"
__version__ = "0.7.7"
25 changes: 3 additions & 22 deletions audiobookdl/sources/source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from audiobookdl.utils import CustomSSLContextHTTPAdapter

# External imports
import requests
import curl_cffi
import lxml.html
from lxml.cssselect import CSSSelector
import re
Expand Down Expand Up @@ -176,25 +176,6 @@ def find_all_in_page(self, url: str, regex: str, **kwargs) -> list:
get_json = networking.get_json
get_stream_files = networking.get_stream_files

def create_ssl_context(self, options: Any) -> SSLContext:
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()
ssl_context: SSLContext = self.create_ssl_context(options)
# session.adapters.pop("https://", None)
session.mount("https://", CustomSSLContextHTTPAdapter(ssl_context))
def create_session(self, options: Any) -> curl_cffi.requests.Session:
session = curl_cffi.requests.Session()
return session
4 changes: 2 additions & 2 deletions audiobookdl/sources/storytel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from requests.models import Response
from curl_cffi.requests.models import Response
from .source import Source
from audiobookdl import (
AudiobookFile,
Expand Down Expand Up @@ -164,7 +164,7 @@ def _login(self, url: str, username: str, password: str) -> None:
self._password = self.encrypt_password(password)
self._session.headers.update(
{
"User-Agent": "Storytel/24.22 (Android 14; Google Pixel 8 Pro) Release/2288629",
"User-Agent": "Storytel/24.52 (Android 14; Google Pixel 8 Pro) Release/2288809",
}
)
self._do_login()
Expand Down
2 changes: 1 addition & 1 deletion audiobookdl/utils/audiobook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import date
import requests
from curl_cffi import requests
from typing import Dict, Generic, List, Optional, Union, Sequence, Tuple, TypeVar, Any, MutableMapping
import json
from attrs import define, Factory
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies = [
"rich",
"tomli",
"urllib3>=2",
"curl_cffi",
]
dynamic = ["version"]

Expand Down