diff --git a/app/api/endpoints/douban.py b/app/api/endpoints/douban.py index 38a6c918..d4981f58 100644 --- a/app/api/endpoints/douban.py +++ b/app/api/endpoints/douban.py @@ -90,7 +90,7 @@ def movie_top250(page: int = 1, """ 浏览豆瓣剧集信息 """ - movies = DoubanChain().movie_top250(page=page, count=count) + movies = DoubanChain().movie_top250(page=page, count=count) or [] return [MediaInfo(douban_info=movie).to_dict() for movie in movies] @@ -101,7 +101,7 @@ def tv_weekly_chinese(page: int = 1, """ 中国每周剧集口碑榜 """ - tvs = DoubanChain().tv_weekly_chinese(page=page, count=count) + tvs = DoubanChain().tv_weekly_chinese(page=page, count=count) or [] return [MediaInfo(douban_info=tv).to_dict() for tv in tvs] @@ -112,7 +112,7 @@ def tv_weekly_global(page: int = 1, """ 全球每周剧集口碑榜 """ - tvs = DoubanChain().tv_weekly_global(page=page, count=count) + tvs = DoubanChain().tv_weekly_global(page=page, count=count) or [] return [MediaInfo(douban_info=tv).to_dict() for tv in tvs] @@ -123,7 +123,7 @@ def tv_animation(page: int = 1, """ 热门动画剧集 """ - tvs = DoubanChain().tv_animation(page=page, count=count) + tvs = DoubanChain().tv_animation(page=page, count=count) or [] return [MediaInfo(douban_info=tv).to_dict() for tv in tvs] @@ -134,7 +134,7 @@ def movie_hot(page: int = 1, """ 热门电影 """ - movies = DoubanChain().movie_hot(page=page, count=count) + movies = DoubanChain().movie_hot(page=page, count=count) or [] return [MediaInfo(douban_info=movie).to_dict() for movie in movies] @@ -145,7 +145,7 @@ def tv_hot(page: int = 1, """ 热门电视剧 """ - tvs = DoubanChain().tv_hot(page=page, count=count) + tvs = DoubanChain().tv_hot(page=page, count=count) or [] return [MediaInfo(douban_info=tv).to_dict() for tv in tvs] diff --git a/app/api/endpoints/system.py b/app/api/endpoints/system.py index 891b3713..09b031cf 100644 --- a/app/api/endpoints/system.py +++ b/app/api/endpoints/system.py @@ -138,7 +138,7 @@ def set_setting(key: str, value: Union[list, dict, bool, int, str] = None, @router.get("/message", summary="实时消息") -def get_message(token: str): +def get_message(token: str, role: str = "sys"): """ 实时获取系统消息,返回格式为SSE """ @@ -152,7 +152,7 @@ def get_message(token: str): def event_generator(): while True: - detail = message.get() + detail = message.get(role) yield 'data: %s\n\n' % (detail or '') time.sleep(3) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 79d6e034..fc9ee27a 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -16,6 +16,7 @@ from app.core.event import EventManager from app.core.meta import MetaBase from app.core.module import ModuleManager from app.db.message_oper import MessageOper +from app.helper.message import MessageHelper from app.log import logger from app.schemas import TransferInfo, TransferTorrent, ExistMediaInfo, DownloadingTorrent, CommingMessage, Notification, \ WebhookEventInfo, TmdbEpisode @@ -35,6 +36,7 @@ class ChainBase(metaclass=ABCMeta): self.modulemanager = ModuleManager() self.eventmanager = EventManager() self.messageoper = MessageOper() + self.messagehelper = MessageHelper() @staticmethod def load_cache(filename: str) -> Any: @@ -420,6 +422,7 @@ class ChainBase(metaclass=ABCMeta): "userid": message.userid, }) # 保存消息 + self.messagehelper.put(message, role="user") self.messageoper.add(channel=message.channel, mtype=message.mtype, title=message.title, text=message.text, image=message.image, link=message.link, diff --git a/app/helper/message.py b/app/helper/message.py index d344faad..57983f00 100644 --- a/app/helper/message.py +++ b/app/helper/message.py @@ -1,19 +1,42 @@ +import json import queue +from typing import Any, Union, Optional +from app.schemas import Notification from app.utils.singleton import Singleton class MessageHelper(metaclass=Singleton): """ - 消息队列管理器 + 消息队列管理器,包括系统消息和用户消息 """ def __init__(self): - self.queue = queue.Queue() + self.sys_queue = queue.Queue() + self.user_queue = queue.Queue() - def put(self, message: str): - self.queue.put(message) + def put(self, message: Union[str, Notification], role: str = "sys"): + """ + 存消息 + :param message: 消息 + :param role: 消息通道 sys/user + """ + if role == "sys": + self.sys_queue.put(message) + else: + if isinstance(message, Notification): + self.user_queue.put(json.dumps(message.dict())) + else: + self.user_queue.put(message) - def get(self): - if not self.queue.empty(): - return self.queue.get(block=False) + def get(self, role: str = "sys") -> Optional[str]: + """ + 取消息 + :param role: 消息通道 sys/user + """ + if role == "sys": + if not self.sys_queue.empty(): + return self.sys_queue.get(block=False) + else: + if not self.user_queue.empty(): + return self.user_queue.get(block=False) return None diff --git a/app/modules/filetransfer/__init__.py b/app/modules/filetransfer/__init__.py index 3927354d..6a28c511 100644 --- a/app/modules/filetransfer/__init__.py +++ b/app/modules/filetransfer/__init__.py @@ -43,7 +43,7 @@ class FileTransferModule(_ModuleBase): continue download_path = Path(path) if not download_path.exists(): - return False, f"目录 {download_path} 不存在" + return False, f"下载目录 {download_path} 不存在" download_paths.append(path) # 下载目录的设备ID download_devids = [Path(path).stat().st_dev for path in download_paths] @@ -54,7 +54,7 @@ class FileTransferModule(_ModuleBase): for path in settings.LIBRARY_PATHS: library_path = Path(path) if not library_path.exists(): - return False, f"目录不存在:{library_path}" + return False, f"媒体库目录不存在:{library_path}" if settings.DOWNLOADER_MONITOR and settings.TRANSFER_TYPE == "link": if library_path.stat().st_dev not in download_devids: return False, f"媒体库目录 {library_path} " \ diff --git a/app/schemas/message.py b/app/schemas/message.py index 051357e1..6cb0d8ce 100644 --- a/app/schemas/message.py +++ b/app/schemas/message.py @@ -38,6 +38,17 @@ class Notification(BaseModel): # 用户ID userid: Optional[Union[str, int]] = None + def dict(self): + return { + "channel": self.channel.value if self.channel else None, + "mtype": self.mtype.value if self.mtype else None, + "title": self.title, + "text": self.text, + "image": self.image, + "link": self.link, + "userid": self.userid + } + class NotificationSwitch(BaseModel): """