Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
feat: support exact domain matching
NTFSvolume139be91bbc7c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d17cbfe59..35816c462 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Changed- Allow multiple concurrent requests with Flaresolverr+- `--skip-hosts` and `--only-hosts` now perform exact domain matching if an absolute URL is provided.+ ex: `--skip-hosts https://x.com` will skip `x.com` URLs but not `vix.com` URLs. Using `--skip-hosts x.com` will skip both### Fixeddiff --git a/cyberdrop_dl/constants.py b/cyberdrop_dl/constants.pyindex 32b73e238..f375dd466 100644--- a/cyberdrop_dl/constants.py+++ b/cyberdrop_dl/constants.py@@ -47,9 +47,8 @@ class TempExt(StrEnum):PART = ".part"-@final-class BlockedDomains:- partial_match = (+BLOCKED_DOMAINS = frozenset(+ ("facebook","instagram","fbcdn",@@ -65,8 +64,7 @@ class BlockedDomains:"beacons.ai","allmylinks.com",)-- exact_match = ()+)class HashMode(CIStrEnum):diff --git a/cyberdrop_dl/crawlers/crawler.py b/cyberdrop_dl/crawlers/crawler.pyindex 28b506a58..dbecf8942 100644--- a/cyberdrop_dl/crawlers/crawler.py+++ b/cyberdrop_dl/crawlers/crawler.py@@ -28,7 +28,7 @@ from cyberdrop_dl.mediaprops import ISO639Subtitle, Resolutionfrom cyberdrop_dl.models.validators import stringsfrom cyberdrop_dl.url_objects import AbsoluteHttpURL, MediaItem, ScrapeItem, is_absolute_http_urlfrom cyberdrop_dl.utils import css, dates, enter_context, fast_cache, is_blob_or_svg, m3u8, parse_url, unique-from cyberdrop_dl.utils._url import remove_trailing_slash+from cyberdrop_dl.utils._url import matches_any_host, remove_trailing_slashfrom cyberdrop_dl.utils.dataclass import ConfigDataclass, DictDataclass, frozenfrom cyberdrop_dl.utils.errors import error_handling_context@@ -1161,14 +1161,13 @@ def auto_task_id[CrawlerT: Crawler, **P, R](def _should_skip_by_config(media_item: MediaItem, config: Config) -> bool:- media_host = media_item.url.hostfilters = config.filters- if (hosts := filters.skip_hosts) and any(host in media_host for host in hosts):+ if (hosts := filters.skip_hosts) and matches_any_host(media_item.url, hosts):logger.info(f"Download skipped {media_item.url} due to skip_hosts config")return True- if (hosts := filters.only_hosts) and not any(host in media_host for host in hosts):+ if (hosts := filters.only_hosts) and not matches_any_host(media_item.url, hosts):logger.info(f"Download skipped {media_item.url} due to only_hosts config")return Truediff --git a/cyberdrop_dl/scrape_mapper.py b/cyberdrop_dl/scrape_mapper.pyindex 5cc51f480..84c64700f 100644--- a/cyberdrop_dl/scrape_mapper.py+++ b/cyberdrop_dl/scrape_mapper.py@@ -9,7 +9,7 @@ from pathlib import Pathfrom typing import TYPE_CHECKING, Literal, Self, overridefrom cyberdrop_dl import aio, env, filepath, storage-from cyberdrop_dl.constants import BlockedDomains+from cyberdrop_dl.constants import BLOCKED_DOMAINSfrom cyberdrop_dl.crawlers import ALLOW_NO_EXT, create_crawlersfrom cyberdrop_dl.csv_logs import CSVLogsManagerfrom cyberdrop_dl.exceptions import JDownloaderError, NoExtensionError@@ -26,6 +26,7 @@ from cyberdrop_dl.scrape_source import ()from cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem, ScrapeItemTypefrom cyberdrop_dl.utils import remove_trailing_slash+from cyberdrop_dl.utils._url import matches_any_hostif TYPE_CHECKING:from collections.abc import AsyncGenerator, Generator, Iterable, Iterator@@ -471,17 +472,17 @@ def _parse_source(def _skip_by_config(url: AbsoluteHttpURL, config: Config) -> bool:- if _filter_by_domain(url, BlockedDomains.partial_match) or url.host in BlockedDomains.exact_match:+ if matches_any_host(url, BLOCKED_DOMAINS):logger.info("Skipping %s as it is a blocked domain", url)return Truehosts = config.filters.skip_hosts- if hosts and _filter_by_domain(url, hosts):+ if hosts and matches_any_host(url, hosts):logger.info("Skipping %s by skip_hosts config", url)return Truehosts = config.filters.only_hosts- if hosts and not _filter_by_domain(url, hosts):+ if hosts and not matches_any_host(url, hosts):logger.info("Skipping %s by only_hosts config", url)return Truediff --git a/cyberdrop_dl/utils/_url.py b/cyberdrop_dl/utils/_url.pyindex 7be0a5ebb..f1e935d76 100644--- a/cyberdrop_dl/utils/_url.py+++ b/cyberdrop_dl/utils/_url.py@@ -1,11 +1,15 @@from __future__ import annotationsimport re+from typing import TYPE_CHECKINGimport yarlfrom cyberdrop_dl.url_objects import AbsoluteHttpURL, is_absolute_http_url+if TYPE_CHECKING:+ from collections.abc import Iterable+def fix_query_params_encoding(link: str) -> str:if "?" not in link:@@ -67,3 +71,17 @@ def remove_trailing_slash(url: AbsoluteHttpURL) -> AbsoluteHttpURL:if url.name or url.path == "/":return urlreturn url.parent.with_fragment(url.fragment).with_query(url.query)+++def matches_any_host(url: yarl.URL, hosts: Iterable[str]) -> bool:+ if not url.host:+ return False++ assert not isinstance(hosts, str)+ for host in hosts:+ if host in url.host:+ return True+ if host.startswith(("https://", "http://")) and yarl.URL(host).host == url.host:+ return True++ return Falsediff --git a/tests/test_utils.py b/tests/test_utils.pyindex 0e1ced668..4c35cd0a3 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, parse_http_url+from cyberdrop_dl.utils._url import fix_multi_slashes, matches_any_host, parse_http_urlclass TestGetFilenameAndExt:@@ -228,3 +228,17 @@ def test_file_browser() -> None:browsers = tuple(f.name for f in file_browser.get_file_browsers())assert browsers == expected+++ ("url", "hosts", "expected"),+ [+ ("https://x.com", {"x.com"}, True),+ ("https://x.com", {"http://x.com"}, True),+ ("https://x.com", {"https://x.com"}, True),+ ("https://vix.com", {"x.com"}, True),+ ("https://vix.com", {"https://x.com"}, False),+ ],+)+def test_matches_any_host(url: str, hosts: tuple[str, ...], *, expected: bool) -> None:+ assert matches_any_host(AbsoluteHttpURL(url), hosts) is expected