117 行
5.2 KiB
Python
117 行
5.2 KiB
Python
"""AstrBot MoviePilot integration plugin."""
|
|
|
|
from astrbot.api import AstrBotConfig, logger, star
|
|
from astrbot.api.event import AstrMessageEvent, filter
|
|
|
|
from access_policy import AccessPolicy
|
|
from media_service import MediaService, format_media_results, format_resource_results
|
|
from session_context import SessionContextStore
|
|
|
|
|
|
@star.register(
|
|
"astrbot_plugin_moviepilot",
|
|
"AstrBot MoviePilot Contributors",
|
|
"在 AstrBot 中通过命令和 LLM 控制 MoviePilot。",
|
|
"0.1.0",
|
|
)
|
|
class MoviePilotPlugin(star.Star):
|
|
"""MoviePilot integration entrypoint."""
|
|
|
|
def __init__(self, context: star.Context, config: AstrBotConfig) -> None:
|
|
super().__init__(context)
|
|
self.config = config
|
|
self.access_policy = AccessPolicy(config)
|
|
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)
|
|
logger.info("MoviePilot 插件已加载")
|
|
|
|
@filter.command_group("mp")
|
|
def moviepilot(self) -> None:
|
|
"""MoviePilot 命令组。"""
|
|
|
|
@moviepilot.command("help", alias={"帮助"})
|
|
async def moviepilot_help(self, event: AstrMessageEvent) -> None:
|
|
"""显示 MoviePilot 插件命令入口。"""
|
|
if not self.access_policy.is_allowed(event):
|
|
return
|
|
yield event.plain_result(
|
|
"MoviePilot 命令:/mp search、/mp detail、/mp seasons、/mp resources、"
|
|
"/mp subscribe、/mp download。"
|
|
)
|
|
|
|
@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)
|
|
yield event.plain_result(format_media_results(results))
|
|
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}")
|
|
|
|
async def terminate(self) -> None:
|
|
"""释放插件资源。"""
|
|
await self.media_service.close()
|
|
logger.info("MoviePilot 插件已停止")
|