mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
优化整理代码效率、减少额外递归
This commit is contained in:
@@ -151,10 +151,10 @@ class StorageChain(ChainBase):
|
|||||||
判断是否包含蓝光必备的文件夹
|
判断是否包含蓝光必备的文件夹
|
||||||
"""
|
"""
|
||||||
required_files = ("BDMV", "CERTIFICATE")
|
required_files = ("BDMV", "CERTIFICATE")
|
||||||
for item in fileitems or []:
|
return any(
|
||||||
if item.type == "dir" and item.name in required_files:
|
item.type == "dir" and item.name in required_files
|
||||||
return True
|
for item in fileitems or []
|
||||||
return False
|
)
|
||||||
|
|
||||||
def delete_media_file(self, fileitem: schemas.FileItem, delete_self: bool = True) -> bool:
|
def delete_media_file(self, fileitem: schemas.FileItem, delete_self: bool = True) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|||||||
+28
-46
@@ -895,13 +895,13 @@ class TransferChain(ChainBase, metaclass=Singleton):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def __get_trans_fileitems(
|
def __get_trans_fileitems(
|
||||||
self, fileitem: FileItem, depth: int = 1
|
self, fileitem: FileItem, check: bool = True
|
||||||
) -> List[Tuple[FileItem, bool]]:
|
) -> List[Tuple[FileItem, bool]]:
|
||||||
"""
|
"""
|
||||||
获取整理目录或文件列表
|
获取整理目录或文件列表
|
||||||
|
|
||||||
:param fileitem: 文件项
|
:param fileitem: 文件项
|
||||||
:param depth: 递归深度,默认为1
|
:param check: 检查文件是否存在,默认为True
|
||||||
"""
|
"""
|
||||||
storagechain = StorageChain()
|
storagechain = StorageChain()
|
||||||
|
|
||||||
@@ -920,44 +920,40 @@ class TransferChain(ChainBase, metaclass=Singleton):
|
|||||||
return storagechain.get_file_item(storage=_storage, path=p.parent)
|
return storagechain.get_file_item(storage=_storage, path=p.parent)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
latest_fileitem = storagechain.get_item(fileitem)
|
if check:
|
||||||
if not latest_fileitem:
|
latest_fileitem = storagechain.get_item(fileitem)
|
||||||
logger.warn(f"目录或文件不存在:{fileitem.path}")
|
if not latest_fileitem:
|
||||||
return []
|
logger.warn(f"目录或文件不存在:{fileitem.path}")
|
||||||
# 确保从历史记录重新整理时 能获得最新的源文件大小、修改日期等
|
return []
|
||||||
fileitem = latest_fileitem
|
# 确保从历史记录重新整理时 能获得最新的源文件大小、修改日期等
|
||||||
|
fileitem = latest_fileitem
|
||||||
|
|
||||||
# 蓝光原盘子目录或文件
|
# 是否蓝光原盘子目录或文件
|
||||||
if __is_bluray_sub(fileitem.path):
|
if __is_bluray_sub(fileitem.path):
|
||||||
dir_item = __get_bluray_dir(fileitem.storage, Path(fileitem.path))
|
if dir_item := __get_bluray_dir(fileitem.storage, Path(fileitem.path)):
|
||||||
if dir_item:
|
# 返回该文件所在的原盘根目录
|
||||||
return [(dir_item, True)]
|
return [(dir_item, True)]
|
||||||
|
|
||||||
# 单文件
|
# 单文件
|
||||||
if fileitem.type == "file":
|
if fileitem.type == "file":
|
||||||
return [(fileitem, False)]
|
return [(fileitem, False)]
|
||||||
|
|
||||||
# 蓝光原盘根目录
|
# 是否蓝光原盘根目录
|
||||||
sub_items = storagechain.list_files(fileitem) or []
|
sub_items = storagechain.list_files(fileitem, recursion=False) or []
|
||||||
if storagechain.contains_bluray_subdirectories(sub_items):
|
if storagechain.contains_bluray_subdirectories(sub_items):
|
||||||
|
# 当前目录是原盘根目录,不需要递归
|
||||||
return [(fileitem, True)]
|
return [(fileitem, True)]
|
||||||
|
|
||||||
# 需要整理的文件项列表
|
# 不是原盘根目录 递归获取目录内需要整理的文件项列表
|
||||||
trans_items = []
|
return [
|
||||||
# 先检查当前目录的下级目录,以支持合集的情况
|
item
|
||||||
for sub_dir in sub_items if depth >= 1 else []:
|
for sub_item in sub_items
|
||||||
if sub_dir.type == "dir":
|
for item in (
|
||||||
trans_items.extend(self.__get_trans_fileitems(sub_dir, depth=depth - 1))
|
self.__get_trans_fileitems(sub_item, check=False)
|
||||||
|
if sub_item.type == "dir"
|
||||||
if not trans_items:
|
else [(sub_item, False)]
|
||||||
# 没有有效子目录,直接整理当前目录
|
)
|
||||||
trans_items.append((fileitem, False))
|
]
|
||||||
else:
|
|
||||||
# 有子目录时,把当前目录的文件添加到整理任务中
|
|
||||||
if sub_items:
|
|
||||||
trans_items.extend([(f, False) for f in sub_items if f.type == "file"])
|
|
||||||
|
|
||||||
return trans_items
|
|
||||||
|
|
||||||
def do_transfer(self, fileitem: FileItem,
|
def do_transfer(self, fileitem: FileItem,
|
||||||
meta: MetaBase = None, mediainfo: MediaInfo = None,
|
meta: MetaBase = None, mediainfo: MediaInfo = None,
|
||||||
@@ -1024,27 +1020,13 @@ class TransferChain(ChainBase, metaclass=Singleton):
|
|||||||
transfer_exclude_words = SystemConfigOper().get(SystemConfigKey.TransferExcludeWords)
|
transfer_exclude_words = SystemConfigOper().get(SystemConfigKey.TransferExcludeWords)
|
||||||
# 汇总错误信息
|
# 汇总错误信息
|
||||||
err_msgs: List[str] = []
|
err_msgs: List[str] = []
|
||||||
# 待整理目录或文件项
|
# 递归获取待整理的文件/目录列表
|
||||||
trans_items = self.__get_trans_fileitems(
|
file_items = self.__get_trans_fileitems(fileitem)
|
||||||
fileitem, depth=2 # 为解决 issue#4371 深度至少需要>=2
|
|
||||||
)
|
|
||||||
# 待整理的文件列表
|
|
||||||
file_items: List[Tuple[FileItem, bool]] = []
|
|
||||||
|
|
||||||
if not trans_items:
|
if not file_items:
|
||||||
logger.warn(f"{fileitem.path} 没有找到可整理的媒体文件")
|
logger.warn(f"{fileitem.path} 没有找到可整理的媒体文件")
|
||||||
return False, f"{fileitem.name} 没有找到可整理的媒体文件"
|
return False, f"{fileitem.name} 没有找到可整理的媒体文件"
|
||||||
|
|
||||||
# 转换为所有待处理的文件清单
|
|
||||||
for trans_item, bluray_dir in trans_items:
|
|
||||||
# 如果是目录且不是⼀蓝光原盘,获取所有文件并整理
|
|
||||||
if trans_item.type == "dir" and not bluray_dir:
|
|
||||||
# 遍历获取下载目录所有文件(递归)
|
|
||||||
if files := StorageChain().list_files(trans_item, recursion=True):
|
|
||||||
file_items.extend([(file, False) for file in files])
|
|
||||||
else:
|
|
||||||
file_items.append((trans_item, bluray_dir))
|
|
||||||
|
|
||||||
# 有集自定义格式,过滤文件
|
# 有集自定义格式,过滤文件
|
||||||
if formaterHandler:
|
if formaterHandler:
|
||||||
file_items = [f for f in file_items if formaterHandler.match(f[0].name)]
|
file_items = [f for f in file_items if formaterHandler.match(f[0].name)]
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ bluray_files = [
|
|||||||
("Pokemon.2029.mp4", 104857600),
|
("Pokemon.2029.mp4", 104857600),
|
||||||
("Pokemon.2039.mp4", 104857600),
|
("Pokemon.2039.mp4", 104857600),
|
||||||
("Pokemon (2030)", [("S", 104857600)]),
|
("Pokemon (2030)", [("S", 104857600)]),
|
||||||
|
("Pokemon (2031)", [("Pokemon (2031).mp4", 104857600)]),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -126,8 +126,9 @@ class BluRayTest(TestCase):
|
|||||||
"/FOLDER/Pokemon (2028)/Pokemon.2028.mkv",
|
"/FOLDER/Pokemon (2028)/Pokemon.2028.mkv",
|
||||||
"/FOLDER/Pokemon.2029.mp4",
|
"/FOLDER/Pokemon.2029.mp4",
|
||||||
"/FOLDER/Pokemon.2039.mp4",
|
"/FOLDER/Pokemon.2039.mp4",
|
||||||
|
"/FOLDER/Pokemon (2031)/Pokemon (2031).mp4",
|
||||||
],
|
],
|
||||||
__test_do_transfer("/FOLDER"),
|
__test_do_transfer("/"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _test_scrape_metadata(self, mock_metadata_nfo):
|
def _test_scrape_metadata(self, mock_metadata_nfo):
|
||||||
|
|||||||
Reference in New Issue
Block a user