2026-07-20 22:13:04 +08:00
|
|
|
|
"""AstrBot MoviePilot integration plugin."""
|
|
|
|
|
|
|
2026-07-20 22:21:29 +08:00
|
|
|
|
from astrbot.api import AstrBotConfig, logger, star
|
2026-07-20 22:13:04 +08:00
|
|
|
|
from astrbot.api.event import AstrMessageEvent, filter
|
|
|
|
|
|
|
2026-07-20 22:54:31 +08:00
|
|
|
|
try:
|
|
|
|
|
|
from .access_policy import AccessPolicy
|
2026-07-20 23:32:48 +08:00
|
|
|
|
from .media_service import MediaService, format_media_result_item, format_resource_results, media_poster_url
|
2026-07-20 22:54:31 +08:00
|
|
|
|
from .session_context import SessionContextStore
|
|
|
|
|
|
from .confirmation import ConfirmationStore
|
2026-07-22 23:08:10 +08:00
|
|
|
|
from .subscription_service import SubscriptionService, format_subscriptions, media_id_from_subscription
|
2026-07-20 22:54:31 +08:00
|
|
|
|
from .download_service import DownloadService, format_download_tasks
|
2026-07-22 23:25:14 +08:00
|
|
|
|
from .notification_service import NotificationPoller, format_notification
|
2026-07-20 22:54:31 +08:00
|
|
|
|
except ImportError:
|
|
|
|
|
|
from access_policy import AccessPolicy
|
2026-07-20 23:32:48 +08:00
|
|
|
|
from media_service import MediaService, format_media_result_item, format_resource_results, media_poster_url
|
2026-07-20 22:54:31 +08:00
|
|
|
|
from session_context import SessionContextStore
|
|
|
|
|
|
from confirmation import ConfirmationStore
|
2026-07-22 23:08:10 +08:00
|
|
|
|
from subscription_service import SubscriptionService, format_subscriptions, media_id_from_subscription
|
2026-07-20 22:54:31 +08:00
|
|
|
|
from download_service import DownloadService, format_download_tasks
|
2026-07-22 23:25:14 +08:00
|
|
|
|
from notification_service import NotificationPoller, format_notification
|
2026-07-20 22:28:19 +08:00
|
|
|
|
from astrbot.api.event import MessageChain
|
2026-07-20 23:32:48 +08:00
|
|
|
|
from astrbot.api.message_components import Image, Plain
|
2026-07-20 22:17:04 +08:00
|
|
|
|
|
2026-07-20 22:13:04 +08:00
|
|
|
|
|
2026-07-22 23:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
def format_moviepilot_error(exc: Exception) -> str:
|
|
|
|
|
|
"""Convert transport/API failures into actionable, secret-safe text."""
|
|
|
|
|
|
status = getattr(exc, "status_code", None)
|
|
|
|
|
|
if status == 404:
|
|
|
|
|
|
return "\u63a5\u53e3\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5 MoviePilot \u7248\u672c\u6216\u63d2\u4ef6\u517c\u5bb9\u914d\u7f6e\u3002"
|
|
|
|
|
|
if status == 429:
|
|
|
|
|
|
return "\u8bf7\u6c42\u8fc7\u4e8e\u9891\u7e41\uff0c\u63d2\u4ef6\u5df2\u81ea\u52a8\u91cd\u8bd5\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\u3002"
|
|
|
|
|
|
text = str(exc)
|
|
|
|
|
|
if "\u8d85\u65f6" in text:
|
|
|
|
|
|
return "\u8fde\u63a5\u8d85\u65f6\uff0c\u8bf7\u68c0\u67e5\u5bb9\u5668\u7f51\u7edc\u548c MoviePilot \u662f\u5426\u5728\u7ebf\u3002"
|
|
|
|
|
|
if "Token" in text or "token" in text.lower():
|
|
|
|
|
|
return "\u9274\u6743\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5 MoviePilot API Token\u3002"
|
|
|
|
|
|
return text or "\u672a\u77e5\u9519\u8bef"
|
|
|
|
|
|
|
2026-07-20 23:32:48 +08:00
|
|
|
|
def _media_result_chain(results: list[dict[str, object]]) -> list[object]:
|
|
|
|
|
|
if not results:
|
|
|
|
|
|
return [Plain("没有找到匹配的影视信息。")]
|
|
|
|
|
|
chain: list[object] = [Plain("影视搜索结果:\n")]
|
|
|
|
|
|
for index, item in enumerate(results, start=1):
|
|
|
|
|
|
chain.append(Plain(format_media_result_item(index, item) + "\n"))
|
|
|
|
|
|
poster_url = media_poster_url(item)
|
|
|
|
|
|
if poster_url:
|
|
|
|
|
|
chain.append(Image.fromURL(poster_url))
|
|
|
|
|
|
chain.append(Plain("\n"))
|
|
|
|
|
|
chain.append(Plain("可继续使用:/mp resources <编号> 或 /mp subscribe <编号>"))
|
|
|
|
|
|
return chain
|
|
|
|
|
|
|
2026-07-20 22:13:04 +08:00
|
|
|
|
@star.register(
|
|
|
|
|
|
"astrbot_plugin_moviepilot",
|
|
|
|
|
|
"AstrBot MoviePilot Contributors",
|
|
|
|
|
|
"在 AstrBot 中通过命令和 LLM 控制 MoviePilot。",
|
2026-07-22 23:27:29 +08:00
|
|
|
|
"0.19.1",
|
2026-07-20 22:13:04 +08:00
|
|
|
|
)
|
|
|
|
|
|
class MoviePilotPlugin(star.Star):
|
|
|
|
|
|
"""MoviePilot integration entrypoint."""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, context: star.Context, config: AstrBotConfig) -> None:
|
|
|
|
|
|
super().__init__(context)
|
|
|
|
|
|
self.config = config
|
2026-07-20 22:17:04 +08:00
|
|
|
|
self.access_policy = AccessPolicy(config)
|
2026-07-20 22:21:29 +08:00
|
|
|
|
self.session_store = SessionContextStore(
|
|
|
|
|
|
star.StarTools.get_data_dir() / "sessions.json",
|
|
|
|
|
|
ttl=int(config.get("search_cache_ttl", 600)),
|
|
|
|
|
|
)
|
|
|
|
|
|
self.media_service = MediaService(config, self.session_store)
|
2026-07-20 22:24:07 +08:00
|
|
|
|
self.subscription_service = SubscriptionService(config)
|
2026-07-20 22:25:50 +08:00
|
|
|
|
self.download_service = DownloadService(config)
|
2026-07-20 22:28:19 +08:00
|
|
|
|
self.notification_poller = NotificationPoller(
|
|
|
|
|
|
config,
|
|
|
|
|
|
star.StarTools.get_data_dir() / "notification_cursor.json",
|
|
|
|
|
|
self._send_notification,
|
|
|
|
|
|
)
|
|
|
|
|
|
if config.get("enable_notification_polling", True) and config.get(
|
|
|
|
|
|
"moviepilot_url"
|
|
|
|
|
|
) and config.get("moviepilot_api_token"):
|
|
|
|
|
|
self.notification_poller.start()
|
2026-07-20 22:24:07 +08:00
|
|
|
|
self.confirmations = ConfirmationStore()
|
2026-07-20 22:13:04 +08:00
|
|
|
|
logger.info("MoviePilot 插件已加载")
|
|
|
|
|
|
|
2026-07-20 22:21:29 +08:00
|
|
|
|
@filter.command_group("mp")
|
|
|
|
|
|
def moviepilot(self) -> None:
|
|
|
|
|
|
"""MoviePilot 命令组。"""
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("help", alias={"帮助"})
|
2026-07-20 22:13:04 +08:00
|
|
|
|
async def moviepilot_help(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""显示 MoviePilot 插件命令入口。"""
|
2026-07-20 22:17:04 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
2026-07-20 22:13:04 +08:00
|
|
|
|
yield event.plain_result(
|
2026-07-20 22:21:29 +08:00
|
|
|
|
"MoviePilot 命令:/mp search、/mp detail、/mp seasons、/mp resources、"
|
|
|
|
|
|
"/mp subscribe、/mp download。"
|
2026-07-20 22:13:04 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-07-22 23:25:14 +08:00
|
|
|
|
@moviepilot.command("test", alias={"\u6d4b\u8bd5"})
|
2026-07-20 22:34:05 +08:00
|
|
|
|
async def test_connection(self, event: AstrMessageEvent) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u6d4b\u8bd5 MoviePilot \u8fde\u63a5\u3001\u9274\u6743\u548c\u8131\u654f\u914d\u7f6e\u3002"""
|
2026-07-20 22:34:05 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
client = self.media_service._get_client()
|
|
|
|
|
|
await client.test_connection()
|
|
|
|
|
|
diagnostics = client.diagnostics()
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
"MoviePilot \u8fde\u63a5\u6210\u529f\uff0cAPI Token \u6709\u6548\u3002\n"
|
|
|
|
|
|
f"\u5730\u5740\uff1a{diagnostics['base_url']}\n"
|
|
|
|
|
|
f"\u9274\u6743\uff1a{diagnostics['token']}\n"
|
|
|
|
|
|
f"\u8d85\u65f6\uff1a{diagnostics['timeout_seconds']} \u79d2\uff0c\u91cd\u8bd5\uff1a{diagnostics['retries']} \u6b21"
|
|
|
|
|
|
)
|
2026-07-20 22:34:05 +08:00
|
|
|
|
except Exception as exc:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"MoviePilot \u8fde\u63a5\u5931\u8d25\uff1a{format_moviepilot_error(exc)}")
|
2026-07-20 22:34:05 +08:00
|
|
|
|
|
2026-07-20 22:21:29 +08:00
|
|
|
|
@moviepilot.command("search", alias={"搜索"})
|
|
|
|
|
|
async def search_media(self, event: AstrMessageEvent, title: str) -> None:
|
|
|
|
|
|
"""搜索影视信息。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
results = await self.media_service.search_media(event.unified_msg_origin, title)
|
2026-07-20 23:32:48 +08:00
|
|
|
|
yield event.chain_result(_media_result_chain(results))
|
2026-07-20 22:21:29 +08:00
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 媒体搜索失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"媒体搜索失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("detail", alias={"详情"})
|
|
|
|
|
|
async def media_detail(self, event: AstrMessageEvent, media_id: str) -> None:
|
|
|
|
|
|
"""查询媒体详情。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, int(media_id))
|
|
|
|
|
|
detail = await self.media_service.media_detail(
|
|
|
|
|
|
media_id if media is None else MediaService._media_id(media)
|
|
|
|
|
|
)
|
|
|
|
|
|
title = detail.get("title") or detail.get("name") or "未命名"
|
|
|
|
|
|
overview = detail.get("description") or detail.get("overview") or "暂无简介"
|
|
|
|
|
|
yield event.plain_result(f"{title}\n{overview}")
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 媒体详情查询失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"媒体详情查询失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("resources", alias={"资源"})
|
|
|
|
|
|
async def search_resources(self, event: AstrMessageEvent, media_index: int) -> None:
|
|
|
|
|
|
"""搜索指定媒体的资源。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, media_index)
|
|
|
|
|
|
if media is None:
|
|
|
|
|
|
yield event.plain_result("媒体编号无效或已过期,请重新使用 /mp search。")
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
results = await self.media_service.search_resources(
|
|
|
|
|
|
event.unified_msg_origin, media
|
|
|
|
|
|
)
|
|
|
|
|
|
yield event.plain_result(format_resource_results(results))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 资源搜索失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"资源搜索失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("seasons", alias={"季"})
|
|
|
|
|
|
async def media_seasons(self, event: AstrMessageEvent, media_id: str) -> None:
|
|
|
|
|
|
"""查询媒体季信息。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, int(media_id))
|
|
|
|
|
|
target_id = media_id if media is None else MediaService._media_id(media)
|
|
|
|
|
|
try:
|
|
|
|
|
|
seasons = await self.media_service.media_seasons(media_id=target_id)
|
|
|
|
|
|
if not seasons:
|
|
|
|
|
|
yield event.plain_result("没有找到季信息。")
|
|
|
|
|
|
return
|
|
|
|
|
|
lines = ["季信息:"]
|
|
|
|
|
|
for item in seasons:
|
|
|
|
|
|
number = item.get("season_number") or item.get("season") or "?"
|
|
|
|
|
|
name = item.get("name") or item.get("title") or ""
|
|
|
|
|
|
lines.append(f"第 {number} 季 {name}".strip())
|
|
|
|
|
|
yield event.plain_result("\n".join(lines))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 季信息查询失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"季信息查询失败:{exc}")
|
|
|
|
|
|
|
2026-07-20 22:24:07 +08:00
|
|
|
|
@moviepilot.command("subscribe", alias={"订阅"})
|
2026-07-22 23:08:10 +08:00
|
|
|
|
async def subscribe_media(self, event: AstrMessageEvent, media_index: int, season: int = 0) -> None:
|
|
|
|
|
|
"""创建订阅确认请求。"""
|
2026-07-20 22:24:07 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, media_index)
|
|
|
|
|
|
if media is None:
|
|
|
|
|
|
yield event.plain_result("媒体编号无效或已过期,请重新使用 /mp search。")
|
|
|
|
|
|
return
|
2026-07-22 23:08:10 +08:00
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="subscribe",
|
|
|
|
|
|
target={"media": media, "season": season or None},
|
|
|
|
|
|
)
|
|
|
|
|
|
title = media.get("title") or media.get("name") or "未命名"
|
|
|
|
|
|
season_text = f",第 {season} 季" if season else ""
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备订阅《{title}》{season_text}。\n"
|
|
|
|
|
|
f"请在 {self.confirmations.ttl} 秒内回复 /mp confirm {request.code} 确认。"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("sub-search", alias={"订阅搜索"})
|
|
|
|
|
|
async def search_subscription(self, event: AstrMessageEvent, subscribe_id: int) -> None:
|
|
|
|
|
|
"""手动触发指定订阅搜索。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
result = await self.subscription_service.trigger_subscription_search(subscribe_id)
|
|
|
|
|
|
yield event.plain_result(f"已触发订阅 {subscribe_id} 搜索。{result or ''}".strip())
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
yield event.plain_result(f"触发订阅搜索失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("unsubscribe", alias={"取消订阅"})
|
|
|
|
|
|
async def unsubscribe(self, event: AstrMessageEvent, target: str) -> None:
|
|
|
|
|
|
"""创建取消订阅确认请求。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
target = str(target).strip()
|
|
|
|
|
|
if not target:
|
|
|
|
|
|
yield event.plain_result("用法:/mp unsubscribe <订阅ID或媒体ID>")
|
|
|
|
|
|
return
|
2026-07-20 22:24:07 +08:00
|
|
|
|
try:
|
2026-07-22 23:08:10 +08:00
|
|
|
|
subscriptions = await self.subscription_service.list_subscriptions()
|
|
|
|
|
|
matched = next((item for item in subscriptions if str(item.get("id")) == target), None)
|
|
|
|
|
|
media_id = media_id_from_subscription(matched) if matched else target
|
|
|
|
|
|
if not media_id:
|
|
|
|
|
|
yield event.plain_result("找不到对应订阅或媒体 ID。")
|
2026-07-20 22:24:07 +08:00
|
|
|
|
return
|
2026-07-22 23:08:10 +08:00
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="unsubscribe",
|
|
|
|
|
|
target={"media_id": media_id, "season": matched.get("season") if matched else None},
|
|
|
|
|
|
)
|
|
|
|
|
|
title = matched.get("name") if matched else media_id
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备取消订阅《{title}》。请在 {self.confirmations.ttl} 秒内回复 "
|
|
|
|
|
|
f"/mp confirm {request.code} 确认。"
|
|
|
|
|
|
)
|
2026-07-20 22:24:07 +08:00
|
|
|
|
except Exception as exc:
|
2026-07-22 23:08:10 +08:00
|
|
|
|
yield event.plain_result(f"查询订阅失败:{exc}")
|
2026-07-20 22:24:07 +08:00
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("subscriptions", alias={"订阅列表"})
|
|
|
|
|
|
async def list_subscriptions(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""查询订阅列表。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
subscriptions = await self.subscription_service.list_subscriptions()
|
|
|
|
|
|
yield event.plain_result(format_subscriptions(subscriptions))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 查询订阅失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"查询订阅失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("download", alias={"下载"})
|
2026-07-20 22:25:50 +08:00
|
|
|
|
async def download_resource(self, event: AstrMessageEvent, target: str) -> None:
|
|
|
|
|
|
"""创建立即下载确认请求或控制已有下载任务。"""
|
2026-07-20 22:24:07 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
2026-07-20 22:25:50 +08:00
|
|
|
|
parts = target.split(maxsplit=1)
|
|
|
|
|
|
if parts and parts[0] in {"start", "开始", "stop", "暂停", "delete", "删除"}:
|
|
|
|
|
|
if len(parts) != 2 or not parts[1].strip():
|
|
|
|
|
|
yield event.plain_result("用法:/mp download start|stop|delete <任务ID>")
|
|
|
|
|
|
return
|
|
|
|
|
|
action, task_id = parts[0], parts[1].strip()
|
|
|
|
|
|
if action in {"delete", "删除"}:
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="delete_download",
|
|
|
|
|
|
target={"task_id": task_id},
|
|
|
|
|
|
)
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备删除下载任务 {task_id}。请回复:/mp confirm {request.code}"
|
|
|
|
|
|
)
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
result = (
|
|
|
|
|
|
await self.download_service.start(task_id)
|
|
|
|
|
|
if action in {"start", "开始"}
|
|
|
|
|
|
else await self.download_service.stop(task_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
state = "开始" if action in {"start", "开始"} else "暂停"
|
|
|
|
|
|
yield event.plain_result(f"已发送{state}任务请求:{task_id}\n{result or '操作成功'}")
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 下载任务控制失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"下载任务操作失败:{exc}")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
resource_index = int(target)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
yield event.plain_result("用法:/mp download <资源编号>,或 /mp download start|stop|delete <任务ID>")
|
|
|
|
|
|
return
|
2026-07-20 22:24:07 +08:00
|
|
|
|
origin = event.unified_msg_origin
|
|
|
|
|
|
resource = self.session_store.get_resource_result(origin, resource_index)
|
|
|
|
|
|
media = self.session_store.get_resource_media(origin)
|
|
|
|
|
|
if resource is None or media is None:
|
|
|
|
|
|
yield event.plain_result("资源编号无效或已过期,请重新使用 /mp resources。")
|
|
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="download",
|
|
|
|
|
|
target={"resource": resource, "media": media},
|
|
|
|
|
|
)
|
|
|
|
|
|
title = resource.get("title") or resource.get("name") or "未命名资源"
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备下载:{title}\n请在 {self.confirmations.ttl} 秒内回复:/mp confirm {request.code}"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@moviepilot.command("confirm", alias={"确认"})
|
|
|
|
|
|
async def confirm_operation(self, event: AstrMessageEvent, code: str) -> None:
|
|
|
|
|
|
"""确认并执行副作用操作。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.consume(
|
|
|
|
|
|
code,
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
)
|
|
|
|
|
|
if request is None:
|
|
|
|
|
|
yield event.plain_result("确认码无效、已过期,或不属于当前会话。")
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
if request.operation == "download":
|
|
|
|
|
|
result = await self.subscription_service.download_resource(
|
|
|
|
|
|
request.target["resource"], request.target["media"]
|
|
|
|
|
|
)
|
|
|
|
|
|
download_id = result.get("download_id") or result.get("id") or "未知"
|
|
|
|
|
|
yield event.plain_result(f"下载任务已提交,任务 ID:{download_id}")
|
|
|
|
|
|
return
|
2026-07-20 22:31:36 +08:00
|
|
|
|
if request.operation == "subscribe":
|
|
|
|
|
|
media = request.target["media"]
|
|
|
|
|
|
result = await self.subscription_service.create_subscription(
|
|
|
|
|
|
media, season=request.target.get("season")
|
|
|
|
|
|
)
|
|
|
|
|
|
subscribe_id = result.get("id")
|
|
|
|
|
|
if not subscribe_id:
|
|
|
|
|
|
yield event.plain_result("订阅创建失败,MoviePilot 没有返回订阅编号。")
|
|
|
|
|
|
return
|
|
|
|
|
|
message = f"订阅已创建,ID:{subscribe_id}"
|
|
|
|
|
|
try:
|
|
|
|
|
|
await self.subscription_service.trigger_subscription_search(
|
|
|
|
|
|
int(subscribe_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
message += ";已自动触发一次资源搜索。"
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
message += f";自动搜索失败:{exc}"
|
|
|
|
|
|
yield event.plain_result(message)
|
|
|
|
|
|
return
|
2026-07-22 23:08:10 +08:00
|
|
|
|
if request.operation == "unsubscribe":
|
|
|
|
|
|
await self.subscription_service.delete_subscription(
|
|
|
|
|
|
str(request.target["media_id"]),
|
|
|
|
|
|
season=request.target.get("season"),
|
|
|
|
|
|
)
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"已取消订阅:{request.target['media_id']}"
|
|
|
|
|
|
)
|
|
|
|
|
|
return
|
2026-07-20 22:25:50 +08:00
|
|
|
|
if request.operation == "delete_download":
|
|
|
|
|
|
task_id = str(request.target["task_id"])
|
|
|
|
|
|
await self.download_service.delete(task_id)
|
|
|
|
|
|
yield event.plain_result(f"已删除下载任务:{task_id}")
|
|
|
|
|
|
return
|
2026-07-20 22:24:07 +08:00
|
|
|
|
yield event.plain_result("不支持的确认操作。")
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 确认操作失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"操作失败:{exc}")
|
|
|
|
|
|
|
2026-07-20 22:25:50 +08:00
|
|
|
|
@moviepilot.command("downloads", alias={"下载列表"})
|
|
|
|
|
|
async def list_downloads(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""查询当前下载任务。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
tasks = await self.download_service.list_tasks()
|
|
|
|
|
|
yield event.plain_result(format_download_tasks(tasks))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning(f"MoviePilot 查询下载任务失败: {exc}")
|
|
|
|
|
|
yield event.plain_result(f"查询下载任务失败:{exc}")
|
|
|
|
|
|
|
2026-07-22 23:25:14 +08:00
|
|
|
|
@moviepilot.command("notifications", alias={"\u901a\u77e5"})
|
2026-07-22 23:08:10 +08:00
|
|
|
|
async def list_notifications(self, event: AstrMessageEvent, count: int = 10) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u67e5\u8be2\u6700\u8fd1\u7684 MoviePilot \u901a\u77e5\u3002"""
|
2026-07-22 23:08:10 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
items = await self.notification_poller.list_recent(count=max(1, min(count, 50)))
|
|
|
|
|
|
if not items:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result("\u6682\u65e0\u901a\u77e5\u3002")
|
2026-07-22 23:08:10 +08:00
|
|
|
|
return
|
|
|
|
|
|
yield event.plain_result("\n\n".join(format_notification(item) for item in items))
|
|
|
|
|
|
except Exception as exc:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u67e5\u8be2\u901a\u77e5\u5931\u8d25\uff1a{format_moviepilot_error(exc)}")
|
2026-07-22 23:08:10 +08:00
|
|
|
|
|
2026-07-20 22:25:50 +08:00
|
|
|
|
@moviepilot.command("cancel", alias={"取消"})
|
|
|
|
|
|
async def cancel_confirmation(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""取消当前会话的待确认操作。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
pending = [
|
|
|
|
|
|
item
|
|
|
|
|
|
for item in self.confirmations.snapshot()
|
|
|
|
|
|
if item["session_origin"] == event.unified_msg_origin
|
|
|
|
|
|
and item["user_id"] == event.get_sender_id()
|
|
|
|
|
|
]
|
|
|
|
|
|
for item in pending:
|
|
|
|
|
|
self.confirmations.cancel(item["code"])
|
|
|
|
|
|
yield event.plain_result("已取消待确认操作。" if pending else "当前没有待确认操作。")
|
|
|
|
|
|
|
2026-07-20 22:31:36 +08:00
|
|
|
|
@filter.llm_tool(name="moviepilot_search_media")
|
|
|
|
|
|
async def llm_search_media(
|
|
|
|
|
|
self, event: AstrMessageEvent, title: str, media_type: str = ""
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""搜索 MoviePilot 中的影视信息。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
title(string): 要搜索的影视标题。
|
|
|
|
|
|
media_type(string): 可选媒体类型,填写 movie 或 tv。
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
results = await self.media_service.search_media(
|
|
|
|
|
|
event.unified_msg_origin,
|
|
|
|
|
|
title,
|
|
|
|
|
|
media_type=media_type or None,
|
|
|
|
|
|
)
|
2026-07-20 23:32:48 +08:00
|
|
|
|
yield event.chain_result(_media_result_chain(results))
|
2026-07-20 22:31:36 +08:00
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
yield event.plain_result(f"媒体搜索失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_search_resources")
|
|
|
|
|
|
async def llm_search_resources(
|
|
|
|
|
|
self, event: AstrMessageEvent, media_index: int
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""搜索指定影视的下载资源。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
media_index(number): 当前会话最近一次媒体搜索结果的编号。
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, media_index)
|
|
|
|
|
|
if media is None:
|
|
|
|
|
|
yield event.plain_result("媒体编号无效或已过期,请先重新搜索影视。")
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
results = await self.media_service.search_resources(
|
|
|
|
|
|
event.unified_msg_origin, media
|
|
|
|
|
|
)
|
|
|
|
|
|
yield event.plain_result(format_resource_results(results))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
yield event.plain_result(f"资源搜索失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_list_subscriptions")
|
|
|
|
|
|
async def llm_list_subscriptions(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""查询 MoviePilot 当前订阅列表。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
subscriptions = await self.subscription_service.list_subscriptions()
|
|
|
|
|
|
yield event.plain_result(format_subscriptions(subscriptions))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
yield event.plain_result(f"查询订阅失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_list_downloads")
|
|
|
|
|
|
async def llm_list_downloads(self, event: AstrMessageEvent) -> None:
|
|
|
|
|
|
"""查询 MoviePilot 当前下载任务。"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
tasks = await self.download_service.list_tasks()
|
|
|
|
|
|
yield event.plain_result(format_download_tasks(tasks))
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
yield event.plain_result(f"查询下载任务失败:{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_request_subscribe")
|
|
|
|
|
|
async def llm_request_subscribe(
|
|
|
|
|
|
self, event: AstrMessageEvent, media_index: int, season: int = 0
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""请求订阅影视,并等待用户确认。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
media_index(number): 当前会话最近一次媒体搜索结果的编号。
|
|
|
|
|
|
season(number): 可选电视剧季号,电影填写 0。
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
media = self.session_store.get_media_result(event.unified_msg_origin, media_index)
|
|
|
|
|
|
if media is None:
|
|
|
|
|
|
yield event.plain_result("媒体编号无效或已过期,请先重新搜索影视。")
|
|
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="subscribe",
|
|
|
|
|
|
target={"media": media, "season": season or None},
|
|
|
|
|
|
)
|
|
|
|
|
|
title = media.get("title") or media.get("name") or "未命名"
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备订阅《{title}》。请回复 /mp confirm {request.code} 确认。"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_request_download")
|
|
|
|
|
|
async def llm_request_download(
|
|
|
|
|
|
self, event: AstrMessageEvent, resource_index: int
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""请求下载资源,并等待用户确认。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
resource_index(number): 当前会话最近一次资源搜索结果的编号。
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
origin = event.unified_msg_origin
|
|
|
|
|
|
resource = self.session_store.get_resource_result(origin, resource_index)
|
|
|
|
|
|
media = self.session_store.get_resource_media(origin)
|
|
|
|
|
|
if resource is None or media is None:
|
|
|
|
|
|
yield event.plain_result("资源编号无效或已过期,请先重新搜索资源。")
|
|
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="download",
|
|
|
|
|
|
target={"resource": resource, "media": media},
|
|
|
|
|
|
)
|
|
|
|
|
|
title = resource.get("title") or resource.get("name") or "未命名资源"
|
|
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
f"准备下载《{title}》。请回复 /mp confirm {request.code} 确认。"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-07-22 23:14:22 +08:00
|
|
|
|
@filter.llm_tool(name="moviepilot_list_notifications")
|
|
|
|
|
|
async def llm_list_notifications(self, event: AstrMessageEvent, count: int = 10) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u67e5\u8be2 MoviePilot \u901a\u77e5\u3002"""
|
2026-07-22 23:14:22 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
items = await self.notification_poller.list_recent(count=max(1, min(count, 50)))
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(
|
|
|
|
|
|
"\n\n".join(format_notification(item) for item in items)
|
|
|
|
|
|
or "\u6682\u65e0\u901a\u77e5\u3002"
|
|
|
|
|
|
)
|
2026-07-22 23:14:22 +08:00
|
|
|
|
except Exception as exc:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u67e5\u8be2\u901a\u77e5\u5931\u8d25\uff1a{format_moviepilot_error(exc)}")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_trigger_subscription_search")
|
|
|
|
|
|
async def llm_trigger_subscription_search(self, event: AstrMessageEvent, subscribe_id: int) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u624b\u52a8\u89e6\u53d1\u6307\u5b9a\u8ba2\u9605\u641c\u7d22\u3002"""
|
2026-07-22 23:14:22 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
await self.subscription_service.trigger_subscription_search(subscribe_id)
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u5df2\u89e6\u53d1\u8ba2\u9605 {subscribe_id} \u641c\u7d22\u3002")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
except Exception as exc:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u89e6\u53d1\u8ba2\u9605\u641c\u7d22\u5931\u8d25\uff1a{format_moviepilot_error(exc)}")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_request_unsubscribe")
|
|
|
|
|
|
async def llm_request_unsubscribe(self, event: AstrMessageEvent, subscription_id: str) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u8bf7\u6c42\u53d6\u6d88\u8ba2\u9605\u5e76\u521b\u5efa\u786e\u8ba4\u8bf7\u6c42\u3002"""
|
2026-07-22 23:14:22 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
subscriptions = await self.subscription_service.list_subscriptions()
|
|
|
|
|
|
matched = next((item for item in subscriptions if str(item.get("id")) == str(subscription_id)), None)
|
|
|
|
|
|
media_id = media_id_from_subscription(matched) if matched else str(subscription_id)
|
|
|
|
|
|
if not media_id:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result("\u627e\u4e0d\u5230\u8ba2\u9605\u3002")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="unsubscribe",
|
|
|
|
|
|
target={"media_id": media_id, "season": matched.get("season") if matched else None},
|
|
|
|
|
|
)
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u51c6\u5907\u53d6\u6d88\u8ba2\u9605 {subscription_id}\uff0c\u8bf7\u56de\u590d /mp confirm {request.code} \u786e\u8ba4\u3002")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
|
|
|
|
|
|
@filter.llm_tool(name="moviepilot_request_delete_download")
|
|
|
|
|
|
async def llm_request_delete_download(self, event: AstrMessageEvent, task_id: str) -> None:
|
2026-07-22 23:25:14 +08:00
|
|
|
|
"""\u8bf7\u6c42\u5220\u9664\u4e0b\u8f7d\u4efb\u52a1\u5e76\u521b\u5efa\u786e\u8ba4\u8bf7\u6c42\u3002"""
|
2026-07-22 23:14:22 +08:00
|
|
|
|
if not self.access_policy.is_allowed(event):
|
|
|
|
|
|
return
|
|
|
|
|
|
request = self.confirmations.create(
|
|
|
|
|
|
session_origin=event.unified_msg_origin,
|
|
|
|
|
|
user_id=event.get_sender_id(),
|
|
|
|
|
|
operation="delete_download",
|
|
|
|
|
|
target={"task_id": str(task_id)},
|
|
|
|
|
|
)
|
2026-07-22 23:25:14 +08:00
|
|
|
|
yield event.plain_result(f"\u51c6\u5907\u5220\u9664\u4e0b\u8f7d\u4efb\u52a1 {task_id}\uff0c\u8bf7\u56de\u590d /mp confirm {request.code} \u786e\u8ba4\u3002")
|
2026-07-22 23:14:22 +08:00
|
|
|
|
|
2026-07-20 22:28:19 +08:00
|
|
|
|
async def _send_notification(self, target: str, text: str) -> None:
|
|
|
|
|
|
await self.context.send_message(target, MessageChain().message(text))
|
|
|
|
|
|
|
2026-07-20 22:13:04 +08:00
|
|
|
|
async def terminate(self) -> None:
|
|
|
|
|
|
"""释放插件资源。"""
|
2026-07-20 22:21:29 +08:00
|
|
|
|
await self.media_service.close()
|
2026-07-20 22:24:07 +08:00
|
|
|
|
await self.subscription_service.close()
|
2026-07-20 22:25:50 +08:00
|
|
|
|
await self.download_service.close()
|
2026-07-20 22:28:19 +08:00
|
|
|
|
await self.notification_poller.close()
|
2026-07-20 22:13:04 +08:00
|
|
|
|
logger.info("MoviePilot 插件已停止")
|