Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: handle flaresolverr invalid response

NTFSvolume110315bbe641
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 4fc498cd3..16c44f603 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -82,8 +82,7 @@ class ScraperClient:
url: URL,
client_session: ClientSession,
origin: ScrapeItem | URL | None = None,
- with_response_url: bool = False,
- ) -> tuple[str, URL | None]:
+ ) -> tuple[BeautifulSoup, URL]:
"""Returns the resolved URL from the given URL."""
if not self.client_manager.flaresolverr:
raise DDOSGuardError(message="FlareSolverr is not configured", origin=origin)
@@ -104,10 +103,12 @@ class ScraperClient:
raise DDOSGuardError(message="Failed to resolve URL with flaresolverr", origin=origin)
solution: dict = json_obj.get("solution")
- response: str = solution.get("response")
- response_url = solution.get("url") if with_response_url else None
- if response_url:
- response_url = URL(response_url)
+ response = BeautifulSoup(solution.get("response"), "html.parser")
+ response_url = URL(solution.get("url"))
+
+ if self.client_manager.check_ddos_guard(response):
+ raise DDOSGuardError(message="Invalid response from flaresolverr", origin=origin)
+
return response, response_url
@limiter
@@ -133,11 +134,10 @@ class ScraperClient:
domain,
url,
origin=origin,
- with_response_url=with_response_url,
)
if with_response_url:
- return BeautifulSoup(response, "html.parser"), response_URL
- return BeautifulSoup(response, "html.parser")
+ return response, response_URL
+ return response
content_type = response.headers.get("Content-Type")
assert content_type is not None
@@ -202,8 +202,8 @@ class ScraperClient:
try:
await self.client_manager.check_http_status(response, origin=origin)
except DDOSGuardError:
- response_text, _ = await self.flaresolverr(domain, url)
- return response_text
+ response_text, _ = await self.flaresolverr(domain, url, origin=origin)
+ return str(response_text)
return await response.text()
@limiter
diff --git a/cyberdrop_dl/managers/client_manager.py b/cyberdrop_dl/managers/client_manager.py
index e9e126857..68fbca8e9 100644
--- a/cyberdrop_dl/managers/client_manager.py
+++ b/cyberdrop_dl/managers/client_manager.py
@@ -10,6 +10,7 @@ import aiohttp
import certifi
from aiohttp import ClientResponse, ContentTypeError
from aiolimiter import AsyncLimiter
+from bs4 import BeautifulSoup
from cyberdrop_dl.clients.download_client import DownloadClient
from cyberdrop_dl.clients.errors import DDOSGuardError, DownloadError, ScrapeError
@@ -18,6 +19,7 @@ from cyberdrop_dl.managers.leaky import LeakyBucket
from cyberdrop_dl.utils.constants import CustomHTTPStatus
if TYPE_CHECKING:
+ from bs4 import BeautifulSoup
from yarl import URL
from cyberdrop_dl.managers.manager import Manager
@@ -28,6 +30,18 @@ DOWNLOAD_ERROR_ETAGS = {
"65b7753c-528a": "SC Scrape Image",
}
+DDOS_GUARD_CHALLENGE_TITLES = ["Just a moment...", "DDoS-Guard"]
+DDOS_GUARD_CHALLENGE_SELECTORS = [
+ "#cf-challenge-running",
+ ".ray_id",
+ ".attack-box",
+ "#cf-please-wait",
+ "#challenge-spinner",
+ "#trk_jschal_js",
+ "#turnstile-wrapper",
+ ".lds-ring",
+]
+
class ClientManager:
"""Creates a 'client' that can be referenced by scraping or download sessions."""
@@ -105,8 +119,9 @@ class ClientManager:
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
- @staticmethod
+ @classmethod
async def check_http_status(
+ cls,
response: ClientResponse,
download: bool = False,
origin: ScrapeItem | URL | None = None,
@@ -132,7 +147,8 @@ class ClientManager:
with contextlib.suppress(UnicodeDecodeError):
response_text = await response.text()
- if "<title>DDoS-Guard</title>" in response_text:
+ soup = BeautifulSoup(response_text)
+ if cls.check_ddos_guard(soup):
raise DDOSGuardError(origin=origin)
status = status if headers.get("Content-Type") else CustomHTTPStatus.IM_A_TEAPOT
@@ -147,3 +163,17 @@ class ClientManager:
async def check_bucket(self, size: float) -> None:
await self._leaky_bucket.acquire(size)
+
+ @staticmethod
+ def check_ddos_guard(soup: BeautifulSoup) -> bool:
+ for title in DDOS_GUARD_CHALLENGE_TITLES:
+ challenge_found = title.casefold() == soup.title.string.casefold()
+ if challenge_found:
+ return True
+
+ for selector in DDOS_GUARD_CHALLENGE_SELECTORS:
+ challenge_found = soup.find(selector)
+ if challenge_found:
+ return True
+
+ return False