286 行
10 KiB
Python
286 行
10 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import shutil
|
|
import time
|
|
from collections.abc import Awaitable, Callable
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
from astrbot.api import logger
|
|
|
|
from .errors import DownloadError
|
|
from .images import collect_image_files, infer_album_title
|
|
from .settings import PluginSettings
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DownloadedComic:
|
|
comic_id: str
|
|
title: str
|
|
work_dir: Path
|
|
download_dir: Path
|
|
image_files: list[Path]
|
|
|
|
@property
|
|
def source_url(self) -> str:
|
|
return f"https://18comic.vip/album/{self.comic_id}"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DownloadProgress:
|
|
comic_id: str
|
|
attempt: int
|
|
max_attempts: int
|
|
image_count: int
|
|
message: str
|
|
|
|
|
|
ProgressCallback = Callable[[DownloadProgress], Awaitable[None]]
|
|
|
|
|
|
class JMDownloadService:
|
|
def __init__(self, settings: PluginSettings) -> None:
|
|
self.settings = settings
|
|
|
|
async def download(
|
|
self,
|
|
comic_id: str,
|
|
progress_callback: ProgressCallback | None = None,
|
|
) -> DownloadedComic:
|
|
job_name = datetime.now().strftime(f"{comic_id}-%Y%m%d-%H%M%S-%f")
|
|
work_dir = self.settings.work_root / job_name
|
|
max_attempts = self.settings.download_retry_count + 1
|
|
last_error = ""
|
|
|
|
for attempt in range(1, max_attempts + 1):
|
|
attempt_dir = work_dir / f"attempt-{attempt}"
|
|
download_dir = attempt_dir / "downloads"
|
|
config_path = attempt_dir / "jmcomic.yml"
|
|
download_dir.mkdir(parents=True, exist_ok=True)
|
|
self._write_jmcomic_config(config_path, download_dir)
|
|
|
|
progress_task = self._create_progress_task(
|
|
comic_id,
|
|
attempt,
|
|
max_attempts,
|
|
download_dir,
|
|
progress_callback,
|
|
)
|
|
try:
|
|
title = await asyncio.wait_for(
|
|
asyncio.to_thread(self._download_sync, comic_id, config_path),
|
|
timeout=self.settings.download_timeout_seconds,
|
|
)
|
|
image_files = collect_image_files(download_dir)
|
|
if not image_files:
|
|
raise DownloadError("下载完成但没有找到图片文件")
|
|
|
|
await self._emit_progress(
|
|
progress_callback,
|
|
DownloadProgress(
|
|
comic_id=comic_id,
|
|
attempt=attempt,
|
|
max_attempts=max_attempts,
|
|
image_count=len(image_files),
|
|
message="download completed",
|
|
),
|
|
)
|
|
return DownloadedComic(
|
|
comic_id=comic_id,
|
|
title=title or infer_album_title(download_dir, comic_id),
|
|
work_dir=work_dir,
|
|
download_dir=download_dir,
|
|
image_files=image_files,
|
|
)
|
|
except (TimeoutError, asyncio.TimeoutError) as exc:
|
|
last_error = (
|
|
f"下载超过 {self.settings.download_timeout_seconds} 秒仍未完成"
|
|
)
|
|
logger.warning(
|
|
f"JMDownload: {comic_id} attempt {attempt} timed out; "
|
|
"not retrying because the underlying jmcomic thread may still be running",
|
|
)
|
|
raise DownloadError(last_error) from exc
|
|
except Exception as exc:
|
|
last_error = str(exc)
|
|
logger.warning(
|
|
f"JMDownload: {comic_id} attempt {attempt}/{max_attempts} failed: {last_error}",
|
|
)
|
|
if attempt >= max_attempts:
|
|
raise DownloadError(last_error) from exc
|
|
finally:
|
|
if progress_task:
|
|
progress_task.cancel()
|
|
try:
|
|
await progress_task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
|
|
raise DownloadError(last_error or "下载失败")
|
|
|
|
def cleanup(self, downloaded: DownloadedComic) -> None:
|
|
if not self.settings.delete_work_dir_after_archive:
|
|
return
|
|
self._remove_work_dir(downloaded.work_dir)
|
|
|
|
def cleanup_old_work_dirs(self, retention_days: int | None = None) -> int:
|
|
retention_days = self.settings.cleanup_retention_days if retention_days is None else retention_days
|
|
if not self.settings.work_root.exists():
|
|
return 0
|
|
|
|
threshold = time.time() - retention_days * 86400
|
|
removed = 0
|
|
for child in self.settings.work_root.iterdir():
|
|
if not child.is_dir():
|
|
continue
|
|
if retention_days > 0 and child.stat().st_mtime >= threshold:
|
|
continue
|
|
self._remove_work_dir(child)
|
|
removed += 1
|
|
return removed
|
|
|
|
def _remove_work_dir(self, path: Path) -> None:
|
|
work_root = self.settings.work_root.resolve()
|
|
target = path.resolve()
|
|
if target == work_root or work_root not in target.parents:
|
|
raise DownloadError(f"拒绝清理工作目录之外的路径:{target}")
|
|
if target.exists():
|
|
shutil.rmtree(target)
|
|
|
|
def _download_sync(self, comic_id: str, config_path: Path) -> str:
|
|
try:
|
|
import jmcomic
|
|
except ImportError as exc:
|
|
raise DownloadError("缺少 jmcomic 依赖,请检查插件 requirements.txt") from exc
|
|
|
|
logger.info(f"JMDownload: downloading album {comic_id} with {config_path}")
|
|
try:
|
|
option = jmcomic.create_option_by_file(str(config_path))
|
|
finally:
|
|
self._remove_sensitive_config(config_path)
|
|
result = jmcomic.download_album(comic_id, option=option, check_exception=True)
|
|
return self._extract_album_title(result)
|
|
|
|
def _create_progress_task(
|
|
self,
|
|
comic_id: str,
|
|
attempt: int,
|
|
max_attempts: int,
|
|
download_dir: Path,
|
|
progress_callback: ProgressCallback | None,
|
|
) -> asyncio.Task | None:
|
|
if not progress_callback or self.settings.progress_interval_seconds <= 0:
|
|
return None
|
|
return asyncio.create_task(
|
|
self._report_progress(comic_id, attempt, max_attempts, download_dir, progress_callback),
|
|
)
|
|
|
|
async def _report_progress(
|
|
self,
|
|
comic_id: str,
|
|
attempt: int,
|
|
max_attempts: int,
|
|
download_dir: Path,
|
|
progress_callback: ProgressCallback,
|
|
) -> None:
|
|
try:
|
|
while True:
|
|
await asyncio.sleep(self.settings.progress_interval_seconds)
|
|
image_count = len(collect_image_files(download_dir))
|
|
message = (
|
|
"fetching album metadata"
|
|
if image_count == 0
|
|
else f"downloaded {image_count} images"
|
|
)
|
|
await self._emit_progress(
|
|
progress_callback,
|
|
DownloadProgress(
|
|
comic_id=comic_id,
|
|
attempt=attempt,
|
|
max_attempts=max_attempts,
|
|
image_count=image_count,
|
|
message=message,
|
|
),
|
|
)
|
|
except asyncio.CancelledError:
|
|
raise
|
|
except Exception as exc:
|
|
logger.warning(f"JMDownload: progress reporter failed: {exc}")
|
|
|
|
async def _emit_progress(
|
|
self,
|
|
progress_callback: ProgressCallback | None,
|
|
progress: DownloadProgress,
|
|
) -> None:
|
|
if not progress_callback:
|
|
return
|
|
try:
|
|
await progress_callback(progress)
|
|
except Exception as exc:
|
|
logger.warning(f"JMDownload: progress callback failed: {exc}")
|
|
|
|
def _extract_album_title(self, result: object) -> str:
|
|
album = getattr(result, "album", None)
|
|
if album is None and isinstance(result, tuple) and result:
|
|
album = result[0]
|
|
title = getattr(album, "title", "") if album is not None else ""
|
|
return str(title).strip()
|
|
|
|
def _write_jmcomic_config(self, config_path: Path, download_dir: Path) -> None:
|
|
config = self._build_jmcomic_config(download_dir)
|
|
config_path.parent.mkdir(parents=True, exist_ok=True)
|
|
config_path.write_text(
|
|
yaml.safe_dump(config, allow_unicode=True, sort_keys=False),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def _build_jmcomic_config(self, download_dir: Path) -> dict:
|
|
config = {
|
|
"version": "1.0",
|
|
"dir_rule": {
|
|
"base_dir": str(download_dir),
|
|
"rule": "Bd_Atitle_Pindex",
|
|
},
|
|
"client": {
|
|
"domain": self.settings.jm_domains,
|
|
},
|
|
"download": {
|
|
"cache": True,
|
|
"image": {
|
|
"decode": True,
|
|
"suffix": ".jpg",
|
|
},
|
|
"threading": {
|
|
"batch_count": self.settings.jm_batch_count,
|
|
},
|
|
},
|
|
}
|
|
|
|
meta_data = {}
|
|
should_write_proxy, proxy_value = self.settings.jm_proxy.to_jmcomic_value()
|
|
if should_write_proxy:
|
|
meta_data["proxies"] = proxy_value
|
|
if self.settings.jm_avs_cookie:
|
|
meta_data["cookies"] = {"AVS": self.settings.jm_avs_cookie}
|
|
if meta_data:
|
|
config["client"]["postman"] = {"meta_data": meta_data}
|
|
if self.settings.jm_login.can_login:
|
|
config["plugins"] = {
|
|
"after_init": [
|
|
self.settings.jm_login.to_jmcomic_plugin(),
|
|
],
|
|
}
|
|
|
|
return config
|
|
|
|
def _remove_sensitive_config(self, config_path: Path) -> None:
|
|
if not (self.settings.jm_avs_cookie or self.settings.jm_login.can_login):
|
|
return
|
|
try:
|
|
config_path.unlink(missing_ok=True)
|
|
except Exception as exc:
|
|
logger.warning(f"JMDownload: failed to remove sensitive config {config_path}: {exc}")
|