Skip to content

Bulk asynchronous downloader for multiple file hosts

perf: more lazy imports

NTFSvolume198120cb78c9
diff --git a/cyberdrop_dl/cli/main.py b/cyberdrop_dl/cli/main.py
index deb3773c4..eda3e3be3 100644
--- a/cyberdrop_dl/cli/main.py
+++ b/cyberdrop_dl/cli/main.py
@@ -17,22 +17,20 @@ from cyberdrop_dl.exceptions import CDLConfigRuntimeErrorsGroup
from cyberdrop_dl.logs import log_spacer, set_console_level, setup_file_logging
from cyberdrop_dl.models import merge_models
from cyberdrop_dl.models.types import HttpURL # noqa: TC001
-from cyberdrop_dl.utils import cleanup
-logger = logging.getLogger(__name__)
_INTERACTIVE: ContextVar[bool] = ContextVar("_INTERACTIVE", default=False)
+logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from cyclopts.argument import ArgumentCollection
from cyberdrop_dl.manager import Manager
+ from cyberdrop_dl.utils import cleanup # noqa: TC004
async def _scrape(manager: Manager) -> None:
- from cyberdrop_dl import ffmpeg, webhook
+ from cyberdrop_dl import ffmpeg
from cyberdrop_dl.scrape_mapper import ScrapeMapper
- from cyberdrop_dl.updates import check_latest_pypi
- from cyberdrop_dl.utils import apprise
manager.log_config_settings()
if not ffmpeg.is_installed():
@@ -45,27 +43,14 @@ async def _scrape(manager: Manager) -> None:
async with ScrapeMapper(manager)() as scrape_mapper:
stats = await scrape_mapper.run()
- log_spacer()
await _post_runtime(manager)
-
stats_summary = manager.print_stats(stats)
-
- log_spacer()
- async with manager.http_client.create_aiohttp_session() as session:
- await check_latest_pypi(session)
- log_spacer()
- logger.info("Closing program...")
- logger.info("Finished downloading. Enjoy :)", extra={"color": "green"})
-
- if webhook_url := manager.config.notifications.webhook:
- await webhook.send_notification(webhook_url, stats_summary)
-
- if urls := manager.config.notifications.apprise:
- await apprise.send_notifications(urls, stats_summary)
+ await _check_for_updates(manager)
+ await _notify(manager.config, stats_summary)
async def _post_runtime(manager: Manager) -> None:
- """Actions to complete after main runtime, and before UI shutdown."""
+ log_spacer()
logger.info("Running Post-Download Processes\n", extra={"color": "green"})
if (
@@ -79,7 +64,31 @@ async def _post_runtime(manager: Manager) -> None:
if manager.config.sort.enabled:
await manager.sorter.run()
- _check_partials_and_empty_folders(manager.config)
+ _perform_cleanup(manager.config)
+
+
+async def _check_for_updates(manager: Manager) -> None:
+ from cyberdrop_dl.updates import check_latest_pypi
+
+ log_spacer()
+ async with manager.http_client.create_aiohttp_session() as session:
+ await check_latest_pypi(session)
+
+
+async def _notify(config: Config, stats_summary: str) -> None:
+ log_spacer()
+ logger.info("Closing program...")
+ logger.info("Finished downloading. Enjoy :)", extra={"color": "green"})
+
+ if webhook_url := config.notifications.webhook:
+ from cyberdrop_dl import webhook
+
+ await webhook.notify(webhook_url, stats_summary)
+
+ if urls := config.notifications.apprise:
+ from cyberdrop_dl.utils import apprise
+
+ await apprise.notify(urls, stats_summary)
def _show_interactive_ui(manager: Manager) -> None:
@@ -119,7 +128,7 @@ def _validate_inputs(args: ArgumentCollection) -> None:
raise
-inputs_group = Group(sort_key=-1, validator=_validate_inputs)
+_inputs_group = Group(sort_key=-1, validator=_validate_inputs)
def interactive(
@@ -143,7 +152,7 @@ def download(
urls: Annotated[
tuple[HttpURL, ...],
Parameter(
- group=inputs_group,
+ group=_inputs_group,
help="URL(s) to download",
),
] = (),
@@ -152,7 +161,7 @@ def download(
input_file: Annotated[
Path | None,
Parameter(
- group=inputs_group,
+ group=_inputs_group,
alias="-i",
help="Text/HTML file with URL(s) to download",
validator=cyclopts.validators.Path(exists=True, dir_okay=False),
@@ -207,7 +216,9 @@ def _check_ffmpeg(config: Config) -> None:
raise CDLConfigRuntimeErrorsGroup("Some config options are impossible to fulfill", errors)
-def _check_partials_and_empty_folders(config: Config) -> None:
+def _perform_cleanup(config: Config) -> None:
+ from cyberdrop_dl.utils import cleanup
+
logger.info("Checking for partial downloads...")
if cleanup.has_partial_files(config.download_folder):
logger.warning("There are partial downloads in the downloads folder")
diff --git a/cyberdrop_dl/utils/apprise.py b/cyberdrop_dl/utils/apprise.py
index 9e825fe3d..291eaf6a2 100644
--- a/cyberdrop_dl/utils/apprise.py
+++ b/cyberdrop_dl/utils/apprise.py
@@ -41,7 +41,7 @@ def read_apprise_urls(file: Path) -> tuple[str, ...]:
return ()
-async def send_notifications(urls: Sequence[AppriseURL], body: str) -> None:
+async def notify(urls: Sequence[AppriseURL], body: str) -> None:
if not urls:
return
diff --git a/cyberdrop_dl/webhook.py b/cyberdrop_dl/webhook.py
index aa8bc1179..8dab6c31f 100644
--- a/cyberdrop_dl/webhook.py
+++ b/cyberdrop_dl/webhook.py
@@ -18,7 +18,7 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
-async def send_notification(webhook: AppriseURL, body: str) -> None:
+async def notify(webhook: AppriseURL, body: str) -> None:
log_spacer()
url, form = await _prepare(webhook)
form.add_field("content", body)
diff --git a/tests/test_webhook.py b/tests/test_webhook.py
index c84060027..8e5438bb3 100644
--- a/tests/test_webhook.py
+++ b/tests/test_webhook.py
@@ -7,7 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
from cyberdrop_dl.models import AppriseURL
-from cyberdrop_dl.webhook import send_notification
+from cyberdrop_dl.webhook import notify
if TYPE_CHECKING:
from collections.abc import Generator
@@ -31,7 +31,7 @@ async def test_send_webhook_success(caplog: pytest.LogCaptureFixture) -> None:
with _mock_aiohttp_request(mock_response):
with caplog.at_level(10):
- await send_notification(webhook, "test")
+ await notify(webhook, "test")
assert "Webhook notifications: Success" in caplog.text
@@ -44,7 +44,7 @@ async def test_send_webhook_failure_with_json_error(caplog: pytest.LogCaptureFix
with _mock_aiohttp_request(mock_response):
with caplog.at_level(10):
- await send_notification(webhook, "test")
+ await notify(webhook, "test")
assert "Webhook notification failed:" in caplog.text
assert "Bad Request" in caplog.text
@@ -66,6 +66,6 @@ async def test_send_webhook_failure_with_non_json_error(caplog: pytest.LogCaptur
with _mock_aiohttp_request(mock_response):
with caplog.at_level(10):
- await send_notification(webhook, "test")
+ await notify(webhook, "test")
assert "ClientResponseError" in caplog.text