From c05223846f4ff73300eca65ee1d5d3ec92355baf Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 2 Jun 2024 21:09:15 +0800 Subject: [PATCH] fix api --- app/api/endpoints/download.py | 8 ++++++-- app/api/endpoints/search.py | 6 ++++-- app/chain/search.py | 16 +++++++++++++--- app/modules/indexer/spider.py | 4 +++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/api/endpoints/download.py b/app/api/endpoints/download.py index ab4c7920..c2dbe9b0 100644 --- a/app/api/endpoints/download.py +++ b/app/api/endpoints/download.py @@ -46,7 +46,9 @@ def download( torrent_info=torrentinfo ) did = DownloadChain().download_single(context=context, username=current_user.name) - return schemas.Response(success=True if did else False, data={ + if not did: + return schemas.Response(success=False, message="任务添加失败") + return schemas.Response(success=True, data={ "download_id": did }) @@ -74,7 +76,9 @@ def add( torrent_info=torrentinfo ) did = DownloadChain().download_single(context=context, username=current_user.name) - return schemas.Response(success=True if did else False, data={ + if not did: + return schemas.Response(success=False, message="任务添加失败") + return schemas.Response(success=True, data={ "download_id": did }) diff --git a/app/api/endpoints/search.py b/app/api/endpoints/search.py index 24a2330e..dae2d23a 100644 --- a/app/api/endpoints/search.py +++ b/app/api/endpoints/search.py @@ -85,7 +85,7 @@ def search_by_id(mediaid: str, return schemas.Response(success=True, data=[torrent.to_dict() for torrent in torrents]) -@router.get("/title", summary="模糊搜索资源", response_model=List[schemas.TorrentInfo]) +@router.get("/title", summary="模糊搜索资源", response_model=schemas.Response) async def search_by_title(keyword: str = None, page: int = 0, site: int = None, @@ -94,4 +94,6 @@ async def search_by_title(keyword: str = None, 根据名称模糊搜索站点资源,支持分页,关键词为空是返回首页资源 """ torrents = SearchChain().search_by_title(title=keyword, page=page, site=site) - return [torrent.to_dict() for torrent in torrents] + if not torrents: + return schemas.Response(success=False, message="未搜索到任何资源") + return schemas.Response(success=True, data=[torrent.to_dict() for torrent in torrents]) diff --git a/app/chain/search.py b/app/chain/search.py index cd62e6c0..78e71d40 100644 --- a/app/chain/search.py +++ b/app/chain/search.py @@ -53,12 +53,12 @@ class SearchChain(ChainBase): } } results = self.process(mediainfo=mediainfo, area=area, no_exists=no_exists) - # 保存眲结果 + # 保存结果 bytes_results = pickle.dumps(results) self.systemconfig.set(SystemConfigKey.SearchResults, bytes_results) return results - def search_by_title(self, title: str, page: int = 0, site: int = None) -> List[TorrentInfo]: + def search_by_title(self, title: str, page: int = 0, site: int = None) -> List[Context]: """ 根据标题搜索资源,不识别不过滤,直接返回站点内容 :param title: 标题,为空时返回所有站点首页内容 @@ -70,7 +70,17 @@ class SearchChain(ChainBase): else: logger.info(f'开始浏览资源,站点:{site} ...') # 搜索 - return self.__search_all_sites(keywords=[title], sites=[site] if site else None, page=page) or [] + torrents = self.__search_all_sites(keywords=[title], sites=[site] if site else None, page=page) or [] + if not torrents: + logger.warn(f'{title} 未搜索到资源') + return [] + # 组装上下文 + contexts = [Context(meta_info=MetaInfo(title=torrent.title, subtitle=torrent.description), + torrent_info=torrent) for torrent in torrents] + # 保存结果 + bytes_results = pickle.dumps(contexts) + self.systemconfig.set(SystemConfigKey.SearchResults, bytes_results) + return contexts def last_search_results(self) -> List[Context]: """ diff --git a/app/modules/indexer/spider.py b/app/modules/indexer/spider.py index 19ecbb0f..f989ee78 100644 --- a/app/modules/indexer/spider.py +++ b/app/modules/indexer/spider.py @@ -674,7 +674,9 @@ class TorrentSpider: try: args = filter_item.get("args") if method_name == "re_search" and isinstance(args, list): - text = re.search(r"%s" % args[0], text).group(args[-1]) + rematch = re.search(r"%s" % args[0], text) + if rematch: + text = rematch.group(args[-1]) elif method_name == "split" and isinstance(args, list): text = text.split(r"%s" % args[0])[args[-1]] elif method_name == "replace" and isinstance(args, list):