Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: match some sites by exact domain /subdomain
NTFSvolume7d3b0e58411a
diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.pyindex 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, Scrapfrom cyberdrop_dl.utils import css, dates, enter_context, fast_cache, is_blob_or_svg, m3u8, parse_url, uniquefrom 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 = Noneallow_empty_path: bool | None = Noneignore_fragment: bool | None = None+ strict_host_match: bool | None = None@final@@ -250,7 +252,10 @@ class Crawler(HTTPMixin, HLSMixin, ABC):@classmethoddef 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 Nonediff --git a/cyberdrop_dl/crawlers/gofile.py b/cyberdrop_dl/crawlers/gofile.pyindex 2a13a5f1f..f17777a70 100644--- a/cyberdrop_dl/crawlers/gofile.py+++ b/cyberdrop_dl/crawlers/gofile.py@@ -10,7 +10,7 @@ from typing_extensions import ReadOnlyfrom cyberdrop_dl import aio, envfrom cyberdrop_dl.cache import disk_cached_methodfrom 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, URLConfigfrom cyberdrop_dl.exceptions import PasswordProtectedError, ScrapeErrorfrom cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem, ScrapeItemTypefrom 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.pyindex 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.pyindex 55fddcb2f..3adbd31a9 100644--- a/cyberdrop_dl/crawlers/nova.py+++ b/cyberdrop_dl/crawlers/nova.py@@ -2,6 +2,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING, ClassVar, override+from cyberdrop_dl.crawlers.crawler import URLConfigfrom cyberdrop_dl.crawlers.pixeldrain import PixelDrainAPI, PixelDrainCrawlerfrom cyberdrop_dl.url_objects import AbsoluteHttpURLfrom 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.pyindex 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, overridefrom cyberdrop_dl import aiofrom 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 ScrapeErrorfrom cyberdrop_dl.models import type_adapterfrom 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.pyindex 56476bf33..fba379f42 100644--- a/cyberdrop_dl/crawlers/twitter/crawler.py+++ b/cyberdrop_dl/crawlers/twitter/crawler.py@@ -6,7 +6,7 @@ import datetime as dtfrom pathlib import Pathfrom typing import TYPE_CHECKING, ClassVar, final, override-from cyberdrop_dl.crawlers.crawler import Crawler, SupportedDomains, SupportedPaths+from cyberdrop_dl.crawlers.crawler import Crawler, SupportedPaths, URLConfigfrom cyberdrop_dl.crawlers.twitter.api import FXTwitterAPI, TwitterAPI, TwitterGQLSearchFilterfrom cyberdrop_dl.exceptions import ScrapeErrorfrom 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 ValueErrorawait 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@overridedef separate_posts(self) -> bool:diff --git a/cyberdrop_dl/utils/_url.py b/cyberdrop_dl/utils/_url.pyindex 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.pyindex cfdf335b9..922266097 100644--- a/tests/test_utils.py+++ b/tests/test_utils.py@@ -11,7 +11,7 @@ from cyberdrop_dl.exceptions import InvalidExtensionError, NoExtensionErrorfrom cyberdrop_dl.filepath import get_filename_and_extfrom cyberdrop_dl.url_objects import AbsoluteHttpURLfrom 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_urlclass 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