From c755dc9b85412d0963b81588ec714d87d278974f Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 14 Mar 2024 19:15:13 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BC=98=E5=8C=96=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=20feat=20=E6=B6=88=E6=81=AF=E8=90=BD?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/media.py | 14 -------- app/chain/__init__.py | 16 ++++++--- app/db/message_oper.py | 57 ++++++++++++++++++++++++++++++ app/db/models/downloadhistory.py | 1 + app/db/models/message.py | 36 +++++++++++++++++++ app/modules/douban/__init__.py | 6 +++- app/modules/themoviedb/__init__.py | 8 +++-- app/modules/themoviedb/tmdbapi.py | 13 +++++-- 8 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 app/db/message_oper.py create mode 100644 app/db/models/message.py diff --git a/app/api/endpoints/media.py b/app/api/endpoints/media.py index 7d9d5443..7d107d71 100644 --- a/app/api/endpoints/media.py +++ b/app/api/endpoints/media.py @@ -113,20 +113,6 @@ def media_info(mediaid: str, type_name: str, doubanid = mediaid[7:] if not tmdbid and not doubanid: return schemas.MediaInfo() - if settings.RECOGNIZE_SOURCE == "themoviedb": - if not tmdbid and doubanid: - tmdbinfo = MediaChain().get_tmdbinfo_by_doubanid(doubanid=doubanid, mtype=mtype) - if tmdbinfo: - tmdbid = tmdbinfo.get("id") - else: - return schemas.MediaInfo() - else: - if not doubanid and tmdbid: - doubaninfo = MediaChain().get_doubaninfo_by_tmdbid(tmdbid=tmdbid, mtype=mtype) - if doubaninfo: - doubanid = doubaninfo.get("id") - else: - return schemas.MediaInfo() mediainfo = MediaChain().recognize_media(tmdbid=tmdbid, doubanid=doubanid, mtype=mtype) if mediainfo: MediaChain().obtain_images(mediainfo) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index e0bcd08e..79d6e034 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -15,6 +15,7 @@ from app.core.context import MediaInfo, TorrentInfo 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.log import logger from app.schemas import TransferInfo, TransferTorrent, ExistMediaInfo, DownloadingTorrent, CommingMessage, Notification, \ WebhookEventInfo, TmdbEpisode @@ -33,6 +34,7 @@ class ChainBase(metaclass=ABCMeta): """ self.modulemanager = ModuleManager() self.eventmanager = EventManager() + self.messageoper = MessageOper() @staticmethod def load_cache(filename: str) -> Any: @@ -403,6 +405,10 @@ class ChainBase(metaclass=ABCMeta): :param message: 消息体 :return: 成功或失败 """ + logger.info(f"发送消息:channel={message.channel}," + f"title={message.title}, " + f"text={message.text}," + f"userid={message.userid}") # 发送事件 self.eventmanager.send_event(etype=EventType.NoticeMessage, data={ @@ -413,10 +419,12 @@ class ChainBase(metaclass=ABCMeta): "image": message.image, "userid": message.userid, }) - logger.info(f"发送消息:channel={message.channel}," - f"title={message.title}, " - f"text={message.text}," - f"userid={message.userid}") + # 保存消息 + self.messageoper.add(channel=message.channel, mtype=message.mtype, + title=message.title, text=message.text, + image=message.image, link=message.link, + userid=message.userid, action=1) + # 发送 self.run_module("post_message", message=message) def post_medias_message(self, message: Notification, medias: List[MediaInfo]) -> Optional[bool]: diff --git a/app/db/message_oper.py b/app/db/message_oper.py new file mode 100644 index 00000000..b296b7de --- /dev/null +++ b/app/db/message_oper.py @@ -0,0 +1,57 @@ +import time +from typing import Optional, Union + +from sqlalchemy.orm import Session + +from app.db import DbOper +from app.db.models.message import Message +from app.schemas import MessageChannel, NotificationType + + +class MessageOper(DbOper): + """ + 消息数据管理 + """ + + def __init__(self, db: Session = None): + super().__init__(db) + + def add(self, + channel: MessageChannel = None, + mtype: NotificationType = None, + title: str = None, + text: str = None, + image: str = None, + link: str = None, + userid: str = None, + action: int = 1, + **kwargs): + """ + 新增媒体服务器数据 + :param channel: 消息渠道 + :param mtype: 消息类型 + :param title: 标题 + :param text: 文本内容 + :param image: 图片 + :param link: 链接 + :param userid: 用户ID + :param action: 消息方向:0-接收息,1-发送消息 + """ + kwargs.update({ + "channel": channel.value if channel else '', + "mtype": mtype.value if mtype else '', + "title": title, + "text": text, + "image": image, + "link": link, + "userid": userid, + "action": action, + "reg_time": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + }) + Message(**kwargs).create(self._db) + + def list_by_page(self, page: int = 1, count: int = 30) -> Optional[str]: + """ + 获取媒体服务器数据ID + """ + return Message.list_by_page(self._db, page, count) diff --git a/app/db/models/downloadhistory.py b/app/db/models/downloadhistory.py index 1fd5c167..27d5c94a 100644 --- a/app/db/models/downloadhistory.py +++ b/app/db/models/downloadhistory.py @@ -200,6 +200,7 @@ class DownloadFiles(Base): result = db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all() return list(result) + @staticmethod @db_update def delete_by_fullpath(db: Session, fullpath: str): db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath, diff --git a/app/db/models/message.py b/app/db/models/message.py new file mode 100644 index 00000000..a14760d8 --- /dev/null +++ b/app/db/models/message.py @@ -0,0 +1,36 @@ +from sqlalchemy import Column, Integer, String, Sequence +from sqlalchemy.orm import Session + +from app.db import db_query, Base + + +class Message(Base): + """ + 消息表 + """ + id = Column(Integer, Sequence('id'), primary_key=True, index=True) + # 消息渠道 + channel = Column(String, nullable=False) + # 消息类型 + mtype = Column(String, nullable=False) + # 标题 + title = Column(String) + # 文本内容 + text = Column(String) + # 图片 + image = Column(String) + # 链接 + link = Column(String) + # 用户ID + userid = Column(String) + # 登记时间 + reg_time = Column(String) + # 消息方向:0-接收息,1-发送消息 + action = Column(Integer) + + @staticmethod + @db_query + def list_by_page(db: Session, page: int = 1, count: int = 30): + result = db.query(Message).order_by(Message.reg_time.desc()).offset((page - 1) * count).limit( + count).all() + return list(result) diff --git a/app/modules/douban/__init__.py b/app/modules/douban/__init__.py index ee7639c5..4eea1c7c 100644 --- a/app/modules/douban/__init__.py +++ b/app/modules/douban/__init__.py @@ -57,7 +57,11 @@ class DoubanModule(_ModuleBase): :param cache: 是否使用缓存 :return: 识别的媒体信息,包括剧集信息 """ - if settings.RECOGNIZE_SOURCE != "douban": + if not doubanid and not meta: + return None + + if meta and not doubanid \ + and settings.RECOGNIZE_SOURCE != "douban": return None if not meta: diff --git a/app/modules/themoviedb/__init__.py b/app/modules/themoviedb/__init__.py index 307c69ae..27dd58c6 100644 --- a/app/modules/themoviedb/__init__.py +++ b/app/modules/themoviedb/__init__.py @@ -67,7 +67,11 @@ class TheMovieDbModule(_ModuleBase): :param cache: 是否使用缓存 :return: 识别的媒体信息,包括剧集信息 """ - if settings.RECOGNIZE_SOURCE != "themoviedb": + if not tmdbid and not meta: + return None + + if meta and not tmdbid \ + and settings.RECOGNIZE_SOURCE != "themoviedb": return None if not meta: @@ -182,7 +186,7 @@ class TheMovieDbModule(_ModuleBase): :param season: 季号 """ # 搜索 - logger.info(f"开始使用 名称:{name}、年份:{year} 匹配TMDB信息 ...") + logger.info(f"开始使用 名称:{name} 年份:{year} 匹配TMDB信息 ...") info = self.tmdb.match(name=name, year=year, mtype=mtype, diff --git a/app/modules/themoviedb/tmdbapi.py b/app/modules/themoviedb/tmdbapi.py index 9c9fab3a..ef20dd59 100644 --- a/app/modules/themoviedb/tmdbapi.py +++ b/app/modules/themoviedb/tmdbapi.py @@ -189,9 +189,16 @@ class TmdbHelper: season_year, season_number) if not info: - logger.debug( - f"正在识别{mtype.value}:{name}, 年份={year} ...") - info = self.__search_tv_by_name(name, year) + year_range = [year] + if year: + year_range.append(str(int(year) + 1)) + year_range.append(str(int(year) - 1)) + for year in year_range: + logger.debug( + f"正在识别{mtype.value}:{name}, 年份={year} ...") + info = self.__search_tv_by_name(name, year) + if info: + break if info: info['media_type'] = MediaType.TV # 返回