Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
fix: ignore low config timeouts for flaresolverr requests (#2190)
NTFSvolumec8447c59ac87
diff --git a/cyberdrop_dl/clients/flaresolverr.py b/cyberdrop_dl/clients/flaresolverr.pyindex 89314cfa1..b83499e84 100644--- a/cyberdrop_dl/clients/flaresolverr.py+++ b/cyberdrop_dl/clients/flaresolverr.py@@ -28,7 +28,8 @@ if TYPE_CHECKING:logger = get_logger(__name__)USER_AGENT: ContextVar[str] = ContextVar("USER_AGENT")-MAX_TIMEOUT = 60_000+MAX_TIMEOUT = 60+MAX_TIMEOUT_MILIS = MAX_TIMEOUT * 1000_CF_BLOCKED_MSG = "Cloudflare has blocked this request"@@ -102,16 +103,20 @@ class Response:)[email protected](slots=True, frozen=True)+DEFAULT_TIMEOUT = aiohttp.ClientTimeout(sock_read=MAX_TIMEOUT, sock_connect=MAX_TIMEOUT)++[email protected](slots=True)class Request:command: RequestCommandpayload: dict[str, Any]- aiohttp_params: dict[str, Any] = dataclasses.field(default_factory=dict)+ timeout: aiohttp.ClientTimeout = dataclasses.field(default_factory=lambda: DEFAULT_TIMEOUT)def __post_init__(self) -> None:- wait: int | None = self.payload.get("waitInSeconds")- if wait and "timeout" not in self.aiohttp_params:- self.aiohttp_params["timeout"] = aiohttp.ClientTimeout(sock_read=wait + 60, sock_connect=60)+ wait: int = self.payload.get("waitInSeconds") or 0+ self.timeout = _merge_timeouts(+ self.timeout, aiohttp.ClientTimeout(sock_read=wait + MAX_TIMEOUT, sock_connect=MAX_TIMEOUT)+ )@classmethoddef build(@@ -124,7 +129,7 @@ class Request:wait: int | None,session: str | None,) -> Self:- payload: dict[str, Any] = {"cmd": str(command), "maxTimeout": MAX_TIMEOUT, "url": str(url)}+ payload: dict[str, Any] = {"cmd": str(command), "maxTimeout": MAX_TIMEOUT_MILIS, "url": str(url)}if wait := max(wait or 0, config.wait):payload["waitInSeconds"] = wait@@ -133,7 +138,7 @@ class Request:elif config.proxy:payload["proxy"] = {"url": str(config.proxy)}- return cls(command, payload, {})+ return cls(command, payload)@dataclass_repr("resp")@@ -271,7 +276,7 @@ class Client:await self._ensure_session()with self._disable_on_error():req = build_request(url, self.config, params, self.session)- resp = await self._request(req.command, req.payload, **req.aiohttp_params)+ resp = await self._request(req.command, req.payload, timeout=req.timeout)if not resp.ok:if _CF_BLOCKED_MSG in resp.message:raise CloudflareBlockedError(resp.message)@@ -282,12 +287,20 @@ class Client:return resp.solution- async def _request(self, command: Command, /, json: dict[str, Any], **aiohttp_params: Any) -> Response:- payload = {"cmd": str(command), "maxTimeout": MAX_TIMEOUT} | json-+ async def _request(+ self,+ command: Command,+ /,+ json: dict[str, Any],+ *,+ timeout: aiohttp.ClientTimeout = DEFAULT_TIMEOUT, # noqa: ASYNC109+ ) -> Response:+ payload = {"cmd": str(command), "maxTimeout": MAX_TIMEOUT_MILIS} | jsonasync with self._new_request(command) as request_id:logger.traffic("Making FlareSolverr request [id=%s]\n%s", request_id, payload)- async with self.http.post(self.config.url, json=payload, **aiohttp_params) as response:+ async with self.http.post(+ self.config.url, json=payload, timeout=_merge_timeouts(self.http.timeout, timeout)+ ) as response:data = await response.json()try:return Response.parse(request_id, data)@@ -387,3 +400,19 @@ async def verify_solution(cdl_user_agent: str, solution: Solution) -> None:if not solution.cookies:logger.warning("Got empty cookies from Flaresolverr request %s", solution.id)+++def _merge_timeouts(a: aiohttp.ClientTimeout, b: aiohttp.ClientTimeout) -> aiohttp.ClientTimeout:+ def max_or_none(val: float | None, val2: float | None) -> float | None:+ if val is None and val2 is None:+ return None++ return max(val or 0, val2 or 0)++ return aiohttp.ClientTimeout(+ total=max_or_none(a.total, b.total),+ connect=max_or_none(a.connect, b.connect),+ sock_read=max_or_none(a.sock_read, b.sock_read),+ sock_connect=max_or_none(a.sock_connect, b.sock_connect),+ ceil_threshold=max(a.ceil_threshold, b.ceil_threshold),+ )diff --git a/tests/test_flaresolverr.py b/tests/test_flaresolverr.pyindex f1b962e21..3384045fc 100644--- a/tests/test_flaresolverr.py+++ b/tests/test_flaresolverr.py@@ -6,6 +6,7 @@ import pytestfrom cyberdrop_dl.clients import HttpMethodfrom cyberdrop_dl.clients.flaresolverr import (+ MAX_TIMEOUT,Client,Command,Config,@@ -144,7 +145,7 @@ def _config(*, proxy: bool, use_session: bool) -> Config:"waitInSeconds": 20,"session": "sessionA",},- {"timeout": aiohttp.ClientTimeout(sock_read=80, sock_connect=60)},+ aiohttp.ClientTimeout(sock_read=80, sock_connect=60),),),(@@ -163,7 +164,7 @@ def _config(*, proxy: bool, use_session: bool) -> Config:"url": "https://example.com/proxy",},},- {"timeout": aiohttp.ClientTimeout(sock_read=80, sock_connect=60)},+ aiohttp.ClientTimeout(sock_read=80, sock_connect=60),),),],@@ -175,7 +176,28 @@ def test_request_build(req = Request.build(command, AbsoluteHttpURL("https://example.com"), config, wait=wait, session=session)assert req.command is expected.commandassert req.payload == expected.payload- assert req.aiohttp_params == expected.aiohttp_params+ assert req.timeout == expected.timeout+++def test_request_always_overrides_min_timeout() -> None:++ for req in (+ Request(Command.GET_REQUEST, {}),+ Request(Command.GET_REQUEST, {}, aiohttp.ClientTimeout(sock_read=1, sock_connect=1)),+ ):+ assert req.timeout.sock_read+ assert req.timeout.sock_read >= MAX_TIMEOUT+ assert req.timeout.sock_connect+ assert req.timeout.sock_connect >= MAX_TIMEOUT+++def test_wait_in_seconds_overrides_read() -> None:+ wait = 120+ req = Request(Command.GET_REQUEST, {"waitInSeconds": wait})+ assert req.timeout.sock_read+ assert req.timeout.sock_read >= MAX_TIMEOUT + wait+ assert req.timeout.sock_connect+ assert req.timeout.sock_connect >= MAX_TIMEOUT_CF_BLOCKED = (