diff --git a/src/main.py b/src/main.py index 8a1feab..fc1c067 100644 --- a/src/main.py +++ b/src/main.py @@ -23,7 +23,7 @@ import function as f from sys import exit -version = "v2.8.2" +version = "v2.8.3" # 检查更新 f.check_update(version) diff --git a/src/qimao_update.py b/src/qimao_update.py index d6f5182..5955695 100644 --- a/src/qimao_update.py +++ b/src/qimao_update.py @@ -130,10 +130,16 @@ def qimao_update(data_folder): print(f"{novel_name} 已是最新,不需要更新。\n") else: print(f"{novel_name} 已更新完成。\n") + # 计算文件 sha256 值 + hash_sha256 = hashlib.sha256() + with open(txt_file_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_sha256.update(chunk) + sha256_hash = hash_sha256.hexdigest() # 获取当前系统时间 current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 创建要写入元信息文件的内容 - new_content = f"{current_time}\n{url}\n{result}\n{encoding}" + new_content = f"{current_time}\n{url}\n{result}\n{encoding}\n{sha256_hash}" # 打开文件并完全覆盖内容 with open(upd_file_path, "w") as file: file.write(new_content) @@ -282,36 +288,52 @@ def onefile(data_folder): url = lines[1].strip() last_chapter_id = lines[2].strip() encoding = lines[3].strip() - save_sha256 = lines[4].strip() + if len(lines) >= 5: + save_sha256 = lines[4].strip() + skip_hash = 0 + else: + print(Fore.YELLOW + Style.BRIGHT + "此小说可能由老版本下载,跳过hash校验") + save_sha256 = None + skip_hash = 1 hash_sha256 = hashlib.sha256() with open(txt_file_path, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_sha256.update(chunk) fact_sha256 = hash_sha256.hexdigest() - - if fact_sha256 != save_sha256: - print(Fore.RED + Style.BRIGHT + "hash校验未通过!") - while True: - upd_choice = input(f"这往往意味着文件已被修改,是否继续更新?(yes/no):") - if upd_choice == "yes": - break - elif upd_choice == "no": - print(Fore.RED + Style.BRIGHT + "更新已取消") + if skip_hash == 0: + if fact_sha256 != save_sha256: + print(Fore.RED + Style.BRIGHT + "hash校验未通过!") + while True: + upd_choice = input(f"这往往意味着文件已被修改,是否继续更新?(yes/no):") + if upd_choice == "yes": + skip_this = 0 + break + elif upd_choice == "no": + skip_this = 1 + break + else: + print("输入错误,请重新输入") + if skip_this == 1: + print(Fore.RED + Style.BRIGHT + f"《{novel_name}》的更新已取消") return - else: - print("输入错误,请重新输入") - else: - print(Fore.GREEN + Style.BRIGHT + "hash校验通过!") + else: + print(Fore.GREEN + Style.BRIGHT + "hash校验通过!") print(f"上次更新时间{last_update_time}") result = download_novel(url, encoding, last_chapter_id, txt_file_path) if result == "DN": print(f"{novel_name} 已是最新,不需要更新。\n") else: print(f"{novel_name} 已更新完成。\n") + # 计算文件 sha256 值 + hash_sha256 = hashlib.sha256() + with open(txt_file_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_sha256.update(chunk) + sha256_hash = hash_sha256.hexdigest() # 获取当前系统时间 current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 创建要写入元信息文件的内容 - new_content = f"{current_time}\n{url}\n{result}\n{encoding}" + new_content = f"{current_time}\n{url}\n{result}\n{encoding}\n{sha256_hash}" # 打开文件并完全覆盖内容 with open(upd_file_path, "w") as file: file.write(new_content)