Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use group for max-children

NTFSvolumed3f6fd091f7c
diff --git a/cyberdrop_dl/config/__init__.py b/cyberdrop_dl/config/__init__.py
index d1e35f8ff..161076a4f 100644
--- a/cyberdrop_dl/config/__init__.py
+++ b/cyberdrop_dl/config/__init__.py
@@ -12,14 +12,14 @@ from pydantic import AfterValidator, BaseModel, Field, NonNegativeInt, PositiveI
from cyberdrop_dl.config.appdata import AppData
from cyberdrop_dl.exceptions import CDLConfigRuntimeErrorsGroup, InvalidYamlError
from cyberdrop_dl.models import ConfigModel
-from cyberdrop_dl.models.types import ByteSizeSerilized, FalsyAsTuple # noqa: TC001
+from cyberdrop_dl.models.types import ByteSizeSerilized # noqa: TC001
from cyberdrop_dl.models.validators import to_bytesize
from cyberdrop_dl.utils import cleanup
from .auth import Authentication, Notifications
from .crawlers import Crawlers
from .filters import Filters
-from .settings import Downloads, Hashing, Jdownloader, Logs, Network, Sort, SubFolders, UIOptions
+from .settings import Downloads, Hashing, Jdownloader, Logs, MaxChildren, Network, Sort, SubFolders, UIOptions
if TYPE_CHECKING:
from collections.abc import Iterable
@@ -76,7 +76,7 @@ class Config(ConfigModel, title="cyberdrop-dl config"):
jdownloader: Jdownloader = Field(default_factory=Jdownloader)
logs: Logs = Field(default_factory=Logs)
- max_children: FalsyAsTuple[NonNegativeInt] = ()
+ max_children: MaxChildren = Field(default_factory=MaxChildren)
"Limit the number of items to scrape per category"
max_file_name_length: PositiveInt = 95
diff --git a/cyberdrop_dl/config/default.yaml b/cyberdrop_dl/config/default.yaml
index 189a534d2..976d3596b 100644
--- a/cyberdrop_dl/config/default.yaml
+++ b/cyberdrop_dl/config/default.yaml
@@ -104,7 +104,11 @@ logs:
folder: null
level: DEBUG
rotate: false
-max_children: []
+max_children:
+ album: 0
+ forum: 0
+ forum_post: 0
+ profile: 0
max_file_name_length: 95
max_folder_name_length: 60
max_thread_depth: 0
diff --git a/cyberdrop_dl/config/schema.json b/cyberdrop_dl/config/schema.json
index 3703710ca..3234575c5 100644
--- a/cyberdrop_dl/config/schema.json
+++ b/cyberdrop_dl/config/schema.json
@@ -339,13 +339,6 @@
}
]
},
- "FalsyAsTuple_Annotated_int__Ge__": {
- "items": {
- "minimum": 0,
- "type": "integer"
- },
- "type": "array"
- },
"FalsyAsTuple_HttpURL_": {
"items": {
"$ref": "#/$defs/HttpURL"
@@ -623,6 +616,37 @@
"title": "Logs",
"type": "object"
},
+ "MaxChildren": {
+ "additionalProperties": false,
+ "properties": {
+ "forum": {
+ "default": 0,
+ "minimum": 0,
+ "title": "Forum",
+ "type": "integer"
+ },
+ "forum_post": {
+ "default": 0,
+ "minimum": 0,
+ "title": "Forum Post",
+ "type": "integer"
+ },
+ "profile": {
+ "default": 0,
+ "minimum": 0,
+ "title": "Profile",
+ "type": "integer"
+ },
+ "album": {
+ "default": 0,
+ "minimum": 0,
+ "title": "Album",
+ "type": "integer"
+ }
+ },
+ "title": "MaxChildren",
+ "type": "object"
+ },
"Network": {
"additionalProperties": false,
"properties": {
@@ -1002,8 +1026,7 @@
"$ref": "#/$defs/Logs"
},
"max_children": {
- "$ref": "#/$defs/FalsyAsTuple_Annotated_int__Ge__",
- "default": []
+ "$ref": "#/$defs/MaxChildren"
},
"max_file_name_length": {
"default": 95,
diff --git a/cyberdrop_dl/config/settings.py b/cyberdrop_dl/config/settings.py
index 88706651b..76c2fcf38 100644
--- a/cyberdrop_dl/config/settings.py
+++ b/cyberdrop_dl/config/settings.py
@@ -6,7 +6,7 @@ from pathlib import Path
from typing import Annotated, ClassVar, Literal, override
from cyclopts import Parameter
-from pydantic import BaseModel, Field, PrivateAttr
+from pydantic import BaseModel, Field, NonNegativeInt, PrivateAttr
from pydantic.types import ByteSize, NonNegativeFloat, PositiveFloat, PositiveInt
from cyberdrop_dl.constants import LOGS_DATE_FORMAT, LOGS_DATETIME_FORMAT, CIStrEnum, HashMode
@@ -349,3 +349,14 @@ class UIOptions(ConfigGroup):
portrait: bool = False
"force CDL to run with a vertical layout"
refresh_rate: PositiveFloat = 10.0
+
+
+class MaxChildren(ConfigGroup, name=None):
+ forum: NonNegativeInt = 0
+ "Do not scrape more that this number of URLs inside a forum thread"
+ forum_post: NonNegativeInt = 0
+ "Do not scrape more that this number of URLs inside an individual a forum post"
+ profile: NonNegativeInt = 0
+ "Do not scrape more that this number of URLs in a profile"
+ album: NonNegativeInt = 0
+ "Do not scrape more that this number of URLs in a album"
diff --git a/cyberdrop_dl/crawlers/gofile.py b/cyberdrop_dl/crawlers/gofile.py
index 8c9a8f464..649777e92 100644
--- a/cyberdrop_dl/crawlers/gofile.py
+++ b/cyberdrop_dl/crawlers/gofile.py
@@ -260,9 +260,7 @@ def _check_node_is_accessible(node: Node) -> TypeGuard[File | Folder]:
def _has_single_not_nested_file(scrape_item: ScrapeItem, folder: Folder) -> bool:
return (
- folder["childrenCount"] == 1
- and folder["name"] == folder["code"]
- and scrape_item.type != ScrapeItemType.FILE_HOST_ALBUM
+ folder["childrenCount"] == 1 and folder["name"] == folder["code"] and scrape_item.type != ScrapeItemType.ALBUM
)
diff --git a/cyberdrop_dl/crawlers/toonily.py b/cyberdrop_dl/crawlers/toonily.py
index 75d08fdde..4bf32f28f 100644
--- a/cyberdrop_dl/crawlers/toonily.py
+++ b/cyberdrop_dl/crawlers/toonily.py
@@ -50,7 +50,7 @@ class ToonilyCrawler(Crawler):
*_, series_name, chapter_title = (css.text(bc) for bc in soup.select(Selector.NAV_BREADCUMBS))
- if scrape_item.type != ScrapeItemType.FILE_HOST_PROFILE:
+ if scrape_item.type != ScrapeItemType.PROFILE:
series_title = self.create_title(series_name)
scrape_item.append_folders(series_title)
diff --git a/cyberdrop_dl/scrape_mapper.py b/cyberdrop_dl/scrape_mapper.py
index 11a49747d..88825895c 100644
--- a/cyberdrop_dl/scrape_mapper.py
+++ b/cyberdrop_dl/scrape_mapper.py
@@ -16,7 +16,7 @@ from cyberdrop_dl.crawlers import ALLOW_NO_EXT, create_crawlers
from cyberdrop_dl.exceptions import JDownloaderError, NoExtensionError
from cyberdrop_dl.logs import log_spacer
from cyberdrop_dl.progress.scraping import ScrapingUI
-from cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem
+from cyberdrop_dl.url_objects import AbsoluteHttpURL, ScrapeItem, ScrapeItemType
from cyberdrop_dl.utils import remove_trailing_slash
if TYPE_CHECKING:
@@ -24,6 +24,7 @@ if TYPE_CHECKING:
from pathlib import Path
from cyberdrop_dl.clients.jdownloader import JDownloader
+ from cyberdrop_dl.config import Config
from cyberdrop_dl.config.crawlers import GenericCrawlers
from cyberdrop_dl.crawlers.crawler import Crawler
from cyberdrop_dl.crawlers.http_direct import DirectHttpFileCrawler
@@ -224,9 +225,9 @@ class ScrapeMapper:
self.create_download_task(wait_until_scrape_is_done())
- children_limits = tuple(self.manager.config.max_children)
+ max_children = _build_max_children_map(self.manager.config)
async for item in items:
- item.children_limits = children_limits
+ item.max_children = max_children
item.download_folder = self.manager.config.download_folder
if self._should_scrape(item):
stats.update(item)
@@ -516,3 +517,13 @@ def _best_match[T](current_map: dict[str, T], domain: str) -> T | None:
else:
current_map[domain] = found = current_map[best_match]
return found
+
+
+def _build_max_children_map(config: Config) -> dict[ScrapeItemType, int]:
+ max_children = config.max_children
+ return {
+ ScrapeItemType.FORUM: max_children.forum,
+ ScrapeItemType.FORUM_POST: max_children.forum_post,
+ ScrapeItemType.PROFILE: max_children.profile,
+ ScrapeItemType.ALBUM: max_children.album,
+ }
diff --git a/cyberdrop_dl/url_objects.py b/cyberdrop_dl/url_objects.py
index 5f47db049..f77dd6709 100644
--- a/cyberdrop_dl/url_objects.py
+++ b/cyberdrop_dl/url_objects.py
@@ -18,7 +18,7 @@ from cyberdrop_dl.exceptions import MaxChildrenError
from cyberdrop_dl.filepath import sanitize_folder
if TYPE_CHECKING:
- from collections.abc import Generator, Iterable, Sequence
+ from collections.abc import Generator, Iterable, Mapping, Sequence
@final
class AbsoluteHttpURL(yarl.URL):
@@ -113,8 +113,8 @@ def is_absolute_http_url(url: yarl.URL) -> TypeIs[AbsoluteHttpURL]:
class ScrapeItemType(IntEnum):
FORUM = 0
FORUM_POST = 1
- FILE_HOST_PROFILE = 2
- FILE_HOST_ALBUM = 3
+ PROFILE = 2
+ ALBUM = 3
logger = logging.getLogger(__name__)
@@ -199,7 +199,7 @@ class ScrapeItem:
folders: list[str] = dataclasses.field(default_factory=list)
parents: list[AbsoluteHttpURL] = dataclasses.field(default_factory=list)
parent_threads: set[AbsoluteHttpURL] = dataclasses.field(default_factory=set)
- children_limits: tuple[int, ...] = dataclasses.field(default_factory=tuple)
+ max_children: Mapping[ScrapeItemType, int] | None = None
password: str | None = None
_children_count: int = 0
@@ -241,11 +241,12 @@ class ScrapeItem:
def type(self, item_type: ScrapeItemType | None) -> None:
self._type = item_type
self._children_count = self._children_limit = 0
- if item_type is None:
+ if item_type is None or not self.max_children:
return
+
try:
- self._children_limit = self.children_limits[item_type]
- except (IndexError, TypeError):
+ self._children_limit = self.max_children[item_type]
+ except LookupError:
pass
@property
@@ -326,10 +327,10 @@ class ScrapeItem:
return self.create_new(url, part_of_album=True, add_parent=True)
def setup_as_album(self: ScrapeItem, title: str, *, album_id: str | None = None) -> None:
- return self.setup_as(title, ScrapeItemType.FILE_HOST_ALBUM, album_id=album_id)
+ return self.setup_as(title, ScrapeItemType.ALBUM, album_id=album_id)
def setup_as_profile(self: ScrapeItem, title: str, *, album_id: str | None = None) -> None:
- return self.setup_as(title, ScrapeItemType.FILE_HOST_PROFILE, album_id=album_id)
+ return self.setup_as(title, ScrapeItemType.PROFILE, album_id=album_id)
def setup_as_forum(self: ScrapeItem, title: str, *, album_id: str | None = None) -> None:
return self.setup_as(title, ScrapeItemType.FORUM, album_id=album_id)