还有一点小bug,以及代码清理

This commit is contained in:
lededev
2021-09-29 06:37:45 +08:00
parent 2c22d70078
commit 531840c3fb
4 changed files with 38 additions and 30 deletions

View File

@@ -614,7 +614,7 @@ def load_cookies(filename):
# 文件修改时间距此时的天数 # 文件修改时间距此时的天数
def file_modification_days(filename) -> int: def file_modification_days(filename) -> int:
mfile = Path(filename) mfile = Path(filename)
if not mfile.exists(): if not mfile.is_file():
return 9999 return 9999
mtime = int(mfile.stat().st_mtime) mtime = int(mfile.stat().st_mtime)
now = int(time.time()) now = int(time.time())

View File

@@ -137,7 +137,7 @@ def movie_lists(root, conf, regexstr):
file_type = conf.media_type().upper().split(",") file_type = conf.media_type().upper().split(",")
trailerRE = re.compile(r'-trailer\.', re.IGNORECASE) trailerRE = re.compile(r'-trailer\.', re.IGNORECASE)
cliRE = None cliRE = None
if len(regexstr): if isinstance(regexstr, str) and len(regexstr):
try: try:
cliRE = re.compile(regexstr, re.IGNORECASE) cliRE = re.compile(regexstr, re.IGNORECASE)
except: except:
@@ -198,9 +198,11 @@ def movie_lists(root, conf, regexstr):
def create_failed_folder(failed_folder): def create_failed_folder(failed_folder):
if not os.path.exists(failed_folder + '/'): # 新建failed文件夹 if not os.path.isdir(failed_folder): # 新建failed文件夹
try: try:
os.makedirs(failed_folder + '/') os.makedirs(failed_folder)
if not os.path.isdir(failed_folder):
raise
except: except:
print("[-]failed!can not be make folder 'failed'\n[-](Please run as Administrator)") print("[-]failed!can not be make folder 'failed'\n[-](Please run as Administrator)")
sys.exit(0) sys.exit(0)

View File

@@ -16,7 +16,7 @@ class Config:
] ]
ini_path = None ini_path = None
for p in path_search_order: for p in path_search_order:
if os.path.exists(p): if os.path.isfile(p):
ini_path = p ini_path = p
break break
if ini_path: if ini_path:

56
core.py
View File

@@ -82,12 +82,13 @@ def create_folder(json_data, conf: config.Config): # 创建文件夹
shorttitle = title[0:maxlen] shorttitle = title[0:maxlen]
location_rule = location_rule.replace(title, shorttitle) location_rule = location_rule.replace(title, shorttitle)
path = success_folder + '/' + location_rule path = os.path.join(success_folder, location_rule).strip()
path = trimblank(path) if not os.path.isdir(path):
if not os.path.exists(path):
path = escape_path(path, conf.escape_literals()) path = escape_path(path, conf.escape_literals())
try: try:
os.makedirs(path) os.makedirs(path)
if not os.path.isdir(path):
raise
except: except:
path = success_folder + '/' + location_rule.replace('/[' + number + ')-' + title, "/number") path = success_folder + '/' + location_rule.replace('/[' + number + ')-' + title, "/number")
path = escape_path(path, conf.escape_literals()) path = escape_path(path, conf.escape_literals())
@@ -96,15 +97,6 @@ def create_folder(json_data, conf: config.Config): # 创建文件夹
return path return path
def trimblank(s: str):
"""
Clear the blank on the right side of the folder name
"""
if s[-1] == " ":
return trimblank(s[:-1])
else:
return s
# =====================资源下载部分=========================== # =====================资源下载部分===========================
# path = examle:photo , video.in the Project Folder! # path = examle:photo , video.in the Project Folder!
@@ -114,8 +106,10 @@ def download_file_with_filename(url, filename, path, conf: config.Config, filepa
for i in range(configProxy.retry): for i in range(configProxy.retry):
try: try:
if configProxy.enable: if configProxy.enable:
if not os.path.exists(path): if not os.path.isdir(path):
os.makedirs(path) os.makedirs(path)
if not os.path.isdir(path):
raise IOError
proxies = configProxy.proxies() proxies = configProxy.proxies()
headers = { headers = {
'User-Agent': G_USER_AGENT} 'User-Agent': G_USER_AGENT}
@@ -127,8 +121,10 @@ def download_file_with_filename(url, filename, path, conf: config.Config, filepa
code.write(r.content) code.write(r.content)
return return
else: else:
if not os.path.exists(path): if not os.path.isdir(path):
os.makedirs(path) os.makedirs(path)
if not os.path.isdir(path):
raise IOError
headers = { headers = {
'User-Agent': G_USER_AGENT} 'User-Agent': G_USER_AGENT}
r = requests.get(url, timeout=configProxy.timeout, headers=headers) r = requests.get(url, timeout=configProxy.timeout, headers=headers)
@@ -150,6 +146,10 @@ def download_file_with_filename(url, filename, path, conf: config.Config, filepa
except requests.exceptions.ConnectTimeout: except requests.exceptions.ConnectTimeout:
i += 1 i += 1
print('[-]Image Download : Connect retry ' + str(i) + '/' + str(configProxy.retry)) print('[-]Image Download : Connect retry ' + str(i) + '/' + str(configProxy.retry))
except IOError:
print(f"[-]Create Directory '{path}' failed!")
moveFailedFolder(filepath, conf)
return
print('[-]Connect Failed! Please check your Proxy or Network!') print('[-]Connect Failed! Please check your Proxy or Network!')
moveFailedFolder(filepath, conf) moveFailedFolder(filepath, conf)
return return
@@ -224,8 +224,10 @@ def print_files(path, leak_word, c_word, naming_rule, part, cn_sub, json_data, f
else: else:
nfo_path = os.path.join(path,f"{number}{part}{leak_word}{c_word}.nfo") nfo_path = os.path.join(path,f"{number}{part}{leak_word}{c_word}.nfo")
try: try:
if not os.path.exists(path): if not os.path.isdir(path):
os.makedirs(path) os.makedirs(path)
if not os.path.isdir(path):
raise IOError
with open(nfo_path, "wt", encoding='UTF-8') as code: with open(nfo_path, "wt", encoding='UTF-8') as code:
print('<?xml version="1.0" encoding="UTF-8" ?>', file=code) print('<?xml version="1.0" encoding="UTF-8" ?>', file=code)
print("<movie>", file=code) print("<movie>", file=code)
@@ -284,12 +286,12 @@ def print_files(path, leak_word, c_word, naming_rule, part, cn_sub, json_data, f
print("[+]Wrote! " + nfo_path) print("[+]Wrote! " + nfo_path)
except IOError as e: except IOError as e:
print("[-]Write Failed!") print("[-]Write Failed!")
print(e) print("[-]", e)
moveFailedFolder(filepath, conf) moveFailedFolder(filepath, conf)
return return
except Exception as e1: except Exception as e1:
print(e1)
print("[-]Write Failed!") print("[-]Write Failed!")
print("[-]", e1)
moveFailedFolder(filepath, conf) moveFailedFolder(filepath, conf)
return return
@@ -390,8 +392,9 @@ def add_to_pic(pic_path, img_pic, size, count, mode):
# ========================结束================================= # ========================结束=================================
def paste_file_to_folder(filepath, path, number, leak_word, c_word, conf: config.Config): # 文件路径,番号,后缀,要移动至的位置 def paste_file_to_folder(filepath, path, number, leak_word, c_word, conf: config.Config): # 文件路径,番号,后缀,要移动至的位置
houzhui = os.path.splitext(filepath)[1].replace(",","") filepath_obj = pathlib.Path(filepath)
file_parent_origin_path = str(pathlib.Path(filepath).parent) 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}{houzhui}") targetpath = os.path.join(path, f"{number}{leak_word}{c_word}{houzhui}")
# 如果soft_link=1 使用软链接 # 如果soft_link=1 使用软链接
@@ -413,8 +416,9 @@ def paste_file_to_folder(filepath, path, number, leak_word, c_word, conf: config
sub_res = conf.sub_rule() sub_res = conf.sub_rule()
for subname in sub_res: for subname in sub_res:
if os.path.exists(filepath.replace(houzhui, subname)): # 字幕移动 sub_filepath = str(filepath_obj.with_suffix(subname))
shutil.move(filepath.replace(houzhui, subname), os.path.join(path, f"{number}{leak_word}{c_word}{subname}")) if os.path.isfile(sub_filepath): # 字幕移动
shutil.move(sub_filepath, os.path.join(path, f"{number}{leak_word}{c_word}{subname}"))
print('[+]Sub moved!') print('[+]Sub moved!')
return True return True
@@ -433,8 +437,9 @@ def paste_file_to_folder(filepath, path, number, leak_word, c_word, conf: config
def paste_file_to_folder_mode2(filepath, path, multi_part, number, part, leak_word, c_word, conf): # 文件路径,番号,后缀,要移动至的位置 def paste_file_to_folder_mode2(filepath, path, multi_part, number, part, leak_word, c_word, conf): # 文件路径,番号,后缀,要移动至的位置
if multi_part == 1: if multi_part == 1:
number += part # 这时number会被附加上CD1后缀 number += part # 这时number会被附加上CD1后缀
houzhui = os.path.splitext(filepath)[1].replace(",","") filepath_obj = pathlib.Path(filepath)
file_parent_origin_path = str(pathlib.Path(filepath).parent) houzhui = filepath_obj.suffix
file_parent_origin_path = str(filepath_obj.parent)
try: try:
if conf.soft_link(): if conf.soft_link():
os.symlink(filepath, os.path.join(path, f"{number}{part}{leak_word}{c_word}{houzhui}")) os.symlink(filepath, os.path.join(path, f"{number}{part}{leak_word}{c_word}{houzhui}"))
@@ -443,8 +448,9 @@ def paste_file_to_folder_mode2(filepath, path, multi_part, number, part, leak_wo
sub_res = conf.sub_rule() sub_res = conf.sub_rule()
for subname in sub_res: for subname in sub_res:
if os.path.exists(filepath.replace(houzhui, subname)): # 字幕移动 sub_filepath = str(filepath_obj.with_suffix(subname))
shutil.move(filepath.replace(houzhui, subname), os.path.join(path, f"{number}{part}{leak_word}{c_word}{subname}")) if os.path.isfile(sub_filepath): # 字幕移动
shutil.move(sub_filepath, os.path.join(path, f"{number}{part}{leak_word}{c_word}{subname}"))
print('[+]Sub moved!') print('[+]Sub moved!')
print('[!]Success') print('[!]Success')
return True return True