From 920b61f254dd1ecb93631151c1f1445ff07d6f7e Mon Sep 17 00:00:00 2001 From: Diaoxiaozhang Date: Mon, 25 Mar 2024 00:28:35 +0800 Subject: [PATCH] feat: selct file/directory with gui --- main.py | 68 ++++++++++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index 6ac2c0d..cd7bdec 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ import os import glob import mutagen +import tkinter as tk +from tkinter import filedialog from Crypto.Cipher import AES from Crypto.Util.Padding import pad from mutagen.easyid3 import ID3 @@ -166,6 +168,22 @@ def replace_invalid_chars(name): return name +def select_file(): + root = tk.Tk() + root.withdraw() + file_path = filedialog.askopenfilename() + root.destroy() + return file_path + + +def select_directory(): + root = tk.Tk() + root.withdraw() + directory_path = filedialog.askdirectory() + root.destroy() + return directory_path + + if __name__ == "__main__": while True: print("欢迎使用喜马拉雅音频解密工具") @@ -177,46 +195,18 @@ def replace_invalid_chars(name): choice = input() if choice == "1" or choice == "2": if choice == "1": - while True: - print("请输入需要解密的文件路径:") - file_to_decrypt = input() - if not os.path.exists(file_to_decrypt): - print("您输入文件不存在,请重新输入!") - elif not os.path.isfile(file_to_decrypt): - print("您输入的不是一个合法的文件目录,请重新输入!") - else: - files_to_decrypt = [file_to_decrypt] - break + files_to_decrypt = [select_file()] + elif choice == "2": + dir_to_decrypt = select_directory() + files_to_decrypt = glob.glob(os.path.join(dir_to_decrypt, "*.xm")) + print("请选择是否需要设置输出路径:(不设置默认为本程序目录下的output文件夹)") + print("1. 设置输出路径") + print("2. 不设置输出路径") + choice = input() + if choice == "1": + output_path = select_directory() elif choice == "2": - while True: - print("请输入包含需要解密的文件的文件夹路径:") - dir_to_decrypt = input() - if not os.path.exists(dir_to_decrypt): - print("您输入的文件夹不存在,请重新输入!") - elif not os.path.isdir(dir_to_decrypt): - print("您输入的不是一个合法的文件夹目录,请重新输入!") - else: - files_to_decrypt = glob.glob(os.path.join(dir_to_decrypt, "*.xm")) - break - while True: - print("请选择是否需要设置输出路径:(不设置默认为本程序目录下的output文件夹)") - print("1. 设置输出路径") - print("2. 不设置输出路径") - choice = input() - if choice == "1": - print("请输入输出路径:") - output_path = input() - if not os.path.exists(output_path): - print("您输入的文件夹不存在,请重新输入!") - elif not os.path.isdir(output_path): - print("您输入的不是一个合法的文件夹目录,请重新输入!") - else: - break - elif choice == "2": - output_path = "" - break - else: - print("输入错误,请重新输入!") + output_path = "" for file in files_to_decrypt: decrypt_xm_file(file, output_path) elif choice == "3":