feat: improve media search result presentation
这个提交存在于:
父节点
7851dc2418
当前提交
6e3fa72be5
共有 5 个文件被更改,包括 99 次插入 和 19 次删除
24
main.py
24
main.py
|
|
@ -5,7 +5,7 @@ from astrbot.api.event import AstrMessageEvent, filter
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .access_policy import AccessPolicy
|
from .access_policy import AccessPolicy
|
||||||
from .media_service import MediaService, format_media_results, format_resource_results
|
from .media_service import MediaService, format_media_result_item, format_resource_results, media_poster_url
|
||||||
from .session_context import SessionContextStore
|
from .session_context import SessionContextStore
|
||||||
from .confirmation import ConfirmationStore
|
from .confirmation import ConfirmationStore
|
||||||
from .subscription_service import SubscriptionService, format_subscriptions
|
from .subscription_service import SubscriptionService, format_subscriptions
|
||||||
|
|
@ -13,20 +13,34 @@ try:
|
||||||
from .notification_service import NotificationPoller
|
from .notification_service import NotificationPoller
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from access_policy import AccessPolicy
|
from access_policy import AccessPolicy
|
||||||
from media_service import MediaService, format_media_results, format_resource_results
|
from media_service import MediaService, format_media_result_item, format_resource_results, media_poster_url
|
||||||
from session_context import SessionContextStore
|
from session_context import SessionContextStore
|
||||||
from confirmation import ConfirmationStore
|
from confirmation import ConfirmationStore
|
||||||
from subscription_service import SubscriptionService, format_subscriptions
|
from subscription_service import SubscriptionService, format_subscriptions
|
||||||
from download_service import DownloadService, format_download_tasks
|
from download_service import DownloadService, format_download_tasks
|
||||||
from notification_service import NotificationPoller
|
from notification_service import NotificationPoller
|
||||||
from astrbot.api.event import MessageChain
|
from astrbot.api.event import MessageChain
|
||||||
|
from astrbot.api.message_components import Image, Plain
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
@star.register(
|
@star.register(
|
||||||
"astrbot_plugin_moviepilot",
|
"astrbot_plugin_moviepilot",
|
||||||
"AstrBot MoviePilot Contributors",
|
"AstrBot MoviePilot Contributors",
|
||||||
"在 AstrBot 中通过命令和 LLM 控制 MoviePilot。",
|
"在 AstrBot 中通过命令和 LLM 控制 MoviePilot。",
|
||||||
"0.11.0",
|
"0.12.0",
|
||||||
)
|
)
|
||||||
class MoviePilotPlugin(star.Star):
|
class MoviePilotPlugin(star.Star):
|
||||||
"""MoviePilot integration entrypoint."""
|
"""MoviePilot integration entrypoint."""
|
||||||
|
|
@ -86,7 +100,7 @@ class MoviePilotPlugin(star.Star):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
results = await self.media_service.search_media(event.unified_msg_origin, title)
|
results = await self.media_service.search_media(event.unified_msg_origin, title)
|
||||||
yield event.plain_result(format_media_results(results))
|
yield event.chain_result(_media_result_chain(results))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(f"MoviePilot 媒体搜索失败: {exc}")
|
logger.warning(f"MoviePilot 媒体搜索失败: {exc}")
|
||||||
yield event.plain_result(f"媒体搜索失败:{exc}")
|
yield event.plain_result(f"媒体搜索失败:{exc}")
|
||||||
|
|
@ -339,7 +353,7 @@ class MoviePilotPlugin(star.Star):
|
||||||
title,
|
title,
|
||||||
media_type=media_type or None,
|
media_type=media_type or None,
|
||||||
)
|
)
|
||||||
yield event.plain_result(format_media_results(results))
|
yield event.chain_result(_media_result_chain(results))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
yield event.plain_result(f"媒体搜索失败:{exc}")
|
yield event.plain_result(f"媒体搜索失败:{exc}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ class MediaService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _media_id(media: Mapping[str, Any]) -> str:
|
def _media_id(media: Mapping[str, Any]) -> str:
|
||||||
for key in ("mediaid", "media_id", "id", "mediaId"):
|
for key in ("mediaid", "media_id", "id", "mediaId", "tmdb_id", "tmdbid"):
|
||||||
value = media.get(key)
|
value = media.get(key)
|
||||||
if value:
|
if value:
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
@ -122,22 +122,67 @@ class MediaService:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def _first_value(item: Mapping[str, Any], *keys: str) -> Any:
|
||||||
|
for key in keys:
|
||||||
|
value = item.get(key)
|
||||||
|
if value not in (None, "", [], {}):
|
||||||
|
return value
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _format_release_month(item: Mapping[str, Any]) -> str:
|
||||||
|
value = _first_value(item, "release_date", "first_air_date", "year")
|
||||||
|
if value is None:
|
||||||
|
return "未公布"
|
||||||
|
text = str(value)
|
||||||
|
if len(text) >= 7 and text[4] == "-":
|
||||||
|
return text[:7]
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def media_poster_url(item: Mapping[str, Any]) -> str | None:
|
||||||
|
value = _first_value(item, "poster_path", "poster", "cover_url", "cover", "image")
|
||||||
|
if isinstance(value, str) and value.startswith(("http://", "https://")):
|
||||||
|
return value
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def format_media_result_item(index: int, item: Mapping[str, Any]) -> str:
|
||||||
|
title = _first_value(item, "title", "name", "original_title") or "未命名"
|
||||||
|
tmdb_id = _first_value(item, "tmdb_id", "tmdbid", "tmdbId")
|
||||||
|
if tmdb_id is None:
|
||||||
|
raw_id = _first_value(item, "id")
|
||||||
|
if isinstance(raw_id, str) and raw_id.lower().startswith("tmdb:"):
|
||||||
|
tmdb_id = raw_id.split(":", 1)[1]
|
||||||
|
episodes = _first_value(
|
||||||
|
item,
|
||||||
|
"number_of_episodes",
|
||||||
|
"episode_count",
|
||||||
|
"episodes",
|
||||||
|
"total_episode",
|
||||||
|
)
|
||||||
|
rating = _first_value(item, "vote_average", "rating", "vote")
|
||||||
|
media_type = _first_value(item, "type_name", "type")
|
||||||
|
lines = [
|
||||||
|
f"{index}. {title}",
|
||||||
|
f"TMDB编号:{tmdb_id if tmdb_id is not None else '未提供'}",
|
||||||
|
f"上映年月:{_format_release_month(item)}",
|
||||||
|
f"集数:{episodes if episodes is not None else '未公布'}",
|
||||||
|
f"评分:{rating if rating is not None else '暂无'}",
|
||||||
|
]
|
||||||
|
if media_type:
|
||||||
|
lines.append(f"类型:{media_type}")
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def format_media_results(results: list[dict[str, Any]]) -> str:
|
def format_media_results(results: list[dict[str, Any]]) -> str:
|
||||||
if not results:
|
if not results:
|
||||||
return "没有找到匹配的影视信息。"
|
return "没有找到匹配的影视信息。"
|
||||||
lines = ["影视搜索结果:"]
|
lines = ["影视搜索结果:"]
|
||||||
for index, item in enumerate(results, start=1):
|
for index, item in enumerate(results, start=1):
|
||||||
title = item.get("title") or item.get("name") or "未命名"
|
lines.append(format_media_result_item(index, item))
|
||||||
year = item.get("year") or item.get("release_date") or ""
|
|
||||||
media_type = item.get("type_name") or item.get("type") or ""
|
|
||||||
media_id = MediaService._media_id(item)
|
|
||||||
vote = item.get("vote") or item.get("rating") or ""
|
|
||||||
details = " / ".join(str(value) for value in (year, media_type, vote) if value)
|
|
||||||
suffix = f"({details})" if details else ""
|
|
||||||
lines.append(f"{index}. {title}{suffix} [{media_id}]")
|
|
||||||
lines.append("可继续使用:/mp resources <编号> 或 /mp subscribe <编号>")
|
lines.append("可继续使用:/mp resources <编号> 或 /mp subscribe <编号>")
|
||||||
return "\n".join(lines)
|
return "\n\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
def format_resource_results(results: list[dict[str, Any]]) -> str:
|
def format_resource_results(results: list[dict[str, Any]]) -> str:
|
||||||
if not results:
|
if not results:
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ name: astrbot_plugin_moviepilot
|
||||||
display_name: MoviePilot 控制
|
display_name: MoviePilot 控制
|
||||||
description: 在 AstrBot 中通过命令和 LLM 控制 MoviePilot。
|
description: 在 AstrBot 中通过命令和 LLM 控制 MoviePilot。
|
||||||
short_desc: 搜索影视、订阅、下载和接收 MoviePilot 通知。
|
short_desc: 搜索影视、订阅、下载和接收 MoviePilot 通知。
|
||||||
version: 0.11.0
|
version: 0.12.0
|
||||||
author: AstrBot MoviePilot Contributors
|
author: AstrBot MoviePilot Contributors
|
||||||
repo: https://github.com/jxxghp/MoviePilot
|
repo: https://github.com/jxxghp/MoviePilot
|
||||||
astrbot_version: ">=4.16,<5"
|
astrbot_version: ">=4.16,<5"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from media_service import MediaService, format_media_results, format_resource_results
|
from media_service import MediaService, format_media_results, format_resource_results, media_poster_url
|
||||||
from session_context import SessionContextStore
|
from session_context import SessionContextStore
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -56,3 +56,24 @@ def test_formatters_include_next_commands() -> None:
|
||||||
assert "/mp download <编号>" in resource_text
|
assert "/mp download <编号>" in resource_text
|
||||||
assert "没有找到" in format_media_results([])
|
assert "没有找到" in format_media_results([])
|
||||||
assert "/mp subscribe <媒体编号>" in format_resource_results([])
|
assert "/mp subscribe <媒体编号>" in format_resource_results([])
|
||||||
|
|
||||||
|
|
||||||
|
def test_media_formatter_uses_moviepilot_metadata_fields() -> None:
|
||||||
|
text = format_media_results(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "铁拳教育",
|
||||||
|
"tmdb_id": 276161,
|
||||||
|
"release_date": "2026-06-05",
|
||||||
|
"number_of_episodes": 12,
|
||||||
|
"vote_average": 9.5,
|
||||||
|
"poster_path": "https://image.tmdb.org/t/p/poster.jpg",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "TMDB编号:276161" in text
|
||||||
|
assert "上映年月:2026-06" in text
|
||||||
|
assert "集数:12" in text
|
||||||
|
assert "评分:9.5" in text
|
||||||
|
assert media_poster_url({"poster_path": "https://image.tmdb.org/p/t.jpg"})
|
||||||
|
|
@ -13,7 +13,7 @@ def test_metadata_contains_required_plugin_fields() -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
assert metadata["name"] == "astrbot_plugin_moviepilot"
|
assert metadata["name"] == "astrbot_plugin_moviepilot"
|
||||||
assert metadata["version"] == "0.11.0"
|
assert metadata["version"] == "0.12.0"
|
||||||
assert metadata["astrbot_version"] == ">=4.16,<5"
|
assert metadata["astrbot_version"] == ">=4.16,<5"
|
||||||
assert "aiocqhttp" in metadata["support_platforms"]
|
assert "aiocqhttp" in metadata["support_platforms"]
|
||||||
assert "telegram" in metadata["support_platforms"]
|
assert "telegram" in metadata["support_platforms"]
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用