Skip to content

Bulk asynchronous downloader for multiple file hosts

feat: add plugins support (#1554)

NTFSvolume8fdf491482ab
diff --git a/cyberdrop_dl/env.py b/cyberdrop_dl/env.py
index f58fa3d38..ef8846d5f 100644
--- a/cyberdrop_dl/env.py
+++ b/cyberdrop_dl/env.py
@@ -16,3 +16,4 @@ PROFILING = os.getenv("CDL_PROFILING")
PIXELDRAIN_PROXY = os.getenv("CDL_PIXELDRAIN_PROXY")
MAX_CRAWLER_ERRORS = int(os.getenv("CDL_MAX_CRAWLER_ERRORS") or 10)
DEBUG_VAR = RUNNING_IN_IDE or DEBUG_LOG_FOLDER or PROFILING
+NO_PLUGINS = bool(os.getenv("CDL_NO_PLUGINS"))
diff --git a/cyberdrop_dl/plugins.py b/cyberdrop_dl/plugins.py
new file mode 100644
index 000000000..b6b7b16b4
--- /dev/null
+++ b/cyberdrop_dl/plugins.py
@@ -0,0 +1,58 @@
+"""Loads external packages dinamically from their metadata
+
+Packages need to specify an entrypoint in their metadata as "cyberdrop_dl_plugins".
+
+The entrypoint will be executed after CDL's startup but before the scrape process begins.
+
+If the entrypoint returns anything other than `None`, it should be a callable that takes an instance of `Manager`
+
+NOTE: Since this works with packages' metadata, only valid packages published to pypi can be used as plugins and they need to be installed on the same env as CDL
+
+See: https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#using-package-metadata"""
+
+from __future__ import annotations
+
+import dataclasses
+from importlib.metadata import entry_points
+from typing import TYPE_CHECKING, Final
+
+from cyberdrop_dl import env
+from cyberdrop_dl.utils.logger import log
+
+if TYPE_CHECKING:
+ from collections.abc import Iterable
+ from importlib.metadata import EntryPoint
+
+ from cyberdrop_dl.managers.manager import Manager
+
+_GROUP_NAME: Final = "cyberdrop_dl_plugins"
+
+
[email protected](slots=True, frozen=True)
+class Plugin:
+ name: str
+ module: str
+ value: str
+ entrypoint: EntryPoint = dataclasses.field(repr=False)
+
+
+def _get_plugins() -> Iterable[Plugin]:
+ for entrypoint in entry_points(group=_GROUP_NAME):
+ yield Plugin(
+ entrypoint.name,
+ entrypoint.module,
+ entrypoint.value,
+ entrypoint,
+ )
+
+
+def load(manager: Manager) -> None:
+ for plugin in _get_plugins():
+ if env.NO_PLUGINS:
+ log(f"Found plugins installed but plugins are disabled. Ignored: {tuple(_get_plugins())}", 40)
+ return
+
+ result = plugin.entrypoint.load()
+ if callable(result):
+ result(manager)
+ log(f"Loaded plugin {plugin}", 20)
diff --git a/cyberdrop_dl/scraper/scrape_mapper.py b/cyberdrop_dl/scraper/scrape_mapper.py
index a1f167b6f..658e9c3dd 100644
--- a/cyberdrop_dl/scraper/scrape_mapper.py
+++ b/cyberdrop_dl/scraper/scrape_mapper.py
@@ -10,7 +10,6 @@ import aiofiles
from yarl import URL
from cyberdrop_dl.constants import REGEX_LINKS, BlockedDomains
-from cyberdrop_dl.crawlers import CRAWLERS
from cyberdrop_dl.crawlers._chevereto import CheveretoCrawler
from cyberdrop_dl.crawlers.crawler import Crawler, create_crawlers
from cyberdrop_dl.crawlers.discourse import DiscourseCrawler
@@ -71,12 +70,15 @@ class ScrapeMapper:
def start_scrapers(self) -> None:
"""Starts all scrapers."""
+ from cyberdrop_dl import plugins
+
self.existing_crawlers = get_crawlers_mapping(self.manager)
self.fallback_generic = GenericCrawler(self.manager)
generic_crawlers = create_generic_crawlers_by_config(self.global_settings.generic_crawlers_instances)
for crawler in generic_crawlers:
register_crawler(self.existing_crawlers, crawler(self.manager), from_user=True)
disable_crawlers_by_config(self.existing_crawlers, self.global_settings.general.disable_crawlers)
+ plugins.load(self.manager)
async def start_real_debrid(self) -> None:
"""Starts RealDebrid."""
@@ -366,6 +368,7 @@ def get_crawlers_mapping(manager: Manager | None = None, include_generics: bool
If manager is `None`, the `MOCK_MANAGER` will be used, which means the crawlers won't be able to actually run"""
+ from cyberdrop_dl.crawlers import CRAWLERS
from cyberdrop_dl.managers.mock_manager import MOCK_MANAGER
manager_ = manager or MOCK_MANAGER