Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: add check for old config files
NTFSvolume566cbb11d3f4
diff --git a/cyberdrop_dl/__main__.py b/cyberdrop_dl/__main__.pyindex 2fa62339c..63c45c5e3 100644--- a/cyberdrop_dl/__main__.py+++ b/cyberdrop_dl/__main__.py@@ -44,7 +44,7 @@ def run_cdl(args: Sequence[str] | None = None) -> int:app.console.print(_error_panel(tb))except CDLConfigRuntimeErrorsGroup as exc_group:tb = tracebacks.from_exception(exc_group, chain_traceback=False)- app.console.print(_error_panel(tb, title="Invalid Config"))+ app.console.print(_error_panel(tb, title=exc_group.message or "Invalid Config"))else:return 0diff --git a/cyberdrop_dl/cli/__init__.py b/cyberdrop_dl/cli/__init__.pyindex ed3b9177f..7391d29d3 100644--- a/cyberdrop_dl/cli/__init__.py+++ b/cyberdrop_dl/cli/__init__.py@@ -36,13 +36,20 @@ app = App()+def show() -> None:+ """Show a list of all supported sites"""+ from cyberdrop_dl import supported_sites++ table = supported_sites.as_rich_table()+ app.console.print(table)++def register_commands() -> None:from cyberdrop_dl.cli.clean_up import app as cleanupfrom cyberdrop_dl.cli.database import app as databasefrom cyberdrop_dl.cli.download import downloadfrom cyberdrop_dl.cli.hash import compute_hashesfrom cyberdrop_dl.cli.report import report- from cyberdrop_dl.cli.show import showapp.default(download)app.command(database)diff --git a/cyberdrop_dl/cli/compat.py b/cyberdrop_dl/cli/compat.pynew file mode 100644index 000000000..7762fa0ba--- /dev/null+++ b/cyberdrop_dl/cli/compat.py@@ -0,0 +1,81 @@+from __future__ import annotations++import logging+import time+from pathlib import Path++from cyberdrop_dl.config.appdata import AppData+from cyberdrop_dl.exceptions import CDLConfigRuntimeErrorsGroup++logger = logging.getLogger(__name__)+++def _check_for_v9_database() -> None:+ db = Path("AppData/Cache/cyberdrop.db")+ default = AppData.default()+ if not db.exists():+ return++ msg = "\n".join(+ (+ "Found old database file at '%s'",+ "The default database location changed in v10",+ "Current default to '%s'",+ "",+ )+ )+ logger.warning(msg, db, default.db_file)++ from cyberdrop_dl.program_ui import _ask_confirmation++ if _ask_confirmation("Do you want to move the database to the new default location?"):+ _move_database(db, default.db_file)+ return++ logger.warning("Ignoring database file at '%s'...", db)+ time.sleep(3)+ return+++def _move_database(source: Path, dest: Path) -> None:+ if dest.exists():+ raise FileExistsError(f"A database file already exists at '{dest}'")++ import shutil++ _ = shutil.move(source, dest)+ logger.info("Database moved to '%s'", dest)+ time.sleep(3)+++def _check_for_v9_settings() -> None:+ default = AppData.default()+ settings = Path("AppData/Configs/Default/settings.yaml")+ if not settings.exists():+ return+ msg = "\n".join(+ (+ "Found old config file at '%s'",+ "The default config location changed in v10",+ "Current default: '%s'",+ "",+ ),+ )+ logger.warning(msg, settings, default.config_file)+ msg = "\n".join(+ (+ "",+ "The config file schema changed on v10. Automatic migration is not supported",+ "Please read version 10.0.0 changelog for details on how to migrate your config",+ "or delete the old config to use v10 default settings",+ )+ )+ raise RuntimeError(msg)+++def check_for_v9_files() -> None:+ try:+ _check_for_v9_database()+ _check_for_v9_settings()+ except (OSError, ValueError, RuntimeError) as e:+ raise CDLConfigRuntimeErrorsGroup("Migration Error", (e.with_traceback(None),)) from Nonediff --git a/cyberdrop_dl/cli/download.py b/cyberdrop_dl/cli/download.pyindex a96edaecb..c83f28eb5 100644--- a/cyberdrop_dl/cli/download.py+++ b/cyberdrop_dl/cli/download.py@@ -9,14 +9,16 @@ from cyclopts import Parameterfrom cyclopts.group import Groupfrom cyberdrop_dl.cli import CLIargs+from cyberdrop_dl.cli.compat import check_for_v9_filesfrom cyberdrop_dl.config import Config+from cyberdrop_dl.config.appdata import AppDatafrom cyberdrop_dl.exceptions import CDLConfigRuntimeErrorsGroupfrom cyberdrop_dl.logs import log_spacer, set_console_level, setup_file_loggingfrom cyberdrop_dl.models import merge_modelsfrom cyberdrop_dl.models.types import HttpURL # noqa: TC001from cyberdrop_dl.utils import cleanup-logger = logging.getLogger("cyberdrop_dl")+logger = logging.getLogger(__name__)if TYPE_CHECKING:from cyberdrop_dl.manager import Manager@@ -126,6 +128,7 @@ def download(cli: CLIargs | None = None,cli_overrides: Config | None = None,) -> None:+ check_for_v9_files()if input_file:input_file = input_file.resolve().absolute()@@ -139,8 +142,7 @@ def _prepare_appdata_and_config(urls: tuple[HttpURL, ...] = (),cli: CLIargs | None = None,cli_overrides: Config | None = None,-):- from cyberdrop_dl.config.appdata import AppData+) -> tuple[AppData, Config]:cli = cli or CLIargs()cli.links = urlsdiff --git a/cyberdrop_dl/cli/hash.py b/cyberdrop_dl/cli/hash.pyindex f2c4c910f..3998e6f63 100644--- a/cyberdrop_dl/cli/hash.py+++ b/cyberdrop_dl/cli/hash.py@@ -2,6 +2,8 @@ from typing import Literalfrom cyclopts.types import ExistingDirectory, ExistingFile+from cyberdrop_dl.cli.compat import check_for_v9_files+async def compute_hashes(folder: ExistingDirectory,@@ -11,7 +13,7 @@ async def compute_hashes(hashes: tuple[Literal["xxh128", "md5", "sha256"], ...] = ("xxh128", "md5", "sha256"),) -> None:"""Compute and save hashes of every file inside `folder` (recursively)"""-+ check_for_v9_files()from cyberdrop_dl import statsfrom cyberdrop_dl.config.appdata import AppDatafrom cyberdrop_dl.database import Databasediff --git a/cyberdrop_dl/cli/show.py b/cyberdrop_dl/cli/show.pydeleted file mode 100644index 88dd4ab8f..000000000--- a/cyberdrop_dl/cli/show.py+++ /dev/null@@ -1,9 +0,0 @@-from cyberdrop_dl.cli import app---def show() -> None:- """Show a list of all supported sites"""- from cyberdrop_dl import supported_sites-- table = supported_sites.as_rich_table()- app.console.print(table)diff --git a/cyberdrop_dl/database/_db.py b/cyberdrop_dl/database/_db.pyindex 494b43584..01063943d 100644--- a/cyberdrop_dl/database/_db.py+++ b/cyberdrop_dl/database/_db.py@@ -59,8 +59,8 @@ class Database:try:await self._create_tables()except Exception:+ await self.conn.close()if self.is_new:- await self.conn.close()try:await aio.unlink(self.path, missing_ok=True)except OSError:diff --git a/cyberdrop_dl/database/schema.py b/cyberdrop_dl/database/schema.pyindex 5e535655f..e45b7b1d0 100644--- a/cyberdrop_dl/database/schema.py+++ b/cyberdrop_dl/database/schema.py@@ -59,15 +59,20 @@ class SchemaTable(Table, name="schema_version"):await self.db_conn.commit()def check_version(self) -> None:+ error = Noneif self.version is None:- raise DatabaseError(f"Database has no schema information. Minimum required version: {REQUIRED_VERSION}")- if self.version < REQUIRED_VERSION:- raise DatabaseError(- f"Incompatible database version detected. Current: {self.version!s} , Minimum required: {REQUIRED_VERSION!s}"+ error = DatabaseError(f"Database has no schema information. Minimum required version: {REQUIRED_VERSION}")+ elif self.version < REQUIRED_VERSION:+ error = DatabaseError(+ f"Incompatible database version detected. Current: {self.version!s} - Required: {REQUIRED_VERSION!s}")- if self.version >= CURRENT_VERSION:+ elif self.version >= CURRENT_VERSION:self.up_to_date = True+ if error:+ error.add_note("Run 'cyberdrop-dl database transfer' to upgrade the database to a new version")+ raise error+async def update(self, version: Version = CURRENT_VERSION) -> None:query = "INSERT INTO schema_version (version) VALUES (?)"_ = await self.db_conn.execute(query, (str(version),))