add 搜索结果保存与查询
This commit is contained in:
parent
f77164f90a
commit
6f3209f95d
@ -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,
|
||||
|
@ -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]:
|
||||
"""
|
||||
|
@ -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]]:
|
||||
"""
|
||||
|
@ -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]]:
|
||||
|
@ -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()
|
||||
|
@ -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):
|
||||
"""
|
||||
|
@ -34,6 +34,8 @@ class EventType(Enum):
|
||||
class SystemConfigKey(Enum):
|
||||
# 用户已安装的插件
|
||||
UserInstalledPlugins = "UserInstalledPlugins"
|
||||
# 搜索结果
|
||||
SearchResults = "SearchResults"
|
||||
|
||||
|
||||
# 站点框架
|
||||
|
Loading…
x
Reference in New Issue
Block a user