Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: handle some error from previous commits + handle cloudflare (#322)

datawhores890e9174f71d
diff --git a/cyberdrop_dl/clients/scraper_client.py b/cyberdrop_dl/clients/scraper_client.py
index 112af7447..b0df02236 100644
--- a/cyberdrop_dl/clients/scraper_client.py
+++ b/cyberdrop_dl/clients/scraper_client.py
@@ -101,7 +101,7 @@ class ScraperClient:
origin,
)
# retry request with flaresolverr cookies
- if self.client_manager.check_ddos_guard(soup):
+ if self.client_manager.check_ddos_guard(soup) or self.client_manager.check_cloudflare(soup):
if not retry:
raise DDOSGuardError(message="Unable to access website with flaresolverr cookies") from None
return await self.get_soup(domain, url, client_session, origin, with_response_url, retry=False)
@@ -174,7 +174,7 @@ class ScraperClient:
await self.client_manager.check_http_status(response, origin=origin)
except DDOSGuardError:
soup, _ = await self.client_manager.flaresolverr.get(url, client_session, origin)
- if self.client_manager.check_ddos_guard(soup):
+ if self.client_manager.check_ddos_guard(soup) or self.client_manager.check_cloudflare(soup):
if not retry:
raise DDOSGuardError(message="Unable to access website with flaresolverr cookies") from None
return await self.get_text(domain, url, client_session, origin, retry=False)
diff --git a/cyberdrop_dl/managers/client_manager.py b/cyberdrop_dl/managers/client_manager.py
index b2729da9d..67c1a53e3 100644
--- a/cyberdrop_dl/managers/client_manager.py
+++ b/cyberdrop_dl/managers/client_manager.py
@@ -41,6 +41,9 @@ DDOS_GUARD_CHALLENGE_SELECTORS = [
".lds-ring",
]
+CLOUDFLARE_CHALLENGE_TITLES = ["Simpcity Cuck Detection"]
+CLOUDFLARE_CHALLENGE_SELECTORS = ["captchawrapper", "cf-turnstile"]
+
class ClientManager:
"""Creates a 'client' that can be referenced by scraping or download sessions."""
@@ -129,9 +132,6 @@ class ClientManager:
message = DOWNLOAD_ERROR_ETAGS.get(headers.get("ETag"))
raise DownloadError(HTTPStatus.NOT_FOUND, message=message, origin=origin)
- if HTTPStatus.OK <= status < HTTPStatus.BAD_REQUEST:
- return
-
if any(domain in response.url.host for domain in ("gofile", "imgur")):
with contextlib.suppress(ContentTypeError):
JSON_Resp: dict = await response.json()
@@ -146,9 +146,12 @@ class ClientManager:
if response_text:
soup = BeautifulSoup(response_text, "html.parser")
- if cls.check_ddos_guard(soup):
+ if cls.check_ddos_guard(soup) or cls.check_cloudflare(soup):
raise DDOSGuardError(origin=origin)
+ if HTTPStatus.OK <= status < HTTPStatus.BAD_REQUEST:
+ return
+
status = status if headers.get("Content-Type") else CustomHTTPStatus.IM_A_TEAPOT
message = "No content-type in response header" if headers.get("Content-Type") else None
@@ -177,6 +180,21 @@ class ClientManager:
return False
+ @staticmethod
+ def check_cloudflare(soup: BeautifulSoup) -> bool:
+ if soup.title:
+ for title in CLOUDFLARE_CHALLENGE_TITLES:
+ challenge_found = title.casefold() == soup.title.string.casefold()
+ if challenge_found:
+ return True
+
+ for selector in CLOUDFLARE_CHALLENGE_SELECTORS:
+ challenge_found = soup.find(selector)
+ if challenge_found:
+ return True
+
+ return False
+
async def close(self) -> None:
await self.flaresolverr._destroy_session()
diff --git a/cyberdrop_dl/managers/config_manager.py b/cyberdrop_dl/managers/config_manager.py
index e260e573d..f6f69008e 100644
--- a/cyberdrop_dl/managers/config_manager.py
+++ b/cyberdrop_dl/managers/config_manager.py
@@ -160,17 +160,15 @@ class ConfigManager:
default_settings_data = copy.deepcopy(settings)
existing_settings_data = _load_yaml(self.settings)
self.settings_data = _match_config_dicts(default_settings_data, existing_settings_data)
- paths = {
- (
- [
- ("Files", "input_file"),
- ("Files", "download_folder"),
- ("Logs", "log_folder"),
- ("Sorting", "sort_folder"),
- ("Sorting", "scan_folder"),
- ]
- )
- }
+ paths = set(
+ [
+ ("Files", "input_file"),
+ ("Files", "download_folder"),
+ ("Logs", "log_folder"),
+ ("Sorting", "sort_folder"),
+ ("Sorting", "scan_folder"),
+ ]
+ )
enums = {("Dupe_Cleanup_Options", "hashing"): Hashing}
for key, value in default_settings_data.items():
for subkey, subvalue in value.items():
diff --git a/cyberdrop_dl/utils/cookie_extraction.py b/cyberdrop_dl/utils/cookie_extraction.py
index c43215a67..ae9c3a235 100644
--- a/cyberdrop_dl/utils/cookie_extraction.py
+++ b/cyberdrop_dl/utils/cookie_extraction.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import contextlib
+import re
from functools import wraps
from http.cookiejar import MozillaCookieJar
from textwrap import dedent
@@ -62,9 +63,12 @@ def get_cookies_from_browsers(
raise ValueError(msg)
browsers = browsers or manager.config_manager.settings_data["Browser_Cookies"]["browsers"]
- domains = domains or manager.config_manager.settings_data["Browser_Cookies"]["sites"]
- browsers = list(map(str.lower, browsers))
- domains = list(map(str.lower, domains))
+ if browsers:
+ browsers = list(map(str.lower, re.split(r"[ ,]+", browsers)))
+ if domains:
+ domains = list(map(str.lower, re.split(r"[ ,]+", domains)))
+ else:
+ domains = list(SupportedDomains.supported_hosts)
extractors = [getattr(browser_cookie3, b) for b in browsers if hasattr(browser_cookie3, b)]
@@ -72,16 +76,16 @@ def get_cookies_from_browsers(
msg = "None of the provided browsers is not supported for extraction"
raise ValueError(msg)
- for extractor in extractors:
- for domain in domains:
- cookie_jar = MozillaCookieJar()
+ for domain in domains:
+ cookie_jar = MozillaCookieJar()
+ for extractor in extractors:
cookies = extractor(domain_name=domain)
for cookie in cookies:
cookie_jar.set_cookie(cookie)
manager.path_manager.cookies_dir.mkdir(parents=True, exist_ok=True)
cookie_file_path = manager.path_manager.cookies_dir / f"{domain}.txt"
update_forum_config_cookies(manager, domain, cookies)
- cookie_jar.save(cookie_file_path, ignore_discard=True, ignore_expires=True)
+ cookie_jar.save(cookie_file_path, ignore_discard=True, ignore_expires=True)
def update_forum_config_cookies(manager: Manager, forum: str, cookie: CookieJar) -> None:
diff --git a/docs/reference/configuration-options/settings.md b/docs/reference/configuration-options/settings.md
index a298adf38..8930742c0 100644
--- a/docs/reference/configuration-options/settings.md
+++ b/docs/reference/configuration-options/settings.md
@@ -507,10 +507,10 @@ These are options for enable/disable hashing and auto dupe delecton
## Enabling auto dupe cleanup
1. Turn change hashing to 'POST_DOWNLOAD' or 'IN_PLACE'
2. Change auto_dedupe to True
-
+
***
-* hashing
+* hashing
There are three possible options for hashing
1. OFF: disables hashing
2. IN_PLACE: performs hashing after each download
@@ -522,11 +522,11 @@ when hashing is enabled all files are hashed with at least xxh128 hashing
* auto\_dedupe:
allows for deduping files when hashing is enabled
-
+
This finds all files in the database with the same hash and size, and keeps the oldest copy of the file
Deletion only occurs if two or more matching files are found from the database search
-
+
***
* add\_sha256\_hash