Skip to content

Bulk asynchronous downloader for multiple file hosts

[Feat] allow restrictions on thread depth (#545)

datawhores044b05934f98
diff --git a/.gitignore b/.gitignore
index 0bac3104c..64268757b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,7 @@
# Project stuff
/Test-Download-Dir/
/Test-AppData-Dir/
-/AppData/
+/AppData*/
/Downloads/
/Old Files/
*.log
diff --git a/cyberdrop_dl/config_definitions/config_settings.py b/cyberdrop_dl/config_definitions/config_settings.py
index 1525668e0..09d1abd30 100644
--- a/cyberdrop_dl/config_definitions/config_settings.py
+++ b/cyberdrop_dl/config_definitions/config_settings.py
@@ -27,6 +27,7 @@ class DownloadOptions(BaseModel):
skip_download_mark_completed: bool = False
skip_referer_seen_before: bool = False
maximum_number_of_children: list[NonNegativeInt] = []
+ maximum_thread_depth: NonNegativeInt = 0
@field_validator("maximum_number_of_children", mode="before")
@classmethod
diff --git a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
index f1e382a14..a41265668 100644
--- a/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
+++ b/cyberdrop_dl/scraper/crawlers/xenforo_crawler.py
@@ -125,6 +125,7 @@ class XenforoCrawler(Crawler):
self.attachment_url_parts = ["attachments", "data"]
self.attachment_url_hosts = ["smgmedia", "attachments.f95zone"]
self.logged_in = False
+ self.scraped_threads = set()
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
@@ -162,6 +163,10 @@ class XenforoCrawler(Crawler):
thread = self.get_thread_info(scrape_item.url)
title = None
last_post_url = thread.url
+ if thread.url in self.scraped_threads:
+ return
+ scrape_item.parent_threads.add(thread.url)
+ self.scraped_threads.add(thread.url)
async for soup in self.thread_pager(scrape_item):
if not title:
title_block = soup.select_one(self.selectors.title.element)
@@ -318,7 +323,7 @@ class XenforoCrawler(Crawler):
new_scrape_item = self.create_scrape_item(scrape_item, link)
if self.is_attachment(link):
return await self.handle_internal_link(new_scrape_item)
- if self.primary_base_domain.host in link.host: # type: ignore
+ if self.primary_base_domain.host in link.host and self.stop_thread_recursion(new_scrape_item): # type: ignore
origin = scrape_item.parents[0]
return log(f"Skipping nested thread URL {link} found on {origin}", 10)
new_scrape_item.set_type(None, self.manager)
@@ -345,6 +350,14 @@ class XenforoCrawler(Crawler):
new_link = self.parse_url(link_str)
return await self.get_absolute_link(new_link)
+ def stop_thread_recursion(self, scrape_item: ScrapeItem) -> bool:
+ max_thread_depth = self.manager.config_manager.settings_data.download_options.maximum_thread_depth
+ if not max_thread_depth:
+ return True
+ if len(scrape_item.parent_threads) > max_thread_depth:
+ return True
+ return False
+
def check_post_number(self, post_number: int, current_post_number: int) -> tuple[bool, bool]:
"""Checks if the program should scrape the current post.
diff --git a/cyberdrop_dl/utils/data_enums_classes/url_objects.py b/cyberdrop_dl/utils/data_enums_classes/url_objects.py
index b47137a2d..0839ba2ea 100644
--- a/cyberdrop_dl/utils/data_enums_classes/url_objects.py
+++ b/cyberdrop_dl/utils/data_enums_classes/url_objects.py
@@ -61,6 +61,7 @@ class MediaItem:
self.original_filename = self.original_filename or self.filename
self.parents = origin.parents.copy()
self.datetime = origin.possible_datetime
+ self.parents_thread = origin.parent_threads.copy()
@dataclass(kw_only=True, slots=True)
@@ -74,6 +75,7 @@ class ScrapeItem:
retry_path: Path | None = None
parents: list[URL] = field(default_factory=list, init=False)
+ parent_threads: set[URL] = field(default_factory=set, init=False)
children: int = field(default=0, init=False)
children_limit: int = field(default=0, init=False)
type: ScrapeItemType | None = field(default=None, init=False)
@@ -114,5 +116,6 @@ class ScrapeItem:
self.reset_childen()
if reset_parents:
self.parents = []
+ self.parent_threads = set()
if reset_parent_title:
self.parent_title = ""
diff --git a/docs/reference/cli-arguments.md b/docs/reference/cli-arguments.md
index ccc939c7a..cd214a74b 100644
--- a/docs/reference/cli-arguments.md
+++ b/docs/reference/cli-arguments.md
@@ -186,6 +186,7 @@ download_options:
--skip-download-mark-completed, --no-skip-download-mark-completed
--skip-referer-seen-before, --no-skip-referer-seen-before
--maximum-number-of-children [MAXIMUM_NUMBER_OF_CHILDREN ...]
+ --maximum-thread-depth MAX_THREAD_NESTING
dupe_cleanup_options:
--hashing HASHING
diff --git a/docs/reference/configuration-options/settings/download_options.md b/docs/reference/configuration-options/settings/download_options.md
index 7af2870fd..16fa22e16 100644
--- a/docs/reference/configuration-options/settings/download_options.md
+++ b/docs/reference/configuration-options/settings/download_options.md
@@ -182,3 +182,43 @@ No **FORUM** limit, no **FORUM_POST** limit, no **FILE_HOST_PROFILE** limit, max
```
{% endtab %}
{% endtabs %}
+
+
+## `maximum_thread_depth`
+
+| Type | Default |
+|----------------|----------|
+| `NonNegativeInt` | 0 |
+
+{% hint style="warning" %}
+It is not recommended to set this above the default value of 0, as there is a high chance of infinite nesting in certain cases.
+
+For example, when dealing with Megathreads, if a Megathread is linked to another Megathread, you could end up scraping an undesirable amount of data.
+{% endhint %}
+
+Restricts how many levels deep the scraper is allowed to go while scraping a thread
+
+Values
+0: No nesting allowed, only the top level thread is allowed
+None: unlimited parents
+1>: limits to the value given
+
+### Example
+Consider CDL finds the following sub-threads while scraping an input URL:
+\```
+└── thread_01
+ ├── thread_02
+ ├── thread_03
+ │ ├── thread_09
+ │ ├── thread_10
+ │ └── thread_11
+ ├── thread_04
+ ├── thread_05
+ ├── thread_06
+ ├── thread_07
+ │ └── thread_12
+ └── thread_08
+\```
+- With `maximum_thread_depth` = 0, CDL will only download files in `thread_01`, all the other threads will be ignored
+- With `maximum_thread_depth` = 1, CDL will only download files in `thread_01` to `thread_08`. `thread_09` to `thread_12` will be ignored
+- With `maximum_thread_depth` >= 2, CDL will download files from all the threads in this case