astrbot_plugin_jmdowloads/services/settings.py
2026-07-07 00:35:15 +08:00

138 行
4.6 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 = ""
scan_timeout_seconds: int = 30
@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(),
scan_timeout_seconds=max(
5,
_int_value(config.get("scan_timeout_seconds"), 30),
),
)
@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"
pdf_chunk_size: int = 80
admin_only: bool = True
send_archive_file: bool = False
overwrite_existing: bool = False
delete_work_dir_after_archive: bool = True
cleanup_retention_days: int = 7
download_timeout_seconds: int = 3600
download_retry_count: int = 2
progress_interval_seconds: int = 30
max_concurrent_tasks: int = 1
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,
pdf_chunk_size=max(1, _int_value(config.get("pdf_chunk_size"), 80)),
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),
),
cleanup_retention_days=max(
0,
_int_value(config.get("cleanup_retention_days"), 7),
),
download_timeout_seconds=max(
60,
_int_value(config.get("download_timeout_seconds"), 3600),
),
download_retry_count=max(
0,
_int_value(config.get("download_retry_count"), 2),
),
progress_interval_seconds=max(
0,
_int_value(config.get("progress_interval_seconds"), 30),
),
max_concurrent_tasks=max(
1,
_int_value(config.get("max_concurrent_tasks"), 1),
),
jm_batch_count=max(1, _int_value(config.get("jm_batch_count"), 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)
def _int_value(value: Any, default: int) -> int:
if value in (None, ""):
return default
try:
return int(value)
except (TypeError, ValueError):
return default