103 行
3.4 KiB
Python
103 行
3.4 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
from pathlib import Path
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
|
||
|
|
DEFAULT_JM_DOMAINS = [
|
||
|
|
"www.jmapiproxyxxx.vip",
|
||
|
|
"www.18comic-mygo.vip",
|
||
|
|
"18comic-MHWs.CC",
|
||
|
|
"18comic.vip",
|
||
|
|
"18comic.org",
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class KomgaSettings:
|
||
|
|
enabled: bool = False
|
||
|
|
base_url: str = ""
|
||
|
|
library_id: str = ""
|
||
|
|
username: str = ""
|
||
|
|
password: str = ""
|
||
|
|
api_key: str = ""
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def from_config(cls, config: dict[str, Any]) -> "KomgaSettings":
|
||
|
|
return cls(
|
||
|
|
enabled=bool(config.get("enabled", False)),
|
||
|
|
base_url=str(config.get("base_url", "")).strip().rstrip("/"),
|
||
|
|
library_id=str(config.get("library_id", "")).strip(),
|
||
|
|
username=str(config.get("username", "")).strip(),
|
||
|
|
password=str(config.get("password", "")),
|
||
|
|
api_key=str(config.get("api_key", "")).strip(),
|
||
|
|
)
|
||
|
|
|
||
|
|
@property
|
||
|
|
def can_scan(self) -> bool:
|
||
|
|
has_auth = bool(self.api_key or (self.username and self.password))
|
||
|
|
return bool(self.enabled and self.base_url and self.library_id and has_auth)
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class PluginSettings:
|
||
|
|
data_dir: Path
|
||
|
|
work_root: Path
|
||
|
|
archive_root: Path
|
||
|
|
archive_format: str = "cbz"
|
||
|
|
admin_only: bool = True
|
||
|
|
send_archive_file: bool = False
|
||
|
|
overwrite_existing: bool = False
|
||
|
|
delete_work_dir_after_archive: bool = True
|
||
|
|
download_timeout_seconds: int = 3600
|
||
|
|
jm_batch_count: int = 20
|
||
|
|
jm_domains: list[str] | None = None
|
||
|
|
komga: KomgaSettings = KomgaSettings()
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def from_config(cls, config: dict[str, Any], data_dir: Path) -> "PluginSettings":
|
||
|
|
archive_root_raw = str(config.get("archive_root", "")).strip()
|
||
|
|
archive_root = (
|
||
|
|
Path(archive_root_raw).expanduser()
|
||
|
|
if archive_root_raw
|
||
|
|
else data_dir / "komga_library"
|
||
|
|
)
|
||
|
|
|
||
|
|
archive_format = str(config.get("archive_format", "cbz")).strip().lower()
|
||
|
|
if archive_format not in {"cbz", "pdf"}:
|
||
|
|
archive_format = "cbz"
|
||
|
|
|
||
|
|
jm_domains = config.get("jm_domains", DEFAULT_JM_DOMAINS)
|
||
|
|
if not isinstance(jm_domains, list) or not jm_domains:
|
||
|
|
jm_domains = DEFAULT_JM_DOMAINS
|
||
|
|
|
||
|
|
komga_config = config.get("komga", {})
|
||
|
|
if not isinstance(komga_config, dict):
|
||
|
|
komga_config = {}
|
||
|
|
|
||
|
|
return cls(
|
||
|
|
data_dir=data_dir,
|
||
|
|
work_root=data_dir / "work",
|
||
|
|
archive_root=archive_root,
|
||
|
|
archive_format=archive_format,
|
||
|
|
admin_only=bool(config.get("admin_only", True)),
|
||
|
|
send_archive_file=bool(config.get("send_archive_file", False)),
|
||
|
|
overwrite_existing=bool(config.get("overwrite_existing", False)),
|
||
|
|
delete_work_dir_after_archive=bool(
|
||
|
|
config.get("delete_work_dir_after_archive", True),
|
||
|
|
),
|
||
|
|
download_timeout_seconds=max(
|
||
|
|
60,
|
||
|
|
int(config.get("download_timeout_seconds", 3600) or 3600),
|
||
|
|
),
|
||
|
|
jm_batch_count=max(1, int(config.get("jm_batch_count", 20) or 20)),
|
||
|
|
jm_domains=[str(domain).strip() for domain in jm_domains if str(domain).strip()],
|
||
|
|
komga=KomgaSettings.from_config(komga_config),
|
||
|
|
)
|
||
|
|
|
||
|
|
def ensure_dirs(self) -> None:
|
||
|
|
self.data_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
self.work_root.mkdir(parents=True, exist_ok=True)
|
||
|
|
self.archive_root.mkdir(parents=True, exist_ok=True)
|