Merge remote-tracking branch 'origin/main'

This commit is contained in:
jxxghp 2023-09-19 17:15:26 +08:00
commit 2250e7db39
3 changed files with 58 additions and 12 deletions

View File

@ -225,7 +225,7 @@ class DownloadChain(ChainBase):
self.downloadhis.add_files(files_to_add) self.downloadhis.add_files(files_to_add)
# 发送消息 # 发送消息
self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel) self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel, userid=userid)
# 下载成功后处理 # 下载成功后处理
self.download_added(context=context, download_dir=download_dir, torrent_path=torrent_file) self.download_added(context=context, download_dir=download_dir, torrent_path=torrent_file)
# 广播事件 # 广播事件

View File

@ -1,3 +1,4 @@
import glob
import re import re
import shutil import shutil
import threading import threading
@ -601,8 +602,10 @@ class TransferChain(ChainBase):
if not path.exists(): if not path.exists():
return return
if path.is_file(): if path.is_file():
# 删除文件 # 删除文件、nfo、jpg
path.unlink() files = glob.glob(f"{Path(path.parent).joinpath(path.stem)}*")
for file in files:
Path(file).unlink()
logger.warn(f"文件 {path} 已删除") logger.warn(f"文件 {path} 已删除")
# 需要删除父目录 # 需要删除父目录
elif str(path.parent) == str(path.root): elif str(path.parent) == str(path.root):
@ -615,11 +618,24 @@ class TransferChain(ChainBase):
# 删除目录 # 删除目录
logger.warn(f"目录 {path} 已删除") logger.warn(f"目录 {path} 已删除")
# 需要删除父目录 # 需要删除父目录
# 判断父目录是否为空, 为空则删除
for parent_path in path.parents: # 判断当前媒体父路径下是否有媒体文件,如有则无需遍历父级
if str(parent_path.parent) != str(path.root): if not SystemUtils.exits_files(path.parent, settings.RMT_MEDIAEXT):
# 父目录非根目录,才删除父目录 # 媒体库二级分类根路径
files = SystemUtils.list_files(parent_path, settings.RMT_MEDIAEXT) library_root_names = [
if not files: settings.LIBRARY_MOVIE_NAME or '电影',
shutil.rmtree(parent_path) settings.LIBRARY_TV_NAME or '电视剧',
logger.warn(f"目录 {parent_path} 已删除") settings.LIBRARY_ANIME_NAME or '动漫',
]
# 判断父目录是否为空, 为空则删除
for parent_path in path.parents:
# 遍历父目录到媒体库二级分类根路径
if str(parent_path.name) in library_root_names:
break
if str(parent_path.parent) != str(path.root):
# 父目录非根目录,才删除父目录
if not SystemUtils.exits_files(path.parent, settings.RMT_MEDIAEXT):
# 当前路径下没有媒体文件则删除
shutil.rmtree(parent_path)
logger.warn(f"目录 {parent_path} 已删除")

View File

@ -122,6 +122,36 @@ class SystemUtils:
return files return files
@staticmethod
def exits_files(directory: Path, extensions: list, min_filesize: int = 0) -> bool:
"""
判断目录下是否存在指定扩展名的文件
:return True存在 False不存在
"""
if not min_filesize:
min_filesize = 0
if not directory.exists():
return False
if directory.is_file():
return True
if not min_filesize:
min_filesize = 0
pattern = r".*(" + "|".join(extensions) + ")$"
# 遍历目录及子目录
for path in directory.rglob('**/*'):
if path.is_file() \
and re.match(pattern, path.name, re.IGNORECASE) \
and path.stat().st_size >= min_filesize * 1024 * 1024:
return True
return False
@staticmethod @staticmethod
def list_sub_files(directory: Path, extensions: list) -> List[Path]: def list_sub_files(directory: Path, extensions: list) -> List[Path]:
""" """