Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: match some sites by exact domain /subdomain

NTFSvolume7d3b0e58411a
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.py
index 3423088f5..622d27c21 100644
--- a/cyberdrop_dl/crawlers/crawler.py
+++ b/cyberdrop_dl/crawlers/crawler.py
@@ -37,6 +37,7 @@ from cyberdrop_dl.url_objects import AbsoluteHttpURL, MediaItem, MuxVideo, Scrap
from cyberdrop_dl.utils import css, dates, enter_context, fast_cache, is_blob_or_svg, m3u8, parse_url, unique
from cyberdrop_dl.utils._url import (
URLDeduper,
+ is_within_domain,
matches_any_host,
path_frag,
path_qs,
@@ -190,6 +191,7 @@ class URLConfig(ConfigDataclass):
trim: bool | None = None
allow_empty_path: bool | None = None
ignore_fragment: bool | None = None
+ strict_host_match: bool | None = None
@final
@@ -250,7 +252,10 @@ class Crawler(HTTPMixin, HLSMixin, ABC):
@classmethod
def check_host_match(cls, host: str) -> bool:
- return bool(host)
+ if not cls.__url_config__.strict_host_match:
+ return True
+
+ return any(is_within_domain(domain, host) for domain in cls.SUPPORTED_DOMAINS)
def _build_downloader[T: Downloader](self, cls: type[T] = Downloader) -> T:
assert self.__dl_config__.server_lock is not None
diff --git a/cyberdrop_dl/crawlers/gofile.py b/cyberdrop_dl/crawlers/gofile.py
index 2a13a5f1f..f17777a70 100644
--- a/cyberdrop_dl/crawlers/gofile.py
+++ b/cyberdrop_dl/crawlers/gofile.py
@@ -10,7 +10,7 @@ from typing_extensions import ReadOnly
from cyberdrop_dl import aio, env
from cyberdrop_dl.cache import disk_cached_method
from cyberdrop_dl.clients.http import HTTPConfig
-from cyberdrop_dl.crawlers.crawler import API, Crawler, SupportedPaths
+from cyberdrop_dl.crawlers.crawler import API, Crawler, SupportedDomains, SupportedPaths, URLConfig
from cyberdrop_dl.exceptions import PasswordProtectedError, ScrapeError
from cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem, ScrapeItemType
from cyberdrop_dl.utils.errors import error_handling_wrapper
@@ -20,7 +20,9 @@ if TYPE_CHECKING:
@HTTPConfig(rate_limit=(4, 10))
+@URLConfig(strict_host_match=True)
class GoFileCrawler(Crawler):
+ SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = ("gofile.io",)
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
"Folder / File": "/d/<content_id>",
"Direct link": (
diff --git a/cyberdrop_dl/crawlers/mega_nz.py b/cyberdrop_dl/crawlers/mega_nz.py
index 26c1d401f..212e3167b 100644
--- a/cyberdrop_dl/crawlers/mega_nz.py
+++ b/cyberdrop_dl/crawlers/mega_nz.py
@@ -29,7 +29,7 @@ if TYPE_CHECKING:
@HTTPConfig.default_headers(user_agent=CDL_USER_AGENT)
-@URLConfig(allow_empty_path=True)
+@URLConfig(allow_empty_path=True, strict_host_match=True)
@Crawler.db_path_builder("path_qs_frag")
class MegaNzCrawler(Crawler):
SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = "mega.io", "mega.nz"
diff --git a/cyberdrop_dl/crawlers/nova.py b/cyberdrop_dl/crawlers/nova.py
index 55fddcb2f..3adbd31a9 100644
--- a/cyberdrop_dl/crawlers/nova.py
+++ b/cyberdrop_dl/crawlers/nova.py
@@ -2,6 +2,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, ClassVar, override
+from cyberdrop_dl.crawlers.crawler import URLConfig
from cyberdrop_dl.crawlers.pixeldrain import PixelDrainAPI, PixelDrainCrawler
from cyberdrop_dl.url_objects import AbsoluteHttpURL
from cyberdrop_dl.utils import basic_auth
@@ -11,6 +12,7 @@ if TYPE_CHECKING:
from cyberdrop_dl.url_objects import ScrapeItem
+@URLConfig(strict_host_match=True)
class NovaStorageCrawler(PixelDrainCrawler):
SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = ()
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
diff --git a/cyberdrop_dl/crawlers/pixeldrain.py b/cyberdrop_dl/crawlers/pixeldrain.py
index 963514722..e1e201019 100644
--- a/cyberdrop_dl/crawlers/pixeldrain.py
+++ b/cyberdrop_dl/crawlers/pixeldrain.py
@@ -8,7 +8,15 @@ from typing import TYPE_CHECKING, Any, ClassVar, Literal, final, override
from cyberdrop_dl import aio
from cyberdrop_dl.clients.http import HTTPConfig
-from cyberdrop_dl.crawlers.crawler import API, Crawler, DownloadConfig, SupportedDomains, SupportedPaths, auto_task_id
+from cyberdrop_dl.crawlers.crawler import (
+ API,
+ Crawler,
+ DownloadConfig,
+ SupportedDomains,
+ SupportedPaths,
+ URLConfig,
+ auto_task_id,
+)
from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.models import type_adapter
from cyberdrop_dl.url_objects import AbsoluteHttpURL
@@ -100,6 +108,7 @@ class PixelDrainProxyCrawler(Crawler):
@HTTPConfig(rate_limit=(10, 1))
@DownloadConfig(slots=2)
+@URLConfig(strict_host_match=True)
class PixelDrainCrawler(Crawler):
SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = (
"pixeldrain.com",
diff --git a/cyberdrop_dl/crawlers/twitter/crawler.py b/cyberdrop_dl/crawlers/twitter/crawler.py
index 56476bf33..fba379f42 100644
--- a/cyberdrop_dl/crawlers/twitter/crawler.py
+++ b/cyberdrop_dl/crawlers/twitter/crawler.py
@@ -6,7 +6,7 @@ import datetime as dt
from pathlib import Path
from typing import TYPE_CHECKING, ClassVar, final, override
-from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths
+from cyberdrop_dl.crawlers.crawler import Crawler, SupportedPaths, URLConfig
from cyberdrop_dl.crawlers.twitter.api import FXTwitterAPI, TwitterAPI, TwitterGQLSearchFilter
from cyberdrop_dl.exceptions import ScrapeError
from cyberdrop_dl.mediaprops import Resolution
@@ -22,20 +22,13 @@ if TYPE_CHECKING:
from cyberdrop_dl.url_objects import ScrapeItem
-class TwitterShortURLCrawler(Crawler):
- SUPPORTED_DOMAINS: ClassVar[SupportedDomains] = ("t.co",)
+@URLConfig(strict_host_match=True)
+class TwitterShortURLCrawler(Crawler, wiki_site="TwitterShortURL"):
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {"Redirect": "t.co/<short_code>"}
PRIMARY_URL: ClassVar[AbsoluteHttpURL] = AbsoluteHttpURL("https://t.co")
DOMAIN: ClassVar[str] = "t.co"
- @classmethod
- @override
- def check_host_match(cls, host: str) -> bool:
- return super().check_host_match(host) and host == "t.co"
-
async def fetch(self, scrape_item: ScrapeItem) -> None:
- if scrape_item.url.host != self.PRIMARY_URL.host:
- raise ValueError
await self._follow_redirect(scrape_item)
@error_handling_wrapper
@@ -87,6 +80,7 @@ class TwimgCrawler(Crawler):
media_item.headers["Referer"] = "https://x.com/"
+@URLConfig(strict_host_match=True)
class TwitterCrawler(Crawler):
SUPPORTED_PATHS: ClassVar[SupportedPaths] = {
"Tweet/Thread/Article": (
@@ -122,11 +116,6 @@ class TwitterCrawler(Crawler):
if before := self.config.filters.before:
self._default_until = int(dt.datetime.combine(before, dt.time.min).timestamp())
- @classmethod
- @override
- def check_host_match(cls, host: str) -> bool:
- return super().check_host_match(host) and (host == "x.com" or not host.endswith("x.com"))
-
@property
@override
def separate_posts(self) -> bool:
diff --git a/cyberdrop_dl/utils/_url.py b/cyberdrop_dl/utils/_url.py
index a91937d54..3e3dd7e91 100644
--- a/cyberdrop_dl/utils/_url.py
+++ b/cyberdrop_dl/utils/_url.py
@@ -145,6 +145,16 @@ def url_hash(url: yarl.URL) -> str:
return str(sort_query(url))
+def reverse_dns_name(host: str) -> tuple[str, ...]:
+ return tuple(reversed(host.split(".")))
+
+
+def is_within_domain(root_domain: str, host: str) -> bool:
+ host = host.lower().removeprefix("www.")
+ root_domain = root_domain.lower().removeprefix("www.")
+ return (host == root_domain) or host.endswith("." + root_domain)
+
+
DedupeT = TypeVar("DedupeT", default=str)
diff --git a/tests/test_utils.py b/tests/test_utils.py
index cfdf335b9..922266097 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -11,7 +11,7 @@ from cyberdrop_dl.exceptions import InvalidExtensionError, NoExtensionError
from cyberdrop_dl.filepath import get_filename_and_ext
from cyberdrop_dl.url_objects import AbsoluteHttpURL
from cyberdrop_dl.utils import file_browser, operators, text_editor
-from cyberdrop_dl.utils._url import fix_multi_slashes, matches_any_host, parse_http_url
+from cyberdrop_dl.utils._url import fix_multi_slashes, is_within_domain, matches_any_host, parse_http_url
class TestGetFilenameAndExt:
@@ -245,3 +245,20 @@ def test_file_browser() -> None:
)
def test_matches_any_host(url: str, hosts: tuple[str, ...], *, expected: bool) -> None:
assert matches_any_host(AbsoluteHttpURL(url), hosts) is expected
+
+
+ ("root_domain", "domain", "expected"),
+ [
+ ("example.com", "example.com", True),
+ ("example.com", "api.example.com", True),
+ ("api.example.com", "example.com", False),
+ ("example.com", "apiexample.com", False),
+ ("example.com", "example.com.co", False),
+ ("example.com", "example.como", False),
+ ("sub1.sub2.example.com", "sub0.sub1.sub2.example.com", True),
+ ("sub1.sub2.example.com", "sub0.sub1.sub3.example.com", False),
+ ],
+)
+def test_is_within_domain(root_domain: str, domain: str, *, expected: bool) -> None:
+ assert is_within_domain(root_domain, domain) is expected