From dd38428f030e99adb12f38f187f4db9b123da93d Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 9 Jun 2023 14:36:54 +0800 Subject: [PATCH] add module function alias --- app/api/endpoints/douban.py | 1 - app/api/endpoints/subscribes.py | 2 - app/chain/__init__.py | 82 +++++++++++++++++++++++++- app/chain/common.py | 44 ++++++-------- app/chain/douban_sync.py | 4 +- app/chain/identify.py | 6 +- app/chain/search.py | 7 +-- app/chain/subscribe.py | 16 ++--- app/chain/transfer.py | 14 ++--- app/chain/user_message.py | 36 +++++------ app/chain/webhook_message.py | 4 +- app/modules/__init__.py | 2 +- app/modules/filetransfer/__init__.py | 24 ++++---- app/modules/indexer/__init__.py | 2 +- app/modules/telegram/telegram.py | 8 +-- app/modules/themoviedb/__init__.py | 1 + app/plugins/autosignin/sites/haidan.py | 3 - app/plugins/sitestatistic/__init__.py | 3 +- app/utils/system.py | 8 +-- tests/run.py | 4 +- 20 files changed, 168 insertions(+), 103 deletions(-) diff --git a/app/api/endpoints/douban.py b/app/api/endpoints/douban.py index 6bb0552a..42b60130 100644 --- a/app/api/endpoints/douban.py +++ b/app/api/endpoints/douban.py @@ -20,7 +20,6 @@ def start_douban_chain(): @router.get("/sync", response_model=schemas.Response) async def sync_douban( background_tasks: BackgroundTasks, - db: Session = Depends(get_db), current_user: User = Depends(get_current_active_superuser)): """ 查询所有订阅 diff --git a/app/api/endpoints/subscribes.py b/app/api/endpoints/subscribes.py index 675da066..a7131c65 100644 --- a/app/api/endpoints/subscribes.py +++ b/app/api/endpoints/subscribes.py @@ -92,7 +92,6 @@ async def seerr_subscribe(request: Request, background_tasks: BackgroundTasks, @router.get("/refresh", response_model=schemas.Response) async def refresh_subscribes( - db: Session = Depends(get_db), current_user: User = Depends(get_current_active_superuser)): """ 刷新所有订阅 @@ -108,7 +107,6 @@ async def refresh_subscribes( @router.get("/search", response_model=schemas.Response) async def search_subscribes( - db: Session = Depends(get_db), current_user: User = Depends(get_current_active_superuser)): """ 搜索所有订阅 diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 18feefbc..970b09f6 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -1,10 +1,15 @@ import traceback from abc import abstractmethod -from typing import Optional, Any +from pathlib import Path +from typing import Optional, Any, Tuple, List, Set, Union -from app.core import Context, ModuleManager +from ruamel.yaml import CommentedMap + +from app.core import Context, ModuleManager, MediaInfo, TorrentInfo +from app.core.meta import MetaBase from app.log import logger from app.utils.singleton import AbstractSingleton, Singleton +from app.utils.types import TorrentStatus class ChainBase(AbstractSingleton, metaclass=Singleton): @@ -56,3 +61,76 @@ class ChainBase(AbstractSingleton, metaclass=Singleton): except Exception as err: logger.error(f"运行模块 {method} 出错:{module.__class__.__name__} - {err}\n{traceback.print_exc()}") return result + + def prepare_recognize(self, title: str, + subtitle: str = None) -> Tuple[str, str]: + return self.run_module("prepare_recognize", title=title, subtitle=subtitle) + + def recognize_media(self, meta: MetaBase, + tmdbid: str = None) -> Optional[MediaInfo]: + return self.run_module("recognize_media", meta=meta, tmdbid=tmdbid) + + def douban_info(self, doubanid: str) -> Optional[dict]: + return self.run_module("douban_info", doubanid=doubanid) + + def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]: + return self.run_module("message_parser", body=body, form=form, args=args) + + def webhook_parser(self, message: dict) -> Optional[dict]: + return self.run_module("webhook_parser", message=message) + + def obtain_image(self, mediainfo: MediaInfo) -> Optional[MediaInfo]: + return self.run_module("obtain_image", mediainfo=mediainfo) + + def search_medias(self, meta: MetaBase) -> Optional[List[MediaInfo]]: + return self.run_module("search_medias", meta=meta) + + def search_torrents(self, mediainfo: Optional[MediaInfo], sites: List[CommentedMap], + keyword: str = None) -> Optional[List[TorrentInfo]]: + return self.run_module("search_torrents", mediainfo=mediainfo, sites=sites, keyword=keyword) + + def refresh_torrents(self, sites: List[CommentedMap]) -> Optional[List[TorrentInfo]]: + return self.run_module("refresh_torrents", sites=sites) + + def filter_torrents(self, torrent_list: List[TorrentInfo]) -> List[TorrentInfo]: + return self.run_module("filter_torrents", torrent_list=torrent_list) + + def download(self, torrent_path: Path, cookie: str, + episodes: Set[int] = None) -> Optional[Tuple[Optional[str], str]]: + return self.run_module("download", torrent_path=torrent_path, cookie=cookie, episodes=episodes) + + def list_torrents(self, status: TorrentStatus) -> Optional[List[dict]]: + return self.run_module("list_torrents", status=status) + + def remove_torrents(self, hashs: Union[str, list]) -> bool: + return self.run_module("remove_torrents", hashs=hashs) + + def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[Path]: + return self.run_module("transfer", path=path, mediainfo=mediainfo) + + def transfer_completed(self, hashs: Union[str, list]) -> bool: + return self.run_module("transfer_completed", hashs=hashs) + + def media_exists(self, mediainfo: MediaInfo) -> Optional[dict]: + return self.run_module("media_exists", mediainfo=mediainfo) + + def refresh_mediaserver(self, mediainfo: MediaInfo, file_path: str) -> Optional[bool]: + return self.run_module("refresh_mediaserver", mediainfo=mediainfo, file_path=file_path) + + def post_message(self, title: str, + text: str = None, image: str = None, userid: Union[str, int] = None) -> Optional[bool]: + return self.run_module("post_message", title=title, text=text, image=image, userid=userid) + + def post_medias_message(self, title: str, items: List[MediaInfo], + userid: Union[str, int] = None) -> Optional[bool]: + return self.run_module("post_medias_message", title=title, items=items, userid=userid) + + def post_torrents_message(self, title: str, items: List[Context], + userid: Union[str, int] = None) -> Optional[bool]: + return self.run_module("post_torrents_message", title=title, items=items, userid=userid) + + def scrape_metadata(self, path: Path, mediainfo: MediaInfo) -> None: + return self.run_module("scrape_metadata", path=path, mediainfo=mediainfo) + + def register_commands(self, commands: dict): + return self.run_module("register_commands", commands=commands) diff --git a/app/chain/common.py b/app/chain/common.py index 05205ca5..de1e269e 100644 --- a/app/chain/common.py +++ b/app/chain/common.py @@ -21,12 +21,6 @@ class CommonChain(ChainBase): def process(self, *args, **kwargs) -> Optional[Context]: pass - def post_message(self, title: str, text: str = None, image: str = None, userid: str = None): - """ - 发送消息 - """ - self.run_module('post_message', title=title, text=text, image=image, userid=userid) - def post_download_message(self, meta: MetaBase, mediainfo: MediaInfo, torrent: TorrentInfo, userid: str = None): """ 发送添加下载的消息 @@ -93,10 +87,9 @@ class CommonChain(ChainBase): proxy=_torrent.site_proxy) if not torrent_file: logger.error(f"下载种子文件失败:{_torrent.title} - {_torrent.enclosure}") - self.run_module('post_message', - title=f"{_torrent.title} 种子下载失败!", - text=f"错误信息:{error_msg}\n种子链接:{_torrent.enclosure}", - userid=userid) + self.post_message(title=f"{_torrent.title} 种子下载失败!", + text=f"错误信息:{error_msg}\n种子链接:{_torrent.enclosure}", + userid=userid) return None, [] return torrent_file, files @@ -113,10 +106,9 @@ class CommonChain(ChainBase): if not _torrent_file: return # 添加下载 - result: Optional[tuple] = self.run_module("download", - torrent_path=_torrent_file, - cookie=_torrent.site_cookie, - episodes=_episodes) + result: Optional[tuple] = self.download(torrent_path=_torrent_file, + cookie=_torrent.site_cookie, + episodes=_episodes) if result: _hash, error_msg = result else: @@ -130,15 +122,15 @@ class CommonChain(ChainBase): # 下载失败 logger.error(f"{_media.get_title_string()} 添加下载任务失败:" f"{_torrent.title} - {_torrent.enclosure},{error_msg}") - self.run_module('post_message', - title="添加下载任务失败:%s %s" - % (_media.get_title_string(), _meta.get_season_episode_string()), - text=f"站点:{_torrent.site_name}\n" - f"种子名称:{_meta.org_string}\n" - f"种子链接:{_torrent.enclosure}\n" - f"错误信息:{error_msg}", - image=_media.get_message_image(), - userid=userid) + self.post_message( + title="添加下载任务失败:%s %s" + % (_media.get_title_string(), _meta.get_season_episode_string()), + text=f"站点:{_torrent.site_name}\n" + f"种子名称:{_meta.org_string}\n" + f"种子链接:{_torrent.enclosure}\n" + f"错误信息:{error_msg}", + image=_media.get_message_image(), + userid=userid) return _hash def __update_seasons(tmdbid: str, need: list, current: list) -> list: @@ -177,7 +169,7 @@ class CommonChain(ChainBase): if season == nt.get("season"): return nt.get("total_episodes") return 0 - + # 分组排序 contexts = TorrentHelper.sort_group_torrents(contexts) @@ -360,7 +352,7 @@ class CommonChain(ChainBase): no_exists = {} if mediainfo.type == MediaType.MOVIE: # 电影 - exists_movies: Optional[dict] = self.run_module("media_exists", mediainfo) + exists_movies: Optional[dict] = self.media_exists(mediainfo) if exists_movies: logger.info(f"媒体库中已存在电影:{mediainfo.get_title_string()}") return True, {} @@ -370,7 +362,7 @@ class CommonChain(ChainBase): logger.error(f"媒体信息中没有季集信息:{mediainfo.get_title_string()}") return False, {} # 电视剧 - exists_tvs: Optional[dict] = self.run_module("media_exists", mediainfo) + exists_tvs: Optional[dict] = self.media_exists(mediainfo) if not exists_tvs: # 所有剧集均缺失 for season, episodes in mediainfo.seasons.items(): diff --git a/app/chain/douban_sync.py b/app/chain/douban_sync.py index 514529cc..d914d02d 100644 --- a/app/chain/douban_sync.py +++ b/app/chain/douban_sync.py @@ -56,7 +56,7 @@ class DoubanSyncChain(ChainBase): if not douban_id or douban_id in caches: continue # 根据豆瓣ID获取豆瓣数据 - doubaninfo: Optional[dict] = self.run_module('douban_info', doubanid=douban_id) + doubaninfo: Optional[dict] = self.douban_info(doubanid=douban_id) if not doubaninfo: logger.warn(f'未获取到豆瓣信息,标题:{title},豆瓣ID:{douban_id}') continue @@ -65,7 +65,7 @@ class DoubanSyncChain(ChainBase): meta = MetaInfo(doubaninfo.get("original_title") or doubaninfo.get("title")) if doubaninfo.get("year"): meta.year = doubaninfo.get("year") - mediainfo: MediaInfo = self.run_module('recognize_media', meta=meta) + mediainfo: MediaInfo = self.recognize_media(meta=meta) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{title},豆瓣ID:{douban_id}') continue diff --git a/app/chain/identify.py b/app/chain/identify.py index 6e377eb0..5c2da970 100644 --- a/app/chain/identify.py +++ b/app/chain/identify.py @@ -16,18 +16,18 @@ class IdentifyChain(ChainBase): """ logger.info(f'开始识别媒体信息,标题:{title},副标题:{subtitle} ...') # 识别前预处理 - result: Optional[tuple] = self.run_module('prepare_recognize', title=title, subtitle=subtitle) + result: Optional[tuple] = self.prepare_recognize(title=title, subtitle=subtitle) if result: title, subtitle = result # 识别元数据 metainfo = MetaInfo(title, subtitle) # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=metainfo) + mediainfo: MediaInfo = self.recognize_media(meta=metainfo) if not mediainfo: logger.warn(f'{title} 未识别到媒体信息') return Context(meta=metainfo) logger.info(f'{title} 识别到媒体信息:{mediainfo.type.value} {mediainfo.get_title_string()}') # 更新媒体图片 - self.run_module('obtain_image', mediainfo=mediainfo) + self.obtain_image(mediainfo=mediainfo) # 返回上下文 return Context(meta=metainfo, mediainfo=mediainfo, title=title, subtitle=subtitle) diff --git a/app/chain/search.py b/app/chain/search.py index ee075234..7cd6daea 100644 --- a/app/chain/search.py +++ b/app/chain/search.py @@ -37,8 +37,7 @@ class SearchChain(ChainBase): logger.warn('未开启任何有效站点,无法搜索资源') return [] # 执行搜索 - torrents: List[TorrentInfo] = self.run_module( - 'search_torrents', + torrents: List[TorrentInfo] = self.search_torrents( mediainfo=mediainfo, keyword=keyword, sites=indexer_sites @@ -47,7 +46,7 @@ class SearchChain(ChainBase): logger.warn(f'{keyword or mediainfo.title} 未搜索到资源') return [] # 过滤种子 - result: List[TorrentInfo] = self.run_module("filter_torrents", torrent_list=torrents) + result: List[TorrentInfo] = self.filter_torrents(torrent_list=torrents) if result is not None: torrents = result if not torrents: @@ -66,7 +65,7 @@ class SearchChain(ChainBase): # 识别 torrent_meta = MetaInfo(torrent.title, torrent.description) # 识别媒体信息 - torrent_mediainfo: MediaInfo = self.run_module('recognize_media', meta=torrent_meta) + torrent_mediainfo: MediaInfo = self.recognize_media(meta=torrent_meta) if not torrent_mediainfo: logger.warn(f'未识别到媒体信息,标题:{torrent.title}') continue diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index c828850d..20218686 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -37,7 +37,7 @@ class SubscribeChain(ChainBase): """ logger.info(f'开始添加订阅,标题:{title} ...') # 识别前预处理 - result: Optional[tuple] = self.run_module('prepare_recognize', title=title) + result: Optional[tuple] = self.prepare_recognize(title=title) if result: title, _ = result # 识别元数据 @@ -48,12 +48,12 @@ class SubscribeChain(ChainBase): metainfo.type = MediaType.TV metainfo.begin_season = season # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=metainfo, tmdbid=tmdbid) + mediainfo: MediaInfo = self.recognize_media(meta=metainfo, tmdbid=tmdbid) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{title},tmdbid:{tmdbid}') return False # 更新媒体图片 - self.run_module('obtain_image', mediainfo=mediainfo) + self.obtain_image(mediainfo=mediainfo) # 添加订阅 state, err_msg = self.subscribes.add(mediainfo, season=season, **kwargs) if state: @@ -89,7 +89,7 @@ class SubscribeChain(ChainBase): meta.begin_season = subscribe.season meta.type = MediaType.MOVIE if subscribe.type == MediaType.MOVIE.value else MediaType.TV # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=meta, tmdbid=subscribe.tmdbid) + mediainfo: MediaInfo = self.recognize_media(meta=meta, tmdbid=subscribe.tmdbid) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{subscribe.name},tmdbid:{subscribe.tmdbid}') continue @@ -128,11 +128,11 @@ class SubscribeChain(ChainBase): continue logger.info(f'开始刷新站点资源,站点:{indexer.get("name")} ...') domain = StringUtils.get_url_domain(indexer.get("domain")) - torrents: List[TorrentInfo] = self.run_module("refresh_torrents", sites=[indexer]) + torrents: List[TorrentInfo] = self.refresh_torrents(sites=[indexer]) if torrents: self._torrents_cache[domain] = [] # 过滤种子 - result: List[TorrentInfo] = self.run_module("filter_torrents", torrent_list=torrents) + result: List[TorrentInfo] = self.filter_torrents(torrent_list=torrents) if result is not None: torrents = result if not torrents: @@ -142,7 +142,7 @@ class SubscribeChain(ChainBase): # 识别 meta = MetaInfo(torrent.title, torrent.description) # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=meta) + mediainfo: MediaInfo = self.recognize_media(meta=meta) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{torrent.title}') continue @@ -167,7 +167,7 @@ class SubscribeChain(ChainBase): meta.begin_season = subscribe.season meta.type = MediaType.MOVIE if subscribe.type == MediaType.MOVIE.value else MediaType.TV # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=meta, tmdbid=subscribe.tmdbid) + mediainfo: MediaInfo = self.recognize_media(meta=meta, tmdbid=subscribe.tmdbid) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{subscribe.name},tmdbid:{subscribe.tmdbid}') continue diff --git a/app/chain/transfer.py b/app/chain/transfer.py index 3aded307..058f6d37 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -18,7 +18,7 @@ class TransferChain(ChainBase): """ logger.info("开始执行下载器文件转移 ...") # 从下载器获取种子列表 - torrents: Optional[List[dict]] = self.run_module("list_torrents", status=TorrentStatus.TRANSFER) + torrents: Optional[List[dict]] = self.list_torrents(status=TorrentStatus.TRANSFER) if not torrents: logger.info("没有获取到已完成的下载任务") return False @@ -31,25 +31,25 @@ class TransferChain(ChainBase): logger.warn(f'未识别到元数据,标题:{torrent.get("title")}') continue # 识别媒体信息 - mediainfo: MediaInfo = self.run_module('recognize_media', meta=meta) + mediainfo: MediaInfo = self.recognize_media(meta=meta) if not mediainfo: logger.warn(f'未识别到媒体信息,标题:{torrent.get("title")}') continue logger.info(f"{torrent.get('title')} 识别为:{mediainfo.type.value} {mediainfo.get_title_string()}") # 更新媒体图片 - self.run_module("obtain_image", mediainfo=mediainfo) + self.obtain_image(mediainfo=mediainfo) # 转移 - dest_path: Path = self.run_module("transfer", mediainfo=mediainfo, path=torrent.get("path")) + dest_path: Path = self.transfer(mediainfo=mediainfo, path=torrent.get("path")) if not dest_path: logger.warn(f"{torrent.get('title')} 转移失败") continue # 转移完成 - self.run_module("transfer_completed", hashs=torrent.get("hash")) + self.transfer_completed(hashs=torrent.get("hash")) # 刮剥 - self.run_module("scrape_metadata", path=dest_path, mediainfo=mediainfo) + self.scrape_metadata(path=dest_path, mediainfo=mediainfo) # 移动模式删除种子 if settings.TRANSFER_TYPE == "move": - result = self.run_module("remove_torrents", hashs=torrent.get("hash")) + result = self.remove_torrents(hashs=torrent.get("hash")) if result: logger.info(f"移动模式删除种子成功:{torrent.get('title')} ") diff --git a/app/chain/user_message.py b/app/chain/user_message.py index 23dc7869..8a2d4acb 100644 --- a/app/chain/user_message.py +++ b/app/chain/user_message.py @@ -36,7 +36,7 @@ class UserMessageChain(ChainBase): 识别消息内容,执行操作 """ # 获取消息内容 - info: dict = self.run_module('message_parser', body=body, form=form, args=args) + info: dict = self.message_parser(body=body, form=form, args=args) if not info: return # 用户ID @@ -79,7 +79,7 @@ class UserMessageChain(ChainBase): mediainfo: MediaInfo = cache_list[int(text) + self._current_page * self._page_size - 1] self._current_media = mediainfo # 检查是否已存在 - exists: list = self.run_module('media_exists', mediainfo=mediainfo) + exists: dict = self.media_exists(mediainfo=mediainfo) if exists: # 已存在 self.common.post_message( @@ -161,15 +161,13 @@ class UserMessageChain(ChainBase): proxy=torrent.site_proxy) if not torrent_file: logger.error(f"下载种子文件失败:{torrent.title} - {torrent.enclosure}") - self.run_module('post_message', - title=f"{torrent.title} 种子下载失败!", - text=f"错误信息:{error_msg}\n种子链接:{torrent.enclosure}", - userid=userid) + self.post_message(title=f"{torrent.title} 种子下载失败!", + text=f"错误信息:{error_msg}\n种子链接:{torrent.enclosure}", + userid=userid) return # 添加下载 - result: Optional[tuple] = self.run_module("download", - torrent_path=torrent_file, - cookie=torrent.site_cookie) + result: Optional[tuple] = self.download(torrent_path=torrent_file, + cookie=torrent.site_cookie) if result: state, msg = result else: @@ -269,7 +267,7 @@ class UserMessageChain(ChainBase): self._current_meta = meta # 开始搜索 logger.info(f"开始搜索:{meta.get_name()}") - medias: Optional[List[MediaInfo]] = self.run_module('search_medias', meta=meta) + medias: Optional[List[MediaInfo]] = self.search_medias(meta=meta) if not medias: self.common.post_message(title=f"{meta.get_name()} 没有找到对应的媒体信息!", userid=userid) return @@ -286,16 +284,18 @@ class UserMessageChain(ChainBase): """ 发送媒体列表消息 """ - self.run_module('post_medias_message', - title=f"共找到{total}条相关信息,请回复数字选择对应媒体(p: 上一页 n: 下一页)", - items=items, - userid=userid) + self.post_medias_message( + title=f"共找到{total}条相关信息,请回复数字选择对应媒体(p: 上一页 n: 下一页)", + items=items, + userid=userid + ) def __post_torrents_message(self, items: list, userid: str, total: int): """ 发送种子列表消息 """ - self.run_module('post_torrents_message', - title=f"共找到{total}条相关信息,请回复数字下载对应资源(0: 自动选择 p: 上一页 n: 下一页)", - items=items, - userid=userid) + self.post_torrents_message( + title=f"共找到{total}条相关信息,请回复数字下载对应资源(0: 自动选择 p: 上一页 n: 下一页)", + items=items, + userid=userid + ) diff --git a/app/chain/webhook_message.py b/app/chain/webhook_message.py index 014881d2..133e25c5 100644 --- a/app/chain/webhook_message.py +++ b/app/chain/webhook_message.py @@ -11,8 +11,8 @@ class WebhookMessageChain(ChainBase): 处理Webhook报文并发送消息 """ # 获取主体内容 - info: dict = self.run_module('webhook_parser', message=message) + info: dict = self.webhook_parser(message=message) if not info: return # 发送消息 - self.run_module("post_message", title=info.get("title"), text=info.get("text"), image=info.get("image")) + self.post_message(title=info.get("title"), text=info.get("text"), image=info.get("image")) diff --git a/app/modules/__init__.py b/app/modules/__init__.py index 06040431..be4410e2 100644 --- a/app/modules/__init__.py +++ b/app/modules/__init__.py @@ -149,7 +149,7 @@ class _ModuleBase(metaclass=ABCMeta): """ pass - def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[str]: + def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[Path]: """ 转移一个路径下的文件 :param path: 文件路径 diff --git a/app/modules/filetransfer/__init__.py b/app/modules/filetransfer/__init__.py index fd6fd5d4..b368694a 100644 --- a/app/modules/filetransfer/__init__.py +++ b/app/modules/filetransfer/__init__.py @@ -26,7 +26,7 @@ class FileTransferModule(_ModuleBase): def init_setting(self) -> Tuple[str, Union[str, bool]]: pass - def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[str]: + def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[Path]: """ 文件转移 :param path: 文件路径 @@ -36,14 +36,14 @@ class FileTransferModule(_ModuleBase): if not settings.LIBRARY_PATH: logger.error("未设置媒体库目录,无法转移文件") return None - state, msg = self.transfer_media(in_path=Path(path), - meidainfo=mediainfo, - rmt_mode=settings.TRANSFER_TYPE, - target_dir=Path(settings.LIBRARY_PATH)) - if not state: + path, msg = self.transfer_media(in_path=Path(path), + meidainfo=mediainfo, + rmt_mode=settings.TRANSFER_TYPE, + target_dir=Path(settings.LIBRARY_PATH)) + if not path: logger.error(msg) - return state + return path @staticmethod def __transfer_command(file_item: Path, target_file: Path, rmt_mode) -> int: @@ -55,8 +55,6 @@ class FileTransferModule(_ModuleBase): """ with lock: - # 创建父目录 - target_file.parent.mkdir(parents=True, exist_ok=True) # 转移 if rmt_mode == 'link': # 硬链接 @@ -285,14 +283,16 @@ class FileTransferModule(_ModuleBase): if over_flag and old_file and old_file.exists(): logger.info(f"正在删除已存在的文件:{old_file}") old_file.unlink() - logger.info(f"正在转移文件:{file_item.name} 到 {new_file}") + logger.info(f"正在转移文件:{file_item} 到 {new_file}") + # 创建父目录 + new_file.parent.mkdir(parents=True, exist_ok=True) retcode = self.__transfer_command(file_item=file_item, target_file=new_file, rmt_mode=rmt_mode) if retcode == 0: - logger.info(f"文件 {file_item.name} {rmt_mode}完成") + logger.info(f"文件 {file_item} {rmt_mode}完成") else: - logger.error(f"文件 {file_item.name} {rmt_mode}失败,错误码:{retcode}") + logger.error(f"文件 {file_item} {rmt_mode}失败,错误码:{retcode}") return retcode # 处理其他相关文件 return self.__transfer_other_files(org_path=file_item, diff --git a/app/modules/indexer/__init__.py b/app/modules/indexer/__init__.py index b38368f3..567afb92 100644 --- a/app/modules/indexer/__init__.py +++ b/app/modules/indexer/__init__.py @@ -5,7 +5,7 @@ from typing import List, Optional, Tuple, Union from ruamel.yaml import CommentedMap -from app.core import MediaInfo, TorrentInfo, settings +from app.core import MediaInfo, TorrentInfo from app.log import logger from app.modules import _ModuleBase from app.modules.indexer.spider import TorrentSpider diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index c5bc5bfc..cf27ef57 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -74,7 +74,7 @@ class Telegram(metaclass=Singleton): else: chat_id = self._telegram_chat_id - return self.__send_request(chat_id=chat_id, image=image, caption=caption) + return self.__send_request(image=image, caption=caption) except Exception as msg_e: logger.error(f"发送消息失败:{msg_e}") @@ -112,7 +112,7 @@ class Telegram(metaclass=Singleton): else: chat_id = self._telegram_chat_id - return self.__send_request(chat_id=chat_id, image=image, caption=caption) + return self.__send_request(image=image, caption=caption) except Exception as msg_e: logger.error(f"发送消息失败:{msg_e}") @@ -142,13 +142,13 @@ class Telegram(metaclass=Singleton): else: chat_id = self._telegram_chat_id - return self.__send_request(chat_id=chat_id, caption=caption) + return self.__send_request(caption=caption) except Exception as msg_e: logger.error(f"发送消息失败:{msg_e}") return False - def __send_request(self, chat_id="", image="", caption="") -> bool: + def __send_request(self, image="", caption="") -> bool: """ 向Telegram发送报文 """ diff --git a/app/modules/themoviedb/__init__.py b/app/modules/themoviedb/__init__.py index cd9a0ba5..afc1167f 100644 --- a/app/modules/themoviedb/__init__.py +++ b/app/modules/themoviedb/__init__.py @@ -97,6 +97,7 @@ class TheMovieDb(_ModuleBase): else: # 使用缓存信息 if cache_info.get("title"): + logger.info(f"使用识别缓存:{cache_info.get('title')}") info = self.tmdb.get_info(mtype=cache_info.get("type"), tmdbid=cache_info.get("id")) else: diff --git a/app/plugins/autosignin/sites/haidan.py b/app/plugins/autosignin/sites/haidan.py index 15342091..1d8acd4a 100644 --- a/app/plugins/autosignin/sites/haidan.py +++ b/app/plugins/autosignin/sites/haidan.py @@ -1,8 +1,5 @@ -import random -import re from typing import Tuple -from lxml import etree from ruamel.yaml import CommentedMap from app.core import settings diff --git a/app/plugins/sitestatistic/__init__.py b/app/plugins/sitestatistic/__init__.py index 100067fa..9ca81811 100644 --- a/app/plugins/sitestatistic/__init__.py +++ b/app/plugins/sitestatistic/__init__.py @@ -243,7 +243,8 @@ class SiteStatistic(_PluginBase): """ 刷新站点数据 """ - logger.info("开始执行站点数据刷新 ...") + if event: + logger.info("收到命令,开始执行站点数据刷新 ...") self.refresh_all_site_data(force=True) def refresh_all_site_data(self, force: bool = False, specify_sites: list = None): diff --git a/app/utils/system.py b/app/utils/system.py index 73354e5b..3b6bccaf 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -56,7 +56,7 @@ class SystemUtils: 移动 """ try: - shutil.move(src.with_name(dest.name), dest) + shutil.move(src.replace(dest.name), dest) return 0, "" except Exception as err: print(str(err)) @@ -68,7 +68,7 @@ class SystemUtils: 硬链接 """ try: - os.link(src, dest) + dest.hardlink_to(src) return 0, "" except Exception as err: print(str(err)) @@ -80,7 +80,7 @@ class SystemUtils: 软链接 """ try: - os.symlink(src, dest) + dest.symlink_to(src) return 0, "" except Exception as err: print(str(err)) @@ -92,7 +92,7 @@ class SystemUtils: pattern = r".*(" + "|".join(extensions) + ")$" # 遍历目录及子目录 - for path in directory.glob('**/*'): + for path in directory.rglob('**/*'): if path.is_file() and re.match(pattern, path.name, re.IGNORECASE): files.append(path) diff --git a/tests/run.py b/tests/run.py index ed4cfb9d..f20cb0b0 100644 --- a/tests/run.py +++ b/tests/run.py @@ -15,11 +15,11 @@ if __name__ == '__main__': # 测试名称识别 # suite.addTest(MetaInfoTest('test_metainfo')) # 测试媒体识别 - # suite.addTest(RecognizeTest('test_recognize')) + suite.addTest(RecognizeTest('test_recognize')) # 测试CookieCloud同步 # suite.addTest(CookieCloudTest('test_cookiecloud')) # 测试文件转移 - suite.addTest(TransferTest('test_transfer')) + # suite.addTest(TransferTest('test_transfer')) # 测试豆瓣同步 # suite.addTest(DoubanSyncTest('test_doubansync'))