From bfa78c5afbdd7ad2bf91d4bcaf48e4ad5fcf9b1c Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 10 Jun 2023 23:07:26 +0800 Subject: [PATCH] fix wechat message --- app/chain/__init__.py | 4 +++- app/chain/user_message.py | 7 ++++++- app/modules/__init__.py | 2 ++ app/modules/telegram/__init__.py | 5 ++++- app/modules/telegram/telegram.py | 7 +++++-- app/modules/wechat/__init__.py | 2 ++ app/modules/wechat/wechat.py | 16 ++++++++++++---- 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index b99d0d6e..e4b2d400 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -130,8 +130,10 @@ class ChainBase(AbstractSingleton, metaclass=Singleton): return self.run_module("post_medias_message", title=title, items=items, userid=userid) def post_torrents_message(self, title: str, items: List[Context], + mediainfo: MediaInfo, userid: Union[str, int] = None) -> Optional[bool]: - return self.run_module("post_torrents_message", title=title, items=items, userid=userid) + return self.run_module("post_torrents_message", title=title, mediainfo=mediainfo, + items=items, userid=userid) def scrape_metadata(self, path: Path, mediainfo: MediaInfo) -> None: return self.run_module("scrape_metadata", path=path, mediainfo=mediainfo) diff --git a/app/chain/user_message.py b/app/chain/user_message.py index 25ca790b..7a5e8641 100644 --- a/app/chain/user_message.py +++ b/app/chain/user_message.py @@ -114,6 +114,7 @@ class UserMessageChain(ChainBase): logger.info(f"搜索到 {len(contexts)} 条数据,开始发送选择消息 ...") self.__post_torrents_message(title=mediainfo.title, items=contexts[:self._page_size], + mediainfo=mediainfo, userid=userid, total=len(contexts)) @@ -212,6 +213,7 @@ class UserMessageChain(ChainBase): # 发送种子数据 self.__post_torrents_message(title=self._current_media.title, items=cache_list[start:end], + mediainfo=self._current_media, userid=userid, total=len(cache_list)) else: @@ -242,6 +244,7 @@ class UserMessageChain(ChainBase): if cache_type == "Torrent": # 发送种子数据 self.__post_torrents_message(title=self._current_media.title, + mediainfo=self._current_media, items=cache_list, userid=userid, total=total) else: # 发送媒体数据 @@ -305,12 +308,14 @@ class UserMessageChain(ChainBase): userid=userid ) - def __post_torrents_message(self, title: str, items: list, userid: str, total: int): + def __post_torrents_message(self, title: str, items: list, + mediainfo: MediaInfo, userid: str, total: int): """ 发送种子列表消息 """ self.post_torrents_message( title=f"【{title}】共找到{total}条相关资源,请回复对应数字下载(0: 自动选择 p: 上一页 n: 下一页)", items=items, + mediainfo=mediainfo, userid=userid ) diff --git a/app/modules/__init__.py b/app/modules/__init__.py index ec96229c..595cb32f 100644 --- a/app/modules/__init__.py +++ b/app/modules/__init__.py @@ -211,11 +211,13 @@ class _ModuleBase(metaclass=ABCMeta): pass def post_torrents_message(self, title: str, items: List[Context], + mediainfo: MediaInfo, userid: Union[str, int] = None) -> Optional[bool]: """ 发送种子信息选择列表 :param title: 标题 :param items: 消息列表 + :param mediainfo: 识别的媒体信息 :param userid: 用户ID :return: 成功或失败 """ diff --git a/app/modules/telegram/__init__.py b/app/modules/telegram/__init__.py index 39de9e3b..983aa37d 100644 --- a/app/modules/telegram/__init__.py +++ b/app/modules/telegram/__init__.py @@ -114,15 +114,18 @@ class TelegramModule(_ModuleBase): return self.telegram.send_meidas_msg(title=title, medias=items, userid=userid) def post_torrents_message(self, title: str, items: List[Context], + mediainfo: MediaInfo = None, userid: Union[str, int] = None) -> Optional[bool]: """ 发送种子信息选择列表 :param title: 标题 :param items: 消息列表 + :param mediainfo: 媒体信息 :param userid: 用户ID :return: 成功或失败 """ - return self.telegram.send_torrents_msg(title=title, torrents=items, userid=userid) + return self.telegram.send_torrents_msg(title=title, torrents=items, + mediainfo=mediainfo, userid=userid) def register_commands(self, commands: dict): """ diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index 70cb5724..2fb58ccb 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -126,7 +126,9 @@ class Telegram(metaclass=Singleton): logger.error(f"发送消息失败:{msg_e}") return False - def send_torrents_msg(self, torrents: List[Context], userid: str = "", title: str = "") -> Optional[bool]: + def send_torrents_msg(self, torrents: List[Context], + mediainfo: MediaInfo = None, + userid: str = "", title: str = "") -> Optional[bool]: """ 发送列表消息 """ @@ -151,7 +153,8 @@ class Telegram(metaclass=Singleton): else: chat_id = self._telegram_chat_id - return self.__send_request(userid=chat_id, caption=caption) + return self.__send_request(userid=chat_id, caption=caption, + image=mediainfo.get_message_image()) except Exception as msg_e: logger.error(f"发送消息失败:{msg_e}") diff --git a/app/modules/wechat/__init__.py b/app/modules/wechat/__init__.py index 19981269..628d8992 100644 --- a/app/modules/wechat/__init__.py +++ b/app/modules/wechat/__init__.py @@ -142,11 +142,13 @@ class WechatModule(_ModuleBase): return self.wechat.send_medias_msg(medias=items, userid=userid) def post_torrents_message(self, title: str, items: List[Context], + mediainfo: MediaInfo, userid: Union[str, int] = None) -> Optional[bool]: """ 发送种子信息选择列表 :param title: 标题 :param items: 消息列表 + :param mediainfo: 媒体信息 :param userid: 用户ID :return: 成功或失败 """ diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index aa069c80..39a55a64 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -5,6 +5,7 @@ from typing import Optional, List from app.core.config import settings from app.core.context import MediaInfo, Context +from app.core.metainfo import MetaInfo from app.log import logger from app.utils.http import RequestUtils from app.utils.singleton import Singleton @@ -188,7 +189,8 @@ class WeChat(metaclass=Singleton): } return self.__post_request(message_url, req_json) - def send_torrents_msg(self, torrents: List[Context], userid: str = "", title: str = "") -> Optional[bool]: + def send_torrents_msg(self, torrents: List[Context], mediainfo: MediaInfo, + userid: str = "", title: str = "") -> Optional[bool]: """ 发送列表消息 """ @@ -208,10 +210,16 @@ class WeChat(metaclass=Singleton): index = 1 for context in torrents: torrent = context.torrent_info + meta = MetaInfo(title=torrent.title, subtitle=torrent.description) + torrent_title = f"【{torrent.site_name}】" \ + f"{meta.get_season_episode_string()} " \ + f"{meta.get_resource_type_string()} " \ + f"{meta.get_volume_factor_string()} " \ + f"{torrent.seeders}↑" articles.append({ - "title": f"{torrent.title}", - "description": f"【{torrent.site_name}】{torrent.description} " - f"{torrent.get_volume_factor_string()} {torrent.seeders}↑", + "title": torrent_title, + "description": torrent.description if index == 1 else '', + "picurl": mediainfo.get_message_image() if index == 1 else '', "url": torrent.page_url }) index += 1