diff --git a/app/actions/add_download.py b/app/actions/add_download.py index 3b6501bf..c25eb922 100644 --- a/app/actions/add_download.py +++ b/app/actions/add_download.py @@ -59,6 +59,7 @@ class AddDownloadAction(BaseAction): 将上下文中的torrents添加到下载任务中 """ params = AddDownloadParams(**params) + _started = False for t in context.torrents: if global_vars.is_workflow_stopped(workflow_id): break @@ -96,6 +97,7 @@ class AddDownloadAction(BaseAction): logger.warning(f"{t.meta_info.title} 第 {t.meta_info.begin_season} 季第 {t.meta_info.episode_list} 集已存在,跳过") continue + _started = True did = self.downloadchain.download_single(context=t, downloader=params.downloader, save_path=params.save_path, @@ -104,14 +106,14 @@ class AddDownloadAction(BaseAction): self._added_downloads.append(did) # 保存缓存 self.save_cache(workflow_id, cache_key) - else: - self._has_error = True if self._added_downloads: logger.info(f"已添加 {len(self._added_downloads)} 个下载任务") context.downloads.extend( [DownloadTask(download_id=did, downloader=params.downloader) for did in self._added_downloads] ) + elif _started: + self._has_error = True self.job_done(f"已添加 {len(self._added_downloads)} 个下载任务") return context diff --git a/app/actions/add_subscribe.py b/app/actions/add_subscribe.py index 8900e05e..300aa1b6 100644 --- a/app/actions/add_subscribe.py +++ b/app/actions/add_subscribe.py @@ -50,6 +50,7 @@ class AddSubscribeAction(BaseAction): """ 将medias中的信息添加订阅,如果订阅不存在的话 """ + _started = False for media in context.medias: if global_vars.is_workflow_stopped(workflow_id): break @@ -64,6 +65,7 @@ class AddSubscribeAction(BaseAction): logger.info(f"{media.title} 已存在订阅") continue # 添加订阅 + _started = True sid, message = self.subscribechain.add(mtype=mediainfo.type, title=mediainfo.title, year=mediainfo.year, @@ -76,13 +78,13 @@ class AddSubscribeAction(BaseAction): self._added_subscribes.append(sid) # 保存缓存 self.save_cache(workflow_id, cache_key) - else: - self._has_error = True if self._added_subscribes: logger.info(f"已添加 {len(self._added_subscribes)} 个订阅") for sid in self._added_subscribes: context.subscribes.append(self.subscribeoper.get(sid)) + elif _started: + self._has_error = True self.job_done(f"已添加 {len(self._added_subscribes)} 个订阅") return context diff --git a/app/actions/fetch_medias.py b/app/actions/fetch_medias.py index bc028b90..093d849a 100644 --- a/app/actions/fetch_medias.py +++ b/app/actions/fetch_medias.py @@ -28,8 +28,8 @@ class FetchMediasAction(BaseAction): """ _inner_sources = [] - _medias = [] + _has_error = False def __init__(self, action_id: str): super().__init__(action_id) @@ -115,7 +115,7 @@ class FetchMediasAction(BaseAction): @property def success(self) -> bool: - return True if self._medias else False + return self._has_error def __get_source(self, source: str): """ @@ -131,37 +131,41 @@ class FetchMediasAction(BaseAction): 获取媒体数据,填充到medias """ params = FetchMediasParams(**params) - if params.source_type == "ranking": - for name in params.sources: - if global_vars.is_workflow_stopped(workflow_id): - break - source = self.__get_source(name) - if not source: - continue - logger.info(f"获取媒体数据 {source} ...") - results = [] - if source.get("func"): - results = source['func']() - else: - # 调用内部API获取数据 - api_url = f"http://127.0.0.1:{settings.PORT}/api/v1/{source['api_path']}?token={settings.API_TOKEN}" - res = RequestUtils(timeout=15).post_res(api_url) - if res: - results = res.json() - if results: - logger.info(f"{name} 获取到 {len(results)} 条数据") - self._medias.extend([MediaInfo(**r) for r in results]) - else: - logger.error(f"{name} 获取数据失败") - else: - # 调用内部API获取数据 - api_url = f"http://127.0.0.1:{settings.PORT}{params.api_path}?token={settings.API_TOKEN}" - res = RequestUtils(timeout=15).post_res(api_url) - if res: - results = res.json() - if results: - logger.info(f"{params.api_path} 获取到 {len(results)} 条数据") - self._medias.extend([MediaInfo(**r) for r in results]) + try: + if params.source_type == "ranking": + for name in params.sources: + if global_vars.is_workflow_stopped(workflow_id): + break + source = self.__get_source(name) + if not source: + continue + logger.info(f"获取媒体数据 {source} ...") + results = [] + if source.get("func"): + results = source['func']() + else: + # 调用内部API获取数据 + api_url = f"http://127.0.0.1:{settings.PORT}/api/v1/{source['api_path']}?token={settings.API_TOKEN}" + res = RequestUtils(timeout=15).post_res(api_url) + if res: + results = res.json() + if results: + logger.info(f"{name} 获取到 {len(results)} 条数据") + self._medias.extend([MediaInfo(**r) for r in results]) + else: + logger.error(f"{name} 获取数据失败") + else: + # 调用内部API获取数据 + api_url = f"http://127.0.0.1:{settings.PORT}{params.api_path}?token={settings.API_TOKEN}" + res = RequestUtils(timeout=15).post_res(api_url) + if res: + results = res.json() + if results: + logger.info(f"{params.api_path} 获取到 {len(results)} 条数据") + self._medias.extend([MediaInfo(**r) for r in results]) + except Exception as e: + logger.error(f"获取媒体数据失败: {e}") + self._has_error = True if self._medias: context.medias.extend(self._medias) diff --git a/app/actions/scrape_file.py b/app/actions/scrape_file.py index eb9d8e35..ee08f883 100644 --- a/app/actions/scrape_file.py +++ b/app/actions/scrape_file.py @@ -52,6 +52,8 @@ class ScrapeFileAction(BaseAction): """ 刮削fileitems中的所有文件 """ + # 失败次数 + _failed_count = 0 for fileitem in context.fileitems: if global_vars.is_workflow_stopped(workflow_id): break @@ -62,12 +64,12 @@ class ScrapeFileAction(BaseAction): # 检查缓存 cache_key = f"{fileitem.path}" if self.check_cache(workflow_id, cache_key): - logger.info(f"{fileitem.path} 已刮削,跳过") + logger.info(f"{fileitem.path} 已刮削过,跳过") continue meta = MetaInfoPath(Path(fileitem.path)) mediainfo = self.mediachain.recognize_media(meta) if not mediainfo: - self._has_error = True + _failed_count += 1 logger.info(f"{fileitem.path} 未识别到媒体信息,无法刮削") continue self.mediachain.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo) @@ -75,5 +77,8 @@ class ScrapeFileAction(BaseAction): # 保存缓存 self.save_cache(workflow_id, cache_key) - self.job_done(f"成功刮削了 {len(self._scraped_files)} 个文件") + if not self._scraped_files and _failed_count: + self._has_error = True + + self.job_done(f"成功刮削 {len(self._scraped_files)} 个文件,失败 {_failed_count} 个") return context diff --git a/app/actions/transfer_file.py b/app/actions/transfer_file.py index cd753767..4ae0f4e8 100644 --- a/app/actions/transfer_file.py +++ b/app/actions/transfer_file.py @@ -68,6 +68,8 @@ class TransferFileAction(BaseAction): return True params = TransferFileParams(**params) + # 失败次数 + _failed_count = 0 if params.source == "downloads": # 从下载任务中整理文件 for download in context.downloads: @@ -92,7 +94,7 @@ class TransferFileAction(BaseAction): logger.info(f"开始整理文件 {download.path} ...") state, errmsg = self.transferchain.do_transfer(fileitem, background=False) if not state: - self._has_error = True + _failed_count += 1 logger.error(f"整理文件 {download.path} 失败: {errmsg}") continue logger.info(f"整理文件 {download.path} 完成") @@ -116,7 +118,7 @@ class TransferFileAction(BaseAction): state, errmsg = self.transferchain.do_transfer(fileitem, background=False, continue_callback=check_continue) if not state: - self._has_error = True + _failed_count += 1 logger.error(f"整理文件 {fileitem.path} 失败: {errmsg}") continue logger.info(f"整理文件 {fileitem.path} 完成") @@ -128,6 +130,8 @@ class TransferFileAction(BaseAction): if self._fileitems: context.fileitems.extend(self._fileitems) + elif _failed_count: + self._has_error = True - self.job_done() + self.job_done(f"整理成功 {len(self._fileitems)} 个文件,失败 {_failed_count} 个") return context