from __future__ import annotations import asyncio import shutil 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 .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 DownloadError(Exception): pass 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("download finished but no image files were found") 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"download timed out after {self.settings.download_timeout_seconds}s" ) 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 "download failed") def cleanup(self, downloaded: DownloadedComic) -> None: if not self.settings.delete_work_dir_after_archive: return if downloaded.work_dir.exists(): shutil.rmtree(downloaded.work_dir) def _download_sync(self, comic_id: str, config_path: Path) -> str: try: import jmcomic except ImportError as exc: raise DownloadError("jmcomic is not installed; check plugin requirements.txt") from exc logger.info(f"JMDownload: downloading album {comic_id} with {config_path}") option = jmcomic.create_option_by_file(str(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 = { "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, }, }, } 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", )