diff --git a/app/actions/add_download.py b/app/actions/add_download.py index f8bcbb57..aea4082f 100644 --- a/app/actions/add_download.py +++ b/app/actions/add_download.py @@ -26,37 +26,31 @@ class AddDownloadAction(BaseAction): 添加下载资源 """ - # 已添加的下载 - _added_downloads = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.downloadchain = DownloadChain() - self.mediachain = MediaChain() self._added_downloads = [] self._has_error = False @classmethod @property - def name(cls) -> str: # noqa + def name(cls) -> str: # noqa return "添加下载" @classmethod @property - def description(cls) -> str: # noqa + def description(cls) -> str: # noqa return "根据资源列表添加下载任务" @classmethod @property - def data(cls) -> dict: # noqa + def data(cls) -> dict: # noqa return AddDownloadParams().dict() @property def success(self) -> bool: return not self._has_error - def execute(self, workflow_id: int, params: dict, context: ActionContext) -> ActionContext: + def execute(self, workflow_id: int, params: dict, context: ActionContext) -> ActionContext: """ 将上下文中的torrents添加到下载任务中 """ @@ -73,13 +67,13 @@ class AddDownloadAction(BaseAction): if not t.meta_info: t.meta_info = MetaInfo(title=t.torrent_info.title, subtitle=t.torrent_info.description) if not t.media_info: - t.media_info = self.mediachain.recognize_media(meta=t.meta_info) + t.media_info = MediaChain().recognize_media(meta=t.meta_info) if not t.media_info: self._has_error = True logger.warning(f"{t.torrent_info.title} 未识别到媒体信息,无法下载") continue if params.only_lack: - exists_info = self.downloadchain.media_exists(t.media_info) + exists_info = DownloadChain().media_exists(t.media_info) if exists_info: if t.media_info.type == MediaType.MOVIE: # 电影 @@ -96,14 +90,15 @@ class AddDownloadAction(BaseAction): exists_episodes = exists_seasons.get(t.meta_info.begin_season) if exists_episodes: if set(t.meta_info.episode_list).issubset(exists_episodes): - logger.warning(f"{t.meta_info.title} 第 {t.meta_info.begin_season} 季第 {t.meta_info.episode_list} 集已存在,跳过") + 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, - label=params.labels) + did = DownloadChain().download_single(context=t, + downloader=params.downloader, + save_path=params.save_path, + label=params.labels) if did: self._added_downloads.append(did) # 保存缓存 diff --git a/app/actions/add_subscribe.py b/app/actions/add_subscribe.py index 8c4ac8fa..84d30548 100644 --- a/app/actions/add_subscribe.py +++ b/app/actions/add_subscribe.py @@ -19,29 +19,24 @@ class AddSubscribeAction(BaseAction): 添加订阅 """ - _added_subscribes = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.subscribechain = SubscribeChain() - self.subscribeoper = SubscribeOper() self._added_subscribes = [] self._has_error = False @classmethod @property - def name(cls) -> str: # noqa + def name(cls) -> str: # noqa return "添加订阅" @classmethod @property - def description(cls) -> str: # noqa + def description(cls) -> str: # noqa return "根据媒体列表添加订阅" @classmethod @property - def data(cls) -> dict: # noqa + def data(cls) -> dict: # noqa return AddSubscribeParams().dict() @property @@ -63,19 +58,20 @@ class AddSubscribeAction(BaseAction): continue mediainfo = MediaInfo() mediainfo.from_dict(media.dict()) - if self.subscribechain.exists(mediainfo): + subscribechain = SubscribeChain() + if subscribechain.exists(mediainfo): logger.info(f"{media.title} 已存在订阅") continue # 添加订阅 _started = True - sid, message = self.subscribechain.add(mtype=mediainfo.type, - title=mediainfo.title, - year=mediainfo.year, - tmdbid=mediainfo.tmdb_id, - season=mediainfo.season, - doubanid=mediainfo.douban_id, - bangumiid=mediainfo.bangumi_id, - username=settings.SUPERUSER) + sid, message = subscribechain.add(mtype=mediainfo.type, + title=mediainfo.title, + year=mediainfo.year, + tmdbid=mediainfo.tmdb_id, + season=mediainfo.season, + doubanid=mediainfo.douban_id, + bangumiid=mediainfo.bangumi_id, + username=settings.SUPERUSER) if sid: self._added_subscribes.append(sid) # 保存缓存 @@ -84,7 +80,7 @@ class AddSubscribeAction(BaseAction): if self._added_subscribes: logger.info(f"已添加 {len(self._added_subscribes)} 个订阅") for sid in self._added_subscribes: - context.subscribes.append(self.subscribeoper.get(sid)) + context.subscribes.append(SubscribeOper().get(sid)) elif _started: self._has_error = True diff --git a/app/actions/fetch_downloads.py b/app/actions/fetch_downloads.py index 3e8c6be4..1fe2f240 100644 --- a/app/actions/fetch_downloads.py +++ b/app/actions/fetch_downloads.py @@ -16,11 +16,8 @@ class FetchDownloadsAction(BaseAction): 获取下载任务 """ - _downloads = [] - def __init__(self, action_id: str): super().__init__(action_id) - self.chain = ActionChain() self._downloads = [] @classmethod @@ -51,7 +48,7 @@ class FetchDownloadsAction(BaseAction): if global_vars.is_workflow_stopped(workflow_id): break logger.info(f"获取下载任务 {download.download_id} 状态 ...") - torrents = self.chain.list_torrents(hashs=[download.download_id]) + torrents = ActionChain().list_torrents(hashs=[download.download_id]) if not torrents: download.completed = True continue diff --git a/app/actions/fetch_medias.py b/app/actions/fetch_medias.py index 53c3907d..a8532e70 100644 --- a/app/actions/fetch_medias.py +++ b/app/actions/fetch_medias.py @@ -27,10 +27,6 @@ class FetchMediasAction(BaseAction): 获取媒体数据 """ - _inner_sources = [] - _medias = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) diff --git a/app/actions/fetch_rss.py b/app/actions/fetch_rss.py index 2e65839a..7d429e6b 100644 --- a/app/actions/fetch_rss.py +++ b/app/actions/fetch_rss.py @@ -29,29 +29,24 @@ class FetchRssAction(BaseAction): 获取RSS资源列表 """ - _rss_torrents = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.rsshelper = RssHelper() - self.chain = ActionChain() self._rss_torrents = [] self._has_error = False @classmethod @property - def name(cls) -> str: # noqa + def name(cls) -> str: # noqa return "获取RSS资源" @classmethod @property - def description(cls) -> str: # noqa + def description(cls) -> str: # noqa return "订阅RSS地址获取资源" @classmethod @property - def data(cls) -> dict: # noqa + def data(cls) -> dict: # noqa return FetchRssParams().dict() @property @@ -74,10 +69,10 @@ class FetchRssAction(BaseAction): if params.ua: headers["User-Agent"] = params.ua - rss_items = self.rsshelper.parse(url=params.url, - proxy=settings.PROXY if params.proxy else None, - timeout=params.timeout, - headers=headers) + rss_items = RssHelper().parse(url=params.url, + proxy=settings.PROXY if params.proxy else None, + timeout=params.timeout, + headers=headers) if rss_items is None or rss_items is False: logger.error(f'RSS地址 {params.url} 请求失败!') self._has_error = True @@ -103,7 +98,7 @@ class FetchRssAction(BaseAction): meta = MetaInfo(title=torrentinfo.title, subtitle=torrentinfo.description) mediainfo = None if params.match_media: - mediainfo = self.chain.recognize_media(meta) + mediainfo = ActionChain().recognize_media(meta) if not mediainfo: logger.warning(f"{torrentinfo.title} 未识别到媒体信息") continue diff --git a/app/actions/fetch_torrents.py b/app/actions/fetch_torrents.py index b14486fc..da3a6ff4 100644 --- a/app/actions/fetch_torrents.py +++ b/app/actions/fetch_torrents.py @@ -29,26 +29,23 @@ class FetchTorrentsAction(BaseAction): 搜索站点资源 """ - _torrents = [] - def __init__(self, action_id: str): super().__init__(action_id) - self.searchchain = SearchChain() self._torrents = [] @classmethod @property - def name(cls) -> str: # noqa + def name(cls) -> str: # noqa return "搜索站点资源" @classmethod @property - def description(cls) -> str: # noqa + def description(cls) -> str: # noqa return "搜索站点种子资源列表" @classmethod @property - def data(cls) -> dict: # noqa + def data(cls) -> dict: # noqa return FetchTorrentsParams().dict() @property @@ -60,9 +57,10 @@ class FetchTorrentsAction(BaseAction): 搜索站点,获取资源列表 """ params = FetchTorrentsParams(**params) + searchchain = SearchChain() if params.search_type == "keyword": # 按关键字搜索 - torrents = self.searchchain.search_by_title(title=params.name, sites=params.sites) + torrents = searchchain.search_by_title(title=params.name, sites=params.sites) for torrent in torrents: if global_vars.is_workflow_stopped(workflow_id): break @@ -74,7 +72,7 @@ class FetchTorrentsAction(BaseAction): continue # 识别媒体信息 if params.match_media: - torrent.media_info = self.searchchain.recognize_media(torrent.meta_info) + torrent.media_info = searchchain.recognize_media(torrent.meta_info) if not torrent.media_info: logger.warning(f"{torrent.torrent_info.title} 未识别到媒体信息") continue @@ -84,10 +82,10 @@ class FetchTorrentsAction(BaseAction): for media in context.medias: if global_vars.is_workflow_stopped(workflow_id): break - torrents = self.searchchain.search_by_id(tmdbid=media.tmdb_id, - doubanid=media.douban_id, - mtype=MediaType(media.type), - sites=params.sites) + torrents = searchchain.search_by_id(tmdbid=media.tmdb_id, + doubanid=media.douban_id, + mtype=MediaType(media.type), + sites=params.sites) for torrent in torrents: self._torrents.append(torrent) diff --git a/app/actions/filter_medias.py b/app/actions/filter_medias.py index ec0086fd..7abddaf6 100644 --- a/app/actions/filter_medias.py +++ b/app/actions/filter_medias.py @@ -22,8 +22,6 @@ class FilterMediasAction(BaseAction): 过滤媒体数据 """ - _medias = [] - def __init__(self, action_id: str): super().__init__(action_id) self._medias = [] diff --git a/app/actions/filter_torrents.py b/app/actions/filter_torrents.py index 0b46cd05..1aa37092 100644 --- a/app/actions/filter_torrents.py +++ b/app/actions/filter_torrents.py @@ -27,12 +27,8 @@ class FilterTorrentsAction(BaseAction): 过滤资源数据 """ - _torrents = [] - def __init__(self, action_id: str): super().__init__(action_id) - self.torrenthelper = TorrentHelper() - self.chain = ActionChain() self._torrents = [] @classmethod @@ -62,7 +58,7 @@ class FilterTorrentsAction(BaseAction): for torrent in context.torrents: if global_vars.is_workflow_stopped(workflow_id): break - if self.torrenthelper.filter_torrent( + if TorrentHelper().filter_torrent( torrent_info=torrent.torrent_info, filter_params={ "quality": params.quality, @@ -73,7 +69,7 @@ class FilterTorrentsAction(BaseAction): "size": params.size } ): - if self.chain.filter_torrents( + if ActionChain().filter_torrents( rule_groups=params.rule_groups, torrent_list=[torrent.torrent_info], mediainfo=torrent.media_info diff --git a/app/actions/invoke_plugin.py b/app/actions/invoke_plugin.py index 5a51f1e0..58578ce5 100644 --- a/app/actions/invoke_plugin.py +++ b/app/actions/invoke_plugin.py @@ -20,8 +20,6 @@ class InvokePluginAction(BaseAction): 调用插件 """ - _success = False - def __init__(self, action_id: str): super().__init__(action_id) self._success = False diff --git a/app/actions/scan_file.py b/app/actions/scan_file.py index 47fa43a4..0701f38f 100644 --- a/app/actions/scan_file.py +++ b/app/actions/scan_file.py @@ -24,12 +24,8 @@ class ScanFileAction(BaseAction): 整理文件 """ - _fileitems = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.storagechain = StorageChain() self._fileitems = [] self._has_error = False @@ -59,12 +55,13 @@ class ScanFileAction(BaseAction): params = ScanFileParams(**params) if not params.storage or not params.directory: return context - fileitem = self.storagechain.get_file_item(params.storage, Path(params.directory)) + storagechain = StorageChain() + fileitem = storagechain.get_file_item(params.storage, Path(params.directory)) if not fileitem: logger.error(f"目录不存在: 【{params.storage}】{params.directory}") self._has_error = True return context - files = self.storagechain.list_files(fileitem, recursion=True) + files = storagechain.list_files(fileitem, recursion=True) for file in files: if global_vars.is_workflow_stopped(workflow_id): break diff --git a/app/actions/scrape_file.py b/app/actions/scrape_file.py index 1353a991..2b4152df 100644 --- a/app/actions/scrape_file.py +++ b/app/actions/scrape_file.py @@ -21,13 +21,8 @@ class ScrapeFileAction(BaseAction): 刮削文件 """ - _scraped_files = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.storagechain = StorageChain() - self.mediachain = MediaChain() self._scraped_files = [] self._has_error = False @@ -61,7 +56,7 @@ class ScrapeFileAction(BaseAction): break if fileitem in self._scraped_files: continue - if not self.storagechain.exists(fileitem): + if not StorageChain().exists(fileitem): continue # 检查缓存 cache_key = f"{fileitem.path}" @@ -69,12 +64,13 @@ class ScrapeFileAction(BaseAction): logger.info(f"{fileitem.path} 已刮削过,跳过") continue meta = MetaInfoPath(Path(fileitem.path)) - mediainfo = self.mediachain.recognize_media(meta) + mediachain = MediaChain() + mediainfo = mediachain.recognize_media(meta) if not mediainfo: _failed_count += 1 logger.info(f"{fileitem.path} 未识别到媒体信息,无法刮削") continue - self.mediachain.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo) + mediachain.scrape_metadata(fileitem=fileitem, meta=meta, mediainfo=mediainfo) self._scraped_files.append(fileitem) # 保存缓存 self.save_cache(workflow_id, cache_key) diff --git a/app/actions/send_message.py b/app/actions/send_message.py index 78d7117d..f3e218c0 100644 --- a/app/actions/send_message.py +++ b/app/actions/send_message.py @@ -22,7 +22,6 @@ class SendMessageAction(BaseAction): def __init__(self, action_id: str): super().__init__(action_id) - self.chain = ActionChain() @classmethod @property @@ -60,7 +59,7 @@ class SendMessageAction(BaseAction): if not params.client: params.client = [""] for client in params.client: - self.chain.post_message( + ActionChain().post_message( Notification( source=client, userid=params.userid, diff --git a/app/actions/transfer_file.py b/app/actions/transfer_file.py index 77a00e31..5963226c 100644 --- a/app/actions/transfer_file.py +++ b/app/actions/transfer_file.py @@ -26,30 +26,24 @@ class TransferFileAction(BaseAction): 整理文件 """ - _fileitems = [] - _has_error = False - def __init__(self, action_id: str): super().__init__(action_id) - self.transferchain = TransferChain() - self.storagechain = StorageChain() - self.transferhis = TransferHistoryOper() self._fileitems = [] self._has_error = False @classmethod @property - def name(cls) -> str: # noqa + def name(cls) -> str: # noqa return "整理文件" @classmethod @property - def description(cls) -> str: # noqa + def description(cls) -> str: # noqa return "整理队列中的文件" @classmethod @property - def data(cls) -> dict: # noqa + def data(cls) -> dict: # noqa return TransferFileParams().dict() @property @@ -72,6 +66,9 @@ class TransferFileAction(BaseAction): params = TransferFileParams(**params) # 失败次数 _failed_count = 0 + storagechain = StorageChain() + transferchain = TransferChain() + transferhis = TransferHistoryOper() if params.source == "downloads": # 从下载任务中整理文件 for download in context.downloads: @@ -85,16 +82,16 @@ class TransferFileAction(BaseAction): if self.check_cache(workflow_id, cache_key): logger.info(f"{download.path} 已整理过,跳过") continue - fileitem = self.storagechain.get_file_item(storage="local", path=Path(download.path)) + fileitem = storagechain.get_file_item(storage="local", path=Path(download.path)) if not fileitem: logger.info(f"文件 {download.path} 不存在") continue - transferd = self.transferhis.get_by_src(fileitem.path, storage=fileitem.storage) + transferd = transferhis.get_by_src(fileitem.path, storage=fileitem.storage) if transferd: # 已经整理过的文件不再整理 continue logger.info(f"开始整理文件 {download.path} ...") - state, errmsg = self.transferchain.do_transfer(fileitem, background=False) + state, errmsg = transferchain.do_transfer(fileitem, background=False) if not state: _failed_count += 1 logger.error(f"整理文件 {download.path} 失败: {errmsg}") @@ -112,13 +109,13 @@ class TransferFileAction(BaseAction): if self.check_cache(workflow_id, cache_key): logger.info(f"{fileitem.path} 已整理过,跳过") continue - transferd = self.transferhis.get_by_src(fileitem.path, storage=fileitem.storage) + transferd = transferhis.get_by_src(fileitem.path, storage=fileitem.storage) if transferd: # 已经整理过的文件不再整理 continue logger.info(f"开始整理文件 {fileitem.path} ...") - state, errmsg = self.transferchain.do_transfer(fileitem, background=False, - continue_callback=check_continue) + state, errmsg = transferchain.do_transfer(fileitem, background=False, + continue_callback=check_continue) if not state: _failed_count += 1 logger.error(f"整理文件 {fileitem.path} 失败: {errmsg}")