fix 取消Chain全局单例

This commit is contained in:
jxxghp 2023-08-01 20:29:24 +08:00
parent 0914dace42
commit cb08d57d7a

View File

@ -9,10 +9,16 @@ from app.core.event import EventManager
from app.log import logger from app.log import logger
from app.schemas import Notification from app.schemas import Notification
from app.schemas.types import EventType, MessageChannel from app.schemas.types import EventType, MessageChannel
from app.utils.singleton import Singleton
# 当前页面
_current_page: int = 0
# 当前元数据
_current_meta: Optional[MetaBase] = None
# 当前媒体信息
_current_media: Optional[MediaInfo] = None
class MessageChain(ChainBase, metaclass=Singleton): class MessageChain(ChainBase):
""" """
外来消息处理链 外来消息处理链
""" """
@ -20,12 +26,6 @@ class MessageChain(ChainBase, metaclass=Singleton):
_cache_file = "__user_messages__" _cache_file = "__user_messages__"
# 每页数据量 # 每页数据量
_page_size: int = 8 _page_size: int = 8
# 当前页面
_current_page: int = 0
# 当前元数据
_current_meta: Optional[MetaBase] = None
# 当前媒体信息
_current_media: Optional[MediaInfo] = None
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -41,6 +41,8 @@ class MessageChain(ChainBase, metaclass=Singleton):
""" """
识别消息内容执行操作 识别消息内容执行操作
""" """
# 申明全局变量
global _current_page, _current_meta, _current_media
# 获取消息内容 # 获取消息内容
info = self.message_parser(body=body, form=form, args=args) info = self.message_parser(body=body, form=form, args=args)
if not info: if not info:
@ -90,16 +92,16 @@ class MessageChain(ChainBase, metaclass=Singleton):
cache_list: list = cache_data.get('items') cache_list: list = cache_data.get('items')
# 选择 # 选择
if cache_type == "Search": if cache_type == "Search":
mediainfo: MediaInfo = cache_list[int(text) + self._current_page * self._page_size - 1] mediainfo: MediaInfo = cache_list[int(text) + _current_page * self._page_size - 1]
self._current_media = mediainfo _current_media = mediainfo
# 查询缺失的媒体信息 # 查询缺失的媒体信息
exist_flag, no_exists = self.downloadchain.get_no_exists_info(meta=self._current_meta, exist_flag, no_exists = self.downloadchain.get_no_exists_info(meta=_current_meta,
mediainfo=self._current_media) mediainfo=_current_media)
if exist_flag: if exist_flag:
self.post_message( self.post_message(
Notification(channel=channel, Notification(channel=channel,
title=f"{self._current_media.title_year}" title=f"{_current_media.title_year}"
f"{self._current_meta.sea} 媒体库中已存在", f"{_current_meta.sea} 媒体库中已存在",
userid=userid)) userid=userid))
return return
# 发送缺失的媒体信息 # 发送缺失的媒体信息
@ -123,7 +125,7 @@ class MessageChain(ChainBase, metaclass=Singleton):
# 没有数据 # 没有数据
self.post_message(Notification( self.post_message(Notification(
channel=channel, title=f"{mediainfo.title}" channel=channel, title=f"{mediainfo.title}"
f"{self._current_meta.sea} 未搜索到需要的资源!", f"{_current_meta.sea} 未搜索到需要的资源!",
userid=userid)) userid=userid))
return return
# 搜索结果排序 # 搜索结果排序
@ -133,7 +135,7 @@ class MessageChain(ChainBase, metaclass=Singleton):
"type": "Torrent", "type": "Torrent",
"items": contexts "items": contexts
} }
self._current_page = 0 _current_page = 0
# 发送种子数据 # 发送种子数据
logger.info(f"搜索到 {len(contexts)} 条数据,开始发送选择消息 ...") logger.info(f"搜索到 {len(contexts)} 条数据,开始发送选择消息 ...")
self.__post_torrents_message(channel=channel, self.__post_torrents_message(channel=channel,
@ -146,13 +148,13 @@ class MessageChain(ChainBase, metaclass=Singleton):
# 订阅媒体 # 订阅媒体
mediainfo: MediaInfo = cache_list[int(text) - 1] mediainfo: MediaInfo = cache_list[int(text) - 1]
# 查询缺失的媒体信息 # 查询缺失的媒体信息
exist_flag, _ = self.downloadchain.get_no_exists_info(meta=self._current_meta, exist_flag, _ = self.downloadchain.get_no_exists_info(meta=_current_meta,
mediainfo=mediainfo) mediainfo=mediainfo)
if exist_flag: if exist_flag:
self.post_message(Notification( self.post_message(Notification(
channel=channel, channel=channel,
title=f"{mediainfo.title_year}" title=f"{mediainfo.title_year}"
f"{self._current_meta.sea} 媒体库中已存在", f"{_current_meta.sea} 媒体库中已存在",
userid=userid)) userid=userid))
return return
# 添加订阅状态为N # 添加订阅状态为N
@ -160,7 +162,7 @@ class MessageChain(ChainBase, metaclass=Singleton):
year=mediainfo.year, year=mediainfo.year,
mtype=mediainfo.type, mtype=mediainfo.type,
tmdbid=mediainfo.tmdb_id, tmdbid=mediainfo.tmdb_id,
season=self._current_meta.begin_season, season=_current_meta.begin_season,
channel=channel, channel=channel,
userid=userid, userid=userid,
username=username) username=username)
@ -168,13 +170,13 @@ class MessageChain(ChainBase, metaclass=Singleton):
if int(text) == 0: if int(text) == 0:
# 自动选择下载 # 自动选择下载
# 查询缺失的媒体信息 # 查询缺失的媒体信息
exist_flag, no_exists = self.downloadchain.get_no_exists_info(meta=self._current_meta, exist_flag, no_exists = self.downloadchain.get_no_exists_info(meta=_current_meta,
mediainfo=self._current_media) mediainfo=_current_media)
if exist_flag: if exist_flag:
self.post_message(Notification( self.post_message(Notification(
channel=channel, channel=channel,
title=f"{self._current_media.title_year}" title=f"{_current_media.title_year}"
f"{self._current_meta.sea} 媒体库中已存在", f"{_current_meta.sea} 媒体库中已存在",
userid=userid)) userid=userid))
return return
# 批量下载 # 批量下载
@ -183,16 +185,16 @@ class MessageChain(ChainBase, metaclass=Singleton):
userid=userid) userid=userid)
if downloads and not lefts: if downloads and not lefts:
# 全部下载完成 # 全部下载完成
logger.info(f'{self._current_media.title_year} 下载完成') logger.info(f'{_current_media.title_year} 下载完成')
else: else:
# 未完成下载 # 未完成下载
logger.info(f'{self._current_media.title_year} 未下载未完整,添加订阅 ...') logger.info(f'{_current_media.title_year} 未下载未完整,添加订阅 ...')
# 添加订阅状态为R # 添加订阅状态为R
self.subscribechain.add(title=self._current_media.title, self.subscribechain.add(title=_current_media.title,
year=self._current_media.year, year=_current_media.year,
mtype=self._current_media.type, mtype=_current_media.type,
tmdbid=self._current_media.tmdb_id, tmdbid=_current_media.tmdb_id,
season=self._current_meta.begin_season, season=_current_meta.begin_season,
channel=channel, channel=channel,
userid=userid, userid=userid,
username=username, username=username,
@ -212,7 +214,7 @@ class MessageChain(ChainBase, metaclass=Singleton):
channel=channel, title="输入有误!", userid=userid)) channel=channel, title="输入有误!", userid=userid))
return return
if self._current_page == 0: if _current_page == 0:
# 第一页 # 第一页
self.post_message(Notification( self.post_message(Notification(
channel=channel, title="已经是第一页了!", userid=userid)) channel=channel, title="已经是第一页了!", userid=userid))
@ -220,24 +222,24 @@ class MessageChain(ChainBase, metaclass=Singleton):
cache_type: str = cache_data.get('type') cache_type: str = cache_data.get('type')
cache_list: list = cache_data.get('items') cache_list: list = cache_data.get('items')
# 减一页 # 减一页
self._current_page -= 1 _current_page -= 1
if self._current_page == 0: if _current_page == 0:
start = 0 start = 0
end = self._page_size end = self._page_size
else: else:
start = self._current_page * self._page_size start = _current_page * self._page_size
end = start + self._page_size end = start + self._page_size
if cache_type == "Torrent": if cache_type == "Torrent":
# 发送种子数据 # 发送种子数据
self.__post_torrents_message(channel=channel, self.__post_torrents_message(channel=channel,
title=self._current_media.title, title=_current_media.title,
items=cache_list[start:end], items=cache_list[start:end],
userid=userid, userid=userid,
total=len(cache_list)) total=len(cache_list))
else: else:
# 发送媒体数据 # 发送媒体数据
self.__post_medias_message(channel=channel, self.__post_medias_message(channel=channel,
title=self._current_meta.name, title=_current_meta.name,
items=cache_list[start:end], items=cache_list[start:end],
userid=userid, userid=userid,
total=len(cache_list)) total=len(cache_list))
@ -255,7 +257,7 @@ class MessageChain(ChainBase, metaclass=Singleton):
total = len(cache_list) total = len(cache_list)
# 加一页 # 加一页
cache_list = cache_list[ cache_list = cache_list[
(self._current_page + 1) * self._page_size:(self._current_page + 2) * self._page_size] (_current_page + 1) * self._page_size:(_current_page + 2) * self._page_size]
if not cache_list: if not cache_list:
# 没有数据 # 没有数据
self.post_message(Notification( self.post_message(Notification(
@ -263,16 +265,16 @@ class MessageChain(ChainBase, metaclass=Singleton):
return return
else: else:
# 加一页 # 加一页
self._current_page += 1 _current_page += 1
if cache_type == "Torrent": if cache_type == "Torrent":
# 发送种子数据 # 发送种子数据
self.__post_torrents_message(channel=channel, self.__post_torrents_message(channel=channel,
title=self._current_media.title, title=_current_media.title,
items=cache_list, userid=userid, total=total) items=cache_list, userid=userid, total=total)
else: else:
# 发送媒体数据 # 发送媒体数据
self.__post_medias_message(channel=channel, self.__post_medias_message(channel=channel,
title=self._current_meta.name, title=_current_meta.name,
items=cache_list, userid=userid, total=total) items=cache_list, userid=userid, total=total)
else: else:
@ -299,13 +301,13 @@ class MessageChain(ChainBase, metaclass=Singleton):
return return
logger.info(f"搜索到 {len(medias)} 条相关媒体信息") logger.info(f"搜索到 {len(medias)} 条相关媒体信息")
# 记录当前状态 # 记录当前状态
self._current_meta = meta _current_meta = meta
user_cache[userid] = { user_cache[userid] = {
'type': action, 'type': action,
'items': medias 'items': medias
} }
self._current_page = 0 _current_page = 0
self._current_media = None _current_media = None
# 发送媒体列表 # 发送媒体列表
self.__post_medias_message(channel=channel, self.__post_medias_message(channel=channel,
title=meta.name, title=meta.name,