diff --git a/README.md b/README.md index b2af9a2..de18f3b 100644 --- a/README.md +++ b/README.md @@ -42,5 +42,5 @@ MoviePilot三方插件市场:https://github.com/thsrite/MoviePilot-Plugins/ - 目录监控(统一入库消息增强版) v1.0 - Sql执行器 v1.2 - 命令执行器 v1.2 -- 云盘助手 v1.4 +- 云盘助手 v1.5 - CloudDrive2助手 v1.0 \ No newline at end of file diff --git a/docs/CloudAssistant.md b/docs/CloudAssistant.md index d89e18d..b6d51cf 100644 --- a/docs/CloudAssistant.md +++ b/docs/CloudAssistant.md @@ -17,8 +17,11 @@ "mount_path": "/mnt/cloud/115/media/movies", "return_path": "/mnt/softlink/movies", "delete_local": "false", - "preserve_hierarchy": 0, + "local_preserve_hierarchy": 0, "delete_history": "false", + "delete_source": "false", + "source_dirs": "/mnt/media/movies, /mnt/media/series", + "source_preserve_hierarchy": 0, "just_media": "true", "overwrite": "false", "upload_cloud": "true" @@ -42,8 +45,11 @@ "cloud_url": "http://localhost:19798", "cloud_scheme": "http/https", "delete_local": "false", - "preserve_hierarchy": 0, + "local_preserve_hierarchy": 0, "delete_history": "false", + "delete_source": "false", + "source_dirs": "/mnt/media/movies, /mnt/media/series", + "source_preserve_hierarchy": 0, "just_media": "true", "overwrite": "false", "upload_cloud": "true" @@ -66,8 +72,11 @@ cd2方式上传--softlink回本地(暂时移除) "cd2_path": "/115/media/movies", "return_path": "/mnt/softlink/movies", "delete_local": "false", - "preserve_hierarchy": 0, + "local_preserve_hierarchy": 0, "delete_history": "false", + "delete_source": "false", + "source_dirs": "/mnt/media/movies, /mnt/media/series", + "source_preserve_hierarchy": 0, "just_media": "true", "overwrite": "false", "upload_cloud": "true" @@ -94,8 +103,11 @@ cd2方式上传--strm回本地(暂时移除) "cloud_url": "http://localhost:19798", "cloud_scheme": "http/https", "delete_local": "false", - "preserve_hierarchy": 0, + "local_preserve_hierarchy": 0, "delete_history": "false", + "delete_source": "false", + "source_dirs": "/mnt/media/movies, /mnt/media/series", + "source_preserve_hierarchy": 0, "just_media": "true", "overwrite": "false", "upload_cloud": "true" @@ -117,8 +129,11 @@ cd2方式上传--strm回本地(暂时移除) - return_path:MoviePilot中软链接/strm生成路径 - monitor_mode:监控模式 compatibility/fast - delete_local:是否删除本地文件 -- preserve_hierarchy:保留监控目录后结构层级,例如 1:表示保留监控目录后一层目录结构,0:表示仅保留到监控目录 +- local_preserve_hierarchy:保留监控路径目录层级,例如 1:表示保留监控目录后一层目录结构,0:表示仅保留到监控目录 - delete_history:是否删除MoviePilot中转移历史记录 +- delete_source:是否删除源文件,仅上述监控路径查询到转移记录时才生效,删除转移记录的源文件路径 +- source_dirs:源文件的目录,多个目录用逗号分隔 +- source_preserve_hierarchy:保留源文件路径目录层级,0:表示仅监控到源文件目录,1:表示监控源文件目录及其一级子目录 - just_media:是否只监控媒体文件 - overwrite:是否覆盖已存在云盘文件 - upload_cloud: 是否上传到云盘,false则直接软连接或者strm回本地 diff --git a/package.json b/package.json index f665e5d..14d36ac 100644 --- a/package.json +++ b/package.json @@ -521,11 +521,12 @@ "name": "云盘助手", "description": "定时移动到云盘,软连接/strm回本地,定时清理无效软连接。", "labels": "云盘", - "version": "1.4", + "version": "1.5", "icon": "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/cloudassistant.png", "author": "thsrite", "level": 3, "history": { + "v1.5": "支持转移后删除源文件、删种", "v1.4": "转移完删除本地文件时,支持自定义保留目录层级", "v1.3": "完善逻辑,增加刷新emby媒体库(需要主程序1.9.9+)", "v1.2": "移除cd2上传", diff --git a/plugins/cloudassistant/__init__.py b/plugins/cloudassistant/__init__.py index 37cb3f3..9fed930 100644 --- a/plugins/cloudassistant/__init__.py +++ b/plugins/cloudassistant/__init__.py @@ -63,7 +63,7 @@ class CloudAssistant(_PluginBase): # 插件图标 plugin_icon = "https://raw.githubusercontent.com/thsrite/MoviePilot-Plugins/main/icons/cloudassistant.png" # 插件版本 - plugin_version = "1.4" + plugin_version = "1.5" # 插件作者 plugin_author = "thsrite" # 作者主页 @@ -109,8 +109,11 @@ class CloudAssistant(_PluginBase): "mount_path": "/mnt/cloud/115/media/movies", "return_path": "/mnt/softlink/movies", "delete_local": "false", - "preserve_hierarchy": 0, + "local_preserve_hierarchy": 0, "delete_history": "false", + "delete_source": "false", + "source_dirs": "/mnt/media/movies, /mnt/media/series", + "source_preserve_hierarchy": 0, "just_media": "true", "overwrite": "false", "upload_cloud": "true" @@ -399,10 +402,13 @@ class CloudAssistant(_PluginBase): # cd2_path = monitor_dir.get("cd2_path") return_path = monitor_dir.get("return_path") delete_local = monitor_dir.get("delete_local") or "false" + delete_source = monitor_dir.get("delete_source") or "false" delete_history = monitor_dir.get("delete_history") or "false" overwrite = monitor_dir.get("overwrite") or "false" upload_cloud = monitor_dir.get("upload_cloud") or "true" - preserve_hierarchy = monitor_dir.get("preserve_hierarchy") or 0 + local_preserve_hierarchy = monitor_dir.get("local_preserve_hierarchy") or 0 + source_dirs = monitor_dir.get("source_dirs") or "" + source_preserve_hierarchy = monitor_dir.get("source_preserve_hierarchy") or 0 # 1、转移到云盘挂载路径 上传到cd2 # 挂载的路径 @@ -520,11 +526,11 @@ class CloudAssistant(_PluginBase): if str(delete_local) == "true": if file_path.exists(): file_path.unlink() - logger.info(f"删除本地文件:{file_path}") + logger.info(f"删除监控文件:{file_path}") # 保留层级 mon_path_depth = len(Path(mon_path).parts) - retain_depth = mon_path_depth + 2 + retain_depth = mon_path_depth + int(local_preserve_hierarchy) for file_dir in file_path.parents: if len(file_dir.parts) <= retain_depth: @@ -532,8 +538,52 @@ class CloudAssistant(_PluginBase): break files = SystemUtils.list_files(file_dir, settings.RMT_MEDIAEXT + settings.DOWNLOAD_TMPEXT) if not files: - logger.warn(f"删除空目录:{file_dir}") + logger.warn(f"删除监控空目录:{file_dir}") shutil.rmtree(file_dir, ignore_errors=True) + + # 是否删除源文件 + if str(delete_source) == "true" and transferhis: + if Path(transferhis.src).exists(): + Path(transferhis.src).unlink() + logger.info(f"删除源文件:{transferhis.src}") + + # 删除下载文件记录 + self.downloadhis.delete_file_by_fullpath(transferhis.src) + + # 发送事件 删种 + eventmanager.send_event( + EventType.DownloadFileDeleted, + { + "src": transferhis.src, + "hash": transferhis.download_hash + } + ) + + # 源文件保留层级 + source_path = None + for source_dir in source_dirs.split(","): + source_dir = source_dir.strip() + if not source_dir: + continue + if transferhis.src.startswith(source_dir): + source_path = source_dir + break + + # 删除源文件空目录 + if source_path: + # 保留层级 + source_path_depth = len(Path(source_path).parts) + retain_depth = source_path_depth + int(source_preserve_hierarchy) + + for file_dir in Path(transferhis.src).parents: + if len(file_dir.parts) <= retain_depth: + # 重要,删除到保留层级目录为止 + break + files = SystemUtils.list_files(file_dir, + settings.RMT_MEDIAEXT + settings.DOWNLOAD_TMPEXT) + if not files: + logger.warn(f"删除源文件空目录:{file_dir}") + shutil.rmtree(file_dir, ignore_errors=True) except Exception as e: logger.error("目录监控发生错误:%s - %s" % (str(e), traceback.format_exc()))