模式2支持硬链接;改进字幕复制适应更多文件名组合

This commit is contained in:
lededev
2022-04-09 01:35:49 +08:00
parent b048c04310
commit de58647402

64
core.py
View File

@@ -494,7 +494,6 @@ def add_to_pic(pic_path, img_pic, size, count, mode):
def paste_file_to_folder(filepath, path, number, leak_word, c_word, hack_word): # 文件路径,番号,后缀,要移动至的位置 def paste_file_to_folder(filepath, path, number, leak_word, c_word, hack_word): # 文件路径,番号,后缀,要移动至的位置
filepath_obj = pathlib.Path(filepath) filepath_obj = pathlib.Path(filepath)
houzhui = filepath_obj.suffix houzhui = filepath_obj.suffix
file_parent_origin_path = str(filepath_obj.parent)
try: try:
targetpath = os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{houzhui}") targetpath = os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{houzhui}")
# 任何情况下都不要覆盖以免遭遇数据源或者引擎错误导致所有文件得到同一个number逐一 # 任何情况下都不要覆盖以免遭遇数据源或者引擎错误导致所有文件得到同一个number逐一
@@ -520,25 +519,19 @@ def paste_file_to_folder(filepath, path, number, leak_word, c_word, hack_word):
filerelpath = os.path.relpath(filepath, path) filerelpath = os.path.relpath(filepath, path)
os.symlink(filerelpath, targetpath) os.symlink(filerelpath, targetpath)
except: except:
os.symlink(filepath_obj.resolve(), targetpath) os.symlink(str(filepath_obj.resolve()), targetpath)
sub_res = config.getInstance().sub_rule()
for subname in sub_res: sub_res = [subext.lower() for subext in config.getInstance().sub_rule()]
sub_filepath = str(filepath_obj.with_suffix(subname)) for subfile in filepath_obj.parent.glob('**/*'):
if os.path.isfile(sub_filepath.replace(subname,".chs" + subname)): if subfile.is_file() and subfile.suffix.lower() in sub_res:
sub_filepath = sub_filepath.replace(subname,".chs" + subname) sub_targetpath = Path(path) / f"{number}{leak_word}{c_word}{hack_word}{''.join(subfile.suffixes)}"
subname = ".chs" + subname
elif os.path.isfile(sub_filepath.replace(subname,".cht" + subname)):
sub_filepath = sub_filepath.replace(subname, ".cht" + subname)
subname = ".cht" + subname
if os.path.isfile(sub_filepath):
if link_mode not in (1, 2): if link_mode not in (1, 2):
shutil.move(sub_filepath, os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{subname}")) shutil.move(str(subfile), str(sub_targetpath))
print('[+]Sub moved!') print(f"[+]Sub Moved! '{sub_targetpath.name}'")
else: else:
shutil.copyfile(sub_filepath, os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{subname}")) shutil.copyfile(str(subfile), str(sub_targetpath))
print('[+]Sub Copied!') print(f"[+]Sub Copied! '{sub_targetpath.name}'")
return True return
except FileExistsError as fee: except FileExistsError as fee:
print(f'[-]FileExistsError: {fee}') print(f'[-]FileExistsError: {fee}')
@@ -557,24 +550,37 @@ def paste_file_to_folder_mode2(filepath, path, multi_part, number, part, leak_wo
number += part # 这时number会被附加上CD1后缀 number += part # 这时number会被附加上CD1后缀
filepath_obj = pathlib.Path(filepath) filepath_obj = pathlib.Path(filepath)
houzhui = filepath_obj.suffix houzhui = filepath_obj.suffix
file_parent_origin_path = str(filepath_obj.parent)
targetpath = os.path.join(path, f"{number}{part}{leak_word}{c_word}{hack_word}{houzhui}") targetpath = os.path.join(path, f"{number}{part}{leak_word}{c_word}{hack_word}{houzhui}")
if os.path.exists(targetpath): if os.path.exists(targetpath):
raise FileExistsError('File Exists on destination path, we will never overwriting.') raise FileExistsError('File Exists on destination path, we will never overwriting.')
try: try:
if config.getInstance().link_mode(): link_mode = config.getInstance().link_mode()
os.symlink(filepath, targetpath) create_softlink = False
else: if link_mode not in (1, 2):
shutil.move(filepath, targetpath) shutil.move(filepath, targetpath)
elif link_mode == 2:
try:
os.link(filepath, targetpath, follow_symlinks=False)
except:
create_softlink = True
if link_mode == 1 or create_softlink:
try:
filerelpath = os.path.relpath(filepath, path)
os.symlink(filerelpath, targetpath)
except:
os.symlink(str(filepath_obj.resolve()), targetpath)
sub_res = config.getInstance().sub_rule() sub_res = [subext.lower() for subext in config.getInstance().sub_rule()]
for subname in sub_res: for subfile in filepath_obj.parent.glob('**/*'):
sub_filepath = str(filepath_obj.with_suffix(subname)) if subfile.is_file() and subfile.suffix.lower() in sub_res:
if os.path.isfile(sub_filepath): # 字幕移动 sub_targetpath = Path(path) / f"{number}{leak_word}{c_word}{hack_word}{''.join(subfile.suffixes)}"
shutil.move(sub_filepath, os.path.join(path, f"{number}{part}{leak_word}{c_word}{hack_word}{subname}")) if link_mode not in (1, 2):
print('[+]Sub moved!') shutil.move(str(subfile), str(sub_targetpath))
print('[!]Success') print(f"[+]Sub Moved! '{sub_targetpath.name}'")
return True else:
shutil.copyfile(str(subfile), str(sub_targetpath))
print(f"[+]Sub Copied! '{sub_targetpath.name}'")
return
except FileExistsError as fee: except FileExistsError as fee:
print(f'[-]FileExistsError: {fee}') print(f'[-]FileExistsError: {fee}')
return return