2026-07-07 00:01:44 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
2026-07-07 00:13:26 +08:00
|
|
|
|
import asyncio
|
2026-07-07 00:01:44 +08:00
|
|
|
|
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
|
2026-07-07 01:35:42 +08:00
|
|
|
|
from .services.access import AccessControl, AccessDecision, EventIdentity
|
2026-07-07 00:13:26 +08:00
|
|
|
|
from .services.download import DownloadProgress, JMDownloadService
|
2026-07-07 00:31:54 +08:00
|
|
|
|
from .services.errors import JMDownloadError
|
2026-07-07 00:01:44 +08:00
|
|
|
|
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)
|
2026-07-07 01:35:42 +08:00
|
|
|
|
self.access_control = AccessControl(self.settings.access_control)
|
2026-07-07 00:13:26 +08:00
|
|
|
|
self.task_semaphore = asyncio.Semaphore(self.settings.max_concurrent_tasks)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
|
|
|
|
|
|
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."""
|
2026-07-07 01:35:42 +08:00
|
|
|
|
deny_message = self._deny_request(event, require_admin=True)
|
|
|
|
|
|
if deny_message is not None:
|
|
|
|
|
|
if deny_message:
|
|
|
|
|
|
yield event.plain_result(deny_message)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
comic_id = comic_id.strip()
|
|
|
|
|
|
if not comic_id.isdigit():
|
|
|
|
|
|
yield event.plain_result("Usage: /jm download <numeric_id>")
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2026-07-07 00:13:26 +08:00
|
|
|
|
await event.send(event.plain_result(f"开始下载 JM {comic_id},任务已进入队列。"))
|
2026-07-07 00:21:46 +08:00
|
|
|
|
file_send_message = ""
|
2026-07-07 00:01:44 +08:00
|
|
|
|
try:
|
2026-07-07 00:13:26 +08:00
|
|
|
|
async with self.task_semaphore:
|
|
|
|
|
|
await event.send(event.plain_result(f"开始执行 JM {comic_id} 下载任务。"))
|
|
|
|
|
|
downloaded = await self.downloader.download(
|
|
|
|
|
|
comic_id,
|
|
|
|
|
|
progress_callback=lambda progress: self._send_progress(event, progress),
|
|
|
|
|
|
)
|
|
|
|
|
|
archive_result = await self.archiver.archive(downloaded)
|
|
|
|
|
|
self.downloader.cleanup(downloaded)
|
|
|
|
|
|
scan_result = await self.komga.scan_library()
|
2026-07-07 00:21:46 +08:00
|
|
|
|
if self.settings.send_archive_file:
|
|
|
|
|
|
file_send_message = await self._send_archive_file(event, archive_result)
|
2026-07-07 00:31:54 +08:00
|
|
|
|
except JMDownloadError as exc:
|
|
|
|
|
|
logger.warning(f"JMDownload expected failure for {comic_id}: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"JMDownload 失败:{exc.user_message}")
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
2026-07-07 00:01:44 +08:00
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.exception(f"JMDownload failed for {comic_id}")
|
2026-07-07 00:31:54 +08:00
|
|
|
|
yield event.plain_result("JMDownload 发生未预期错误,请查看 AstrBot 日志。")
|
2026-07-07 00:01:44 +08:00
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2026-07-07 00:21:46 +08:00
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
self._format_success(archive_result, scan_result, file_send_message),
|
|
|
|
|
|
)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
event.stop_event()
|
|
|
|
|
|
|
|
|
|
|
|
@jm.command("status")
|
|
|
|
|
|
async def status(self, event: AstrMessageEvent):
|
|
|
|
|
|
"""Show current JMDownload archive and Komga settings."""
|
2026-07-07 01:35:42 +08:00
|
|
|
|
deny_message = self._deny_request(event, require_admin=False)
|
|
|
|
|
|
if deny_message is not None:
|
|
|
|
|
|
if deny_message:
|
|
|
|
|
|
yield event.plain_result(deny_message)
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
identity = EventIdentity.from_event(event)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
lines = [
|
2026-07-07 00:13:26 +08:00
|
|
|
|
"JMDownload 状态",
|
|
|
|
|
|
f"归档目录:{self.settings.archive_root}",
|
|
|
|
|
|
f"归档格式:{self.settings.archive_format}",
|
2026-07-07 00:28:06 +08:00
|
|
|
|
f"PDF 分块大小:{self.settings.pdf_chunk_size}",
|
2026-07-07 00:13:26 +08:00
|
|
|
|
f"并发任务数:{self.settings.max_concurrent_tasks}",
|
|
|
|
|
|
f"下载重试次数:{self.settings.download_retry_count}",
|
|
|
|
|
|
f"进度提示间隔:{self.settings.progress_interval_seconds} 秒",
|
2026-07-07 01:35:42 +08:00
|
|
|
|
f"访问控制:{'启用' if self.settings.access_control.enabled else '关闭'}",
|
|
|
|
|
|
f"需要白名单:{self.settings.access_control.require_whitelist}",
|
|
|
|
|
|
f"当前平台类型:{identity.platform_name or '未知'}",
|
|
|
|
|
|
f"当前平台 ID:{identity.platform_id or '未知'}",
|
|
|
|
|
|
f"当前机器人自身 ID:{identity.self_id or '未知'}",
|
|
|
|
|
|
f"当前会话类型:{identity.message_type or '未知'}",
|
|
|
|
|
|
f"当前群聊 ID:{identity.group_id or '无'}",
|
|
|
|
|
|
f"当前私聊/发送者 ID:{identity.sender_id or '未知'}",
|
|
|
|
|
|
f"当前 UMO:{identity.unified_msg_origin or '未知'}",
|
2026-07-07 00:55:08 +08:00
|
|
|
|
f"JM 代理模式:{self.settings.jm_proxy.mode}",
|
2026-07-07 01:05:32 +08:00
|
|
|
|
f"JM 账号登录:{'已启用' if self.settings.jm_login.can_login else '未配置'}",
|
2026-07-07 00:55:08 +08:00
|
|
|
|
f"JM 登录 Cookie:{'已配置' if self.settings.jm_avs_cookie else '未配置'}",
|
2026-07-07 00:13:26 +08:00
|
|
|
|
f"发送归档文件:{self.settings.send_archive_file}",
|
2026-07-07 00:35:15 +08:00
|
|
|
|
f"临时目录保留:{self.settings.cleanup_retention_days} 天",
|
2026-07-07 00:13:26 +08:00
|
|
|
|
f"Komga 自动扫描:{self.settings.komga.enabled}",
|
|
|
|
|
|
f"Komga 配置完整:{self.settings.komga.can_scan}",
|
2026-07-07 00:19:34 +08:00
|
|
|
|
f"Komga 地址:{self.settings.komga.base_url or '未配置'}",
|
|
|
|
|
|
f"Komga Library ID:{self.settings.komga.library_id or '未配置'}",
|
2026-07-07 00:01:44 +08:00
|
|
|
|
]
|
|
|
|
|
|
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."""
|
2026-07-07 01:35:42 +08:00
|
|
|
|
deny_message = self._deny_request(event, require_admin=True)
|
|
|
|
|
|
if deny_message is not None:
|
|
|
|
|
|
if deny_message:
|
|
|
|
|
|
yield event.plain_result(deny_message)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2026-07-07 00:19:34 +08:00
|
|
|
|
await event.send(event.plain_result("正在触发 Komga 扫描..."))
|
2026-07-07 00:01:44 +08:00
|
|
|
|
result = await self.komga.scan_library()
|
|
|
|
|
|
yield event.plain_result(result.message)
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
|
2026-07-07 00:35:15 +08:00
|
|
|
|
@jm.command("cleanup")
|
|
|
|
|
|
async def cleanup(self, event: AstrMessageEvent):
|
|
|
|
|
|
"""Cleanup old JMDownload work directories."""
|
2026-07-07 01:35:42 +08:00
|
|
|
|
deny_message = self._deny_request(event, require_admin=True)
|
|
|
|
|
|
if deny_message is not None:
|
|
|
|
|
|
if deny_message:
|
|
|
|
|
|
yield event.plain_result(deny_message)
|
2026-07-07 00:35:15 +08:00
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
removed = await asyncio.to_thread(self.downloader.cleanup_old_work_dirs)
|
|
|
|
|
|
except JMDownloadError as exc:
|
|
|
|
|
|
yield event.plain_result(f"清理失败:{exc.user_message}")
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
return
|
|
|
|
|
|
yield event.plain_result(f"清理完成:删除 {removed} 个临时工作目录。")
|
|
|
|
|
|
event.stop_event()
|
|
|
|
|
|
|
2026-07-07 01:35:42 +08:00
|
|
|
|
def _deny_request(
|
|
|
|
|
|
self,
|
|
|
|
|
|
event: AstrMessageEvent,
|
|
|
|
|
|
*,
|
|
|
|
|
|
require_admin: bool,
|
|
|
|
|
|
) -> str | None:
|
|
|
|
|
|
decision = self.access_control.check(event)
|
|
|
|
|
|
if not decision.allowed:
|
|
|
|
|
|
self._log_access_denied(decision)
|
|
|
|
|
|
return self.settings.access_control.deny_message
|
|
|
|
|
|
if require_admin and self.settings.admin_only and not event.is_admin():
|
|
|
|
|
|
return "JMDownload 当前配置仅允许 AstrBot 管理员执行此命令。"
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _log_access_denied(self, decision: AccessDecision) -> None:
|
|
|
|
|
|
identity = decision.identity
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
"JMDownload access denied: "
|
|
|
|
|
|
f"reason={decision.reason}, "
|
|
|
|
|
|
f"platform_id={identity.platform_id}, "
|
|
|
|
|
|
f"platform_name={identity.platform_name}, "
|
|
|
|
|
|
f"self_id={identity.self_id}, "
|
|
|
|
|
|
f"target_kind={identity.target_kind}, "
|
|
|
|
|
|
f"target_id={identity.target_id}, "
|
|
|
|
|
|
f"sender_id={identity.sender_id}, "
|
|
|
|
|
|
f"umo={identity.unified_msg_origin}",
|
|
|
|
|
|
)
|
2026-07-07 00:01:44 +08:00
|
|
|
|
|
|
|
|
|
|
def _format_success(
|
|
|
|
|
|
self,
|
|
|
|
|
|
archive_result: ArchiveResult,
|
|
|
|
|
|
scan_result: KomgaScanResult,
|
2026-07-07 00:21:46 +08:00
|
|
|
|
file_send_message: str = "",
|
2026-07-07 00:01:44 +08:00
|
|
|
|
) -> str:
|
2026-07-07 00:13:26 +08:00
|
|
|
|
existed = "复用已有归档" if archive_result.existed else "已创建归档"
|
2026-07-07 00:21:46 +08:00
|
|
|
|
lines = [
|
|
|
|
|
|
f"JMDownload 完成:{existed}",
|
|
|
|
|
|
f"系列:{archive_result.series_name}",
|
|
|
|
|
|
f"格式:{archive_result.archive_format}",
|
|
|
|
|
|
f"页数:{archive_result.page_count}",
|
|
|
|
|
|
f"文件:{archive_result.path}",
|
|
|
|
|
|
f"Komga: {scan_result.message}",
|
|
|
|
|
|
]
|
|
|
|
|
|
if file_send_message:
|
|
|
|
|
|
lines.append(file_send_message)
|
|
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
async def _send_archive_file(
|
|
|
|
|
|
self,
|
|
|
|
|
|
event: AstrMessageEvent,
|
|
|
|
|
|
archive_result: ArchiveResult,
|
|
|
|
|
|
) -> str:
|
|
|
|
|
|
if not archive_result.path.exists():
|
|
|
|
|
|
return "文件发送:归档文件不存在,已跳过发送。"
|
|
|
|
|
|
try:
|
|
|
|
|
|
await event.send(
|
|
|
|
|
|
event.chain_result(
|
|
|
|
|
|
[
|
|
|
|
|
|
Comp.File(
|
|
|
|
|
|
name=archive_result.path.name,
|
|
|
|
|
|
file=str(archive_result.path.resolve()),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
return "文件发送:已尝试发送归档文件。"
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"JMDownload: file send failed: {exc}")
|
|
|
|
|
|
return f"文件发送:平台发送失败,归档仍保存在本地。原因:{exc}"
|
2026-07-07 00:01:44 +08:00
|
|
|
|
|
2026-07-07 00:13:26 +08:00
|
|
|
|
async def _send_progress(
|
|
|
|
|
|
self,
|
|
|
|
|
|
event: AstrMessageEvent,
|
|
|
|
|
|
progress: DownloadProgress,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
await event.send(
|
|
|
|
|
|
event.plain_result(
|
|
|
|
|
|
"下载进度:"
|
|
|
|
|
|
f"JM {progress.comic_id} "
|
|
|
|
|
|
f"第 {progress.attempt}/{progress.max_attempts} 次尝试,"
|
|
|
|
|
|
f"已发现 {progress.image_count} 张图片"
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-07-07 00:01:44 +08:00
|
|
|
|
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()
|