From 531840c3fb960190a24dba6ec067dbf5c89e8c19 Mon Sep 17 00:00:00 2001 From: lededev Date: Wed, 29 Sep 2021 06:37:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=98=E6=9C=89=E4=B8=80=E7=82=B9=E5=B0=8Fbu?= =?UTF-8?q?g=EF=BC=8C=E4=BB=A5=E5=8F=8A=E4=BB=A3=E7=A0=81=E6=B8=85?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ADC_function.py | 2 +- AV_Data_Capture.py | 8 ++++--- config.py | 2 +- core.py | 56 +++++++++++++++++++++++++--------------------- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/ADC_function.py b/ADC_function.py index 24d6e60..b13d0b4 100755 --- a/ADC_function.py +++ b/ADC_function.py @@ -614,7 +614,7 @@ def load_cookies(filename): # 文件修改时间距此时的天数 def file_modification_days(filename) -> int: mfile = Path(filename) - if not mfile.exists(): + if not mfile.is_file(): return 9999 mtime = int(mfile.stat().st_mtime) now = int(time.time()) diff --git a/AV_Data_Capture.py b/AV_Data_Capture.py index 8ac0d9c..670e68a 100755 --- a/AV_Data_Capture.py +++ b/AV_Data_Capture.py @@ -137,7 +137,7 @@ def movie_lists(root, conf, regexstr): file_type = conf.media_type().upper().split(",") trailerRE = re.compile(r'-trailer\.', re.IGNORECASE) cliRE = None - if len(regexstr): + if isinstance(regexstr, str) and len(regexstr): try: cliRE = re.compile(regexstr, re.IGNORECASE) except: @@ -198,9 +198,11 @@ def movie_lists(root, conf, regexstr): def create_failed_folder(failed_folder): - if not os.path.exists(failed_folder + '/'): # 新建failed文件夹 + if not os.path.isdir(failed_folder): # 新建failed文件夹 try: - os.makedirs(failed_folder + '/') + os.makedirs(failed_folder) + if not os.path.isdir(failed_folder): + raise except: print("[-]failed!can not be make folder 'failed'\n[-](Please run as Administrator)") sys.exit(0) diff --git a/config.py b/config.py index a96f6c7..82fd345 100644 --- a/config.py +++ b/config.py @@ -16,7 +16,7 @@ class Config: ] ini_path = None for p in path_search_order: - if os.path.exists(p): + if os.path.isfile(p): ini_path = p break if ini_path: diff --git a/core.py b/core.py index 780c4aa..cb1a782 100755 --- a/core.py +++ b/core.py @@ -82,12 +82,13 @@ def create_folder(json_data, conf: config.Config): # 创建文件夹 shorttitle = title[0:maxlen] location_rule = location_rule.replace(title, shorttitle) - path = success_folder + '/' + location_rule - path = trimblank(path) - if not os.path.exists(path): + path = os.path.join(success_folder, location_rule).strip() + if not os.path.isdir(path): path = escape_path(path, conf.escape_literals()) try: os.makedirs(path) + if not os.path.isdir(path): + raise except: path = success_folder + '/' + location_rule.replace('/[' + number + ')-' + title, "/number") path = escape_path(path, conf.escape_literals()) @@ -96,15 +97,6 @@ def create_folder(json_data, conf: config.Config): # 创建文件夹 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! @@ -114,8 +106,10 @@ def download_file_with_filename(url, filename, path, conf: config.Config, filepa for i in range(configProxy.retry): try: if configProxy.enable: - if not os.path.exists(path): + if not os.path.isdir(path): os.makedirs(path) + if not os.path.isdir(path): + raise IOError proxies = configProxy.proxies() headers = { '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) return else: - if not os.path.exists(path): + if not os.path.isdir(path): os.makedirs(path) + if not os.path.isdir(path): + raise IOError headers = { 'User-Agent': G_USER_AGENT} 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: i += 1 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!') moveFailedFolder(filepath, conf) return @@ -224,8 +224,10 @@ def print_files(path, leak_word, c_word, naming_rule, part, cn_sub, json_data, f else: nfo_path = os.path.join(path,f"{number}{part}{leak_word}{c_word}.nfo") try: - if not os.path.exists(path): + if not os.path.isdir(path): os.makedirs(path) + if not os.path.isdir(path): + raise IOError with open(nfo_path, "wt", encoding='UTF-8') as code: print('', file=code) print("", 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) except IOError as e: print("[-]Write Failed!") - print(e) + print("[-]", e) moveFailedFolder(filepath, conf) return except Exception as e1: - print(e1) print("[-]Write Failed!") + print("[-]", e1) moveFailedFolder(filepath, conf) 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): # 文件路径,番号,后缀,要移动至的位置 - houzhui = os.path.splitext(filepath)[1].replace(",","") - file_parent_origin_path = str(pathlib.Path(filepath).parent) + filepath_obj = pathlib.Path(filepath) + houzhui = filepath_obj.suffix + file_parent_origin_path = str(filepath_obj.parent) try: targetpath = os.path.join(path, f"{number}{leak_word}{c_word}{houzhui}") # 如果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() for subname in sub_res: - if os.path.exists(filepath.replace(houzhui, subname)): # 字幕移动 - shutil.move(filepath.replace(houzhui, subname), os.path.join(path, f"{number}{leak_word}{c_word}{subname}")) + sub_filepath = str(filepath_obj.with_suffix(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!') 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): # 文件路径,番号,后缀,要移动至的位置 if multi_part == 1: number += part # 这时number会被附加上CD1后缀 - houzhui = os.path.splitext(filepath)[1].replace(",","") - file_parent_origin_path = str(pathlib.Path(filepath).parent) + filepath_obj = pathlib.Path(filepath) + houzhui = filepath_obj.suffix + file_parent_origin_path = str(filepath_obj.parent) try: if conf.soft_link(): 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() for subname in sub_res: - if os.path.exists(filepath.replace(houzhui, subname)): # 字幕移动 - shutil.move(filepath.replace(houzhui, subname), os.path.join(path, f"{number}{part}{leak_word}{c_word}{subname}")) + sub_filepath = str(filepath_obj.with_suffix(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('[!]Success') return True