From 6f3209f95df75e9b22083b7dfc72c2234cc72c02 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 8 Jul 2023 13:53:40 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=B8=8E=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/search.py | 9 +++++++++ app/chain/douban.py | 4 ++-- app/chain/media.py | 4 ++-- app/chain/search.py | 25 ++++++++++++++++++++----- app/chain/subscribe.py | 2 +- app/core/context.py | 19 ++----------------- app/schemas/types.py | 2 ++ 7 files changed, 38 insertions(+), 27 deletions(-) diff --git a/app/api/endpoints/search.py b/app/api/endpoints/search.py index e754274c..d15a22bd 100644 --- a/app/api/endpoints/search.py +++ b/app/api/endpoints/search.py @@ -12,6 +12,15 @@ from app.schemas.types import MediaType router = APIRouter() +@router.get("/last", summary="查询搜索结果", response_model=List[schemas.Context]) +async def search_latest(_: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 查询搜索结果 + """ + torrents = SearchChain().last_search_results() + return [torrent.to_dict() for torrent in torrents] + + @router.get("/media/{mediaid}", summary="精确搜索资源", response_model=List[schemas.Context]) async def search_by_tmdbid(mediaid: str, mtype: str = None, diff --git a/app/chain/douban.py b/app/chain/douban.py index 74346aa8..b3b7370d 100644 --- a/app/chain/douban.py +++ b/app/chain/douban.py @@ -46,10 +46,10 @@ class DoubanChain(ChainBase): mediainfo: MediaInfo = self.recognize_media(meta=meta) if not mediainfo: logger.warn(f'{meta.name} 未识别到TMDB媒体信息') - return Context(meta=meta, mediainfo=MediaInfo(douban_info=doubaninfo)) + return Context(meta_info=meta, media_info=MediaInfo(douban_info=doubaninfo)) logger.info(f'{doubanid} 识别到媒体信息:{mediainfo.type.value} {mediainfo.title_year}{meta.season}') mediainfo.set_douban_info(doubaninfo) - return Context(meta=meta, mediainfo=mediainfo) + return Context(meta_info=meta, media_info=mediainfo) def movie_top250(self, page: int = 1, count: int = 30) -> List[dict]: """ diff --git a/app/chain/media.py b/app/chain/media.py index de06b52a..d85775b3 100644 --- a/app/chain/media.py +++ b/app/chain/media.py @@ -28,12 +28,12 @@ class MediaChain(ChainBase): mediainfo: MediaInfo = self.recognize_media(meta=metainfo) if not mediainfo: logger.warn(f'{title} 未识别到媒体信息') - return Context(meta=metainfo) + return Context(meta_info=metainfo) logger.info(f'{title} 识别到媒体信息:{mediainfo.type.value} {mediainfo.title_year}') # 更新媒体图片 self.obtain_images(mediainfo=mediainfo) # 返回上下文 - return Context(meta=metainfo, mediainfo=mediainfo, title=title, subtitle=subtitle) + return Context(meta_info=metainfo, media_info=mediainfo) def search(self, title: str) -> Tuple[MetaBase, List[MediaInfo]]: """ diff --git a/app/chain/search.py b/app/chain/search.py index d3c8b6d2..b5c5a19c 100644 --- a/app/chain/search.py +++ b/app/chain/search.py @@ -1,3 +1,4 @@ +import pickle from concurrent.futures import ThreadPoolExecutor, as_completed from datetime import datetime from typing import Dict @@ -8,11 +9,12 @@ from app.core.config import settings from app.core.context import Context from app.core.context import MediaInfo, TorrentInfo from app.core.metainfo import MetaInfo +from app.db.systemconfig_oper import SystemConfigOper from app.helper.progress import ProgressHelper from app.helper.sites import SitesHelper from app.log import logger from app.schemas import NotExistMediaInfo -from app.schemas.types import MediaType, ProgressKey +from app.schemas.types import MediaType, ProgressKey, SystemConfigKey from app.utils.string import StringUtils @@ -25,6 +27,7 @@ class SearchChain(ChainBase): super().__init__() self.siteshelper = SitesHelper() self.progress = ProgressHelper() + self.systemconfig = SystemConfigOper() def search_by_tmdbid(self, tmdbid: int, mtype: MediaType = None) -> Optional[List[Context]]: """ @@ -36,7 +39,10 @@ class SearchChain(ChainBase): if not mediainfo: logger.error(f'{tmdbid} 媒体信息识别失败!') return None - return self.process(mediainfo=mediainfo) + results = self.process(mediainfo=mediainfo) + # 保存眲结果 + self.systemconfig.set(SystemConfigKey.SearchResults, pickle.dumps(results)) + return results def search_by_title(self, title: str) -> List[TorrentInfo]: """ @@ -47,6 +53,15 @@ class SearchChain(ChainBase): # 搜索 return self.__search_all_sites(keyword=title) + def last_search_results(self) -> List[Context]: + """ + 获取上次搜索结果 + """ + results = self.systemconfig.get(SystemConfigKey.SearchResults) + if not results: + return [] + return pickle.loads(results) + def browse(self, domain: str, keyword: str = None) -> List[TorrentInfo]: """ 浏览站点首页内容 @@ -168,9 +183,9 @@ class SearchChain(ChainBase): _match_torrents = torrents logger.info(f"匹配完成,共匹配到 {len(_match_torrents)} 个资源") # 组装上下文返回 - return [Context(meta=MetaInfo(title=torrent.title, subtitle=torrent.description), - mediainfo=mediainfo, - torrentinfo=torrent) for torrent in _match_torrents] + return [Context(meta_info=MetaInfo(title=torrent.title, subtitle=torrent.description), + media_info=mediainfo, + torrent_info=torrent) for torrent in _match_torrents] def __search_all_sites(self, mediainfo: Optional[MediaInfo] = None, keyword: str = None) -> Optional[List[TorrentInfo]]: diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index 103cb680..0012015c 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -304,7 +304,7 @@ class SubscribeChain(ChainBase): logger.warn(f'未识别到媒体信息,标题:{torrent.title}') continue # 上下文 - context = Context(meta=meta, mediainfo=mediainfo, torrentinfo=torrent) + context = Context(meta_info=meta, media_info=mediainfo, torrent_info=torrent) self._torrents_cache[domain].append(context) # 从缓存中匹配订阅 self.match() diff --git a/app/core/context.py b/app/core/context.py index 03b7fa74..162ebec9 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -478,25 +478,10 @@ class Context: # 识别信息 meta_info: MetaBase = None - # 种子信息 - torrent_info: TorrentInfo = None # 媒体信息 media_info: MediaInfo = None - - def __init__(self, - meta: MetaBase = None, - mediainfo: MediaInfo = None, - torrentinfo: TorrentInfo = None, - **kwargs): - if meta: - self.meta_info = meta - if mediainfo: - self.media_info = mediainfo - if torrentinfo: - self.torrent_info = torrentinfo - if kwargs: - for k, v in kwargs.items(): - setattr(self, k, v) + # 种子信息 + torrent_info: TorrentInfo = None def to_dict(self): """ diff --git a/app/schemas/types.py b/app/schemas/types.py index 9519b28c..6e87b0d3 100644 --- a/app/schemas/types.py +++ b/app/schemas/types.py @@ -34,6 +34,8 @@ class EventType(Enum): class SystemConfigKey(Enum): # 用户已安装的插件 UserInstalledPlugins = "UserInstalledPlugins" + # 搜索结果 + SearchResults = "SearchResults" # 站点框架