from __future__ import annotations from pathlib import Path from astrbot.api import AstrBotConfig, logger from astrbot.api.event import AstrMessageEvent, filter import astrbot.api.message_components as Comp from astrbot.api.star import Context, Star, StarTools from .services.archive import ArchiveResult, ArchiveService from .services.download import JMDownloadService from .services.komga import KomgaClient, KomgaScanResult from .services.settings import PluginSettings class JMDownloadPlugin(Star): def __init__(self, context: Context, config: AstrBotConfig | None = None) -> None: super().__init__(context, config) data_dir = self._get_data_dir() self.config = config or {} self.settings = PluginSettings.from_config(dict(self.config), data_dir) self.settings.ensure_dirs() self.downloader = JMDownloadService(self.settings) self.archiver = ArchiveService(self.settings) self.komga = KomgaClient(self.settings.komga) async def initialize(self) -> None: logger.info( "JMDownload initialized: " f"archive_root={self.settings.archive_root}, " f"format={self.settings.archive_format}, " f"komga={self.settings.komga.enabled}", ) async def terminate(self) -> None: logger.info("JMDownload terminated") @filter.command_group("jm", alias={"JM", "Jm", "jM"}) def jm(self): pass @jm.command("download", alias={"下载"}) async def download(self, event: AstrMessageEvent, comic_id: str): """Download a JM album, archive it, and optionally ask Komga to scan.""" if self._deny_non_admin(event): yield event.plain_result("JMDownload is admin-only in the current config.") event.stop_event() return comic_id = comic_id.strip() if not comic_id.isdigit(): yield event.plain_result("Usage: /jm download ") event.stop_event() return await event.send(event.plain_result(f"Starting JM download: {comic_id}")) try: downloaded = await self.downloader.download(comic_id) archive_result = await self.archiver.archive(downloaded) self.downloader.cleanup(downloaded) scan_result = await self.komga.scan_library() except Exception as exc: logger.exception(f"JMDownload failed for {comic_id}") yield event.plain_result(f"JMDownload failed: {exc}") event.stop_event() return if self.settings.send_archive_file: yield event.chain_result( [ Comp.Plain(self._format_success(archive_result, scan_result)), Comp.File(name=archive_result.path.name, file=str(archive_result.path)), ], ) else: yield event.plain_result(self._format_success(archive_result, scan_result)) event.stop_event() @jm.command("status") async def status(self, event: AstrMessageEvent): """Show current JMDownload archive and Komga settings.""" lines = [ "JMDownload status", f"Archive root: {self.settings.archive_root}", f"Archive format: {self.settings.archive_format}", f"Send archive file: {self.settings.send_archive_file}", f"Komga enabled: {self.settings.komga.enabled}", f"Komga can scan: {self.settings.komga.can_scan}", ] yield event.plain_result("\n".join(lines)) event.stop_event() @jm.command("scan") async def scan(self, event: AstrMessageEvent): """Trigger a Komga scan for the configured library.""" if self._deny_non_admin(event): yield event.plain_result("JMDownload is admin-only in the current config.") event.stop_event() return result = await self.komga.scan_library() yield event.plain_result(result.message) event.stop_event() def _deny_non_admin(self, event: AstrMessageEvent) -> bool: return bool(self.settings.admin_only and not event.is_admin()) def _format_success( self, archive_result: ArchiveResult, scan_result: KomgaScanResult, ) -> str: existed = "existing archive reused" if archive_result.existed else "archive created" return "\n".join( [ f"JMDownload finished: {existed}", f"Series: {archive_result.series_name}", f"Pages: {archive_result.page_count}", f"File: {archive_result.path}", f"Komga: {scan_result.message}", ], ) def _get_data_dir(self) -> Path: try: return StarTools.get_data_dir("astrbot_plugin_jmdownload") except Exception: fallback = Path("data/plugin_data/astrbot_plugin_jmdownload") fallback.mkdir(parents=True, exist_ok=True) return fallback.resolve()