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

183 行
6.3 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 JmProxySettings:
mode: str = "inherit"
proxy_url: str = ""
http_proxy: str = ""
https_proxy: str = ""
@classmethod
def from_config(cls, config: dict[str, Any]) -> "JmProxySettings":
mode = str(config.get("mode", "inherit")).strip().lower()
if mode not in {"inherit", "none", "system", "clash", "v2ray", "custom"}:
mode = "inherit"
return cls(
mode=mode,
proxy_url=str(config.get("proxy_url", "")).strip(),
http_proxy=str(config.get("http_proxy", "")).strip(),
https_proxy=str(config.get("https_proxy", "")).strip(),
)
def to_jmcomic_value(self) -> tuple[bool, str | dict[str, str] | None]:
if self.mode == "inherit":
return False, None
if self.mode == "none":
return True, None
if self.mode in {"system", "clash", "v2ray"}:
return True, self.mode
http_proxy = self.http_proxy or self.proxy_url
https_proxy = self.https_proxy or self.proxy_url
if http_proxy and https_proxy and http_proxy != https_proxy:
return True, {"http": http_proxy, "https": https_proxy}
proxy = http_proxy or https_proxy
if not proxy:
return False, None
return True, proxy
@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_proxy: JmProxySettings = JmProxySettings()
jm_avs_cookie: str = ""
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
jm_proxy_config = config.get("jm_proxy", {})
if not isinstance(jm_proxy_config, dict):
jm_proxy_config = {}
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_proxy=JmProxySettings.from_config(jm_proxy_config),
jm_avs_cookie=str(config.get("jm_avs_cookie", "")).strip(),
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