OSS文件下载器带MAC地址对照表-改良版本最终版
import json import os import re import subprocess import tkinter as tk from tkinter import filedialog, messagebox, simpledialog from tkinter.scrolledtext import ScrolledText import chardet import oss2 # MAC地址对照表文件名 MAC_TO_NAME_DICT_FILE = 'mac_to_name_dict.json' # 预设的MAC地址对照表 DEFAULT_MAC_TO_NAME_DICT = { "30_d0_42_f5_22_05": "刘海", "e8_9c_25_a1_04_25": "蒋海", "b8_85_84_af_8d_31": "张海", "00_be_43_90_60_8c": "罗海", "90_8d_6e_5f_bb_b7": "冯海", "30_d0_42_e1_02_98": "颜海", "10_7c_61_64_84_78": "BING", "0c_7a_15_9c_6e_26": "周海", "e4_b9_7a_ee_eb_51": "李海", "90_8d_6e_5f_ba_8e": "侯海", "00_be_43_93_40_71": "段海", "10_7c_61_d3_75_b3": "谈海" } def on_entry_focus_in(event): if entry_date.get() == "例:2024-01-01": entry_date.delete(0, tk.END) def on_entry_focus_out(event): if not entry_date.get(): entry_date.insert(0, "2024-01-01") def create_dir_if_not_exists(path): if not os.path.exists(path): os.makedirs(path) def mac_to_name(file_name, mac_to_name_dict): mac_address_pattern = re.compile(r'([0-9a-fA-F]{2}[_-]){5}[0-9a-fA-F]{2}') match = mac_address_pattern.search(file_name) if match: mac_address = match.group(0) # 尝试从对照表中获取名称,如果没有找到则返回原文件名 return mac_to_name_dict.get(mac_address, file_name) return file_name def load_mac_to_name_dict(filename=MAC_TO_NAME_DICT_FILE): try: # 检测文件编码 with open(filename, 'rb') as f: rawdata = f.read() encoding = chardet.detect(rawdata)['encoding'] # 使用检测到的编码读取文件 with open(filename, 'r', encoding=encoding) as f: return json.load(f) except FileNotFoundError: return {} # 返回空字典 except json.JSONDecodeError: messagebox.showerror("错误", "MAC地址对照表文件格式错误,请检查") return {} # 返回空字典 mac_to_name_dict = load_mac_to_name_dict() if not mac_to_name_dict: mac_to_name_dict = DEFAULT_MAC_TO_NAME_DICT # 如果没有加载成功,使用默认对照表 def save_mac_to_name_dict(mac_to_name_dict, filename=MAC_TO_NAME_DICT_FILE): with open(filename, 'w') as f: json.dump(mac_to_name_dict, f, indent=4) def download_matching_files(bucket, file_name_part, download_path, mac_to_name_dict, log_text): file_count = 0 for obj in oss2.ObjectIterator(bucket): if file_name_part in obj.key: # 提取文件名中的MAC地址 mac_address_pattern = re.compile(r'([0-9a-f]{2}[_-]){5}[0-9a-f]{2}') match = mac_address_pattern.search(obj.key) if match: mac_address = match.group(0) # 根据MAC地址查找对照表中的名字 name = mac_to_name_dict.get(mac_address) if name: # 如果找到了名字,用名字替换MAC地址 new_file_name = obj.key.replace(mac_address, name) else: # 如果没找到,则使用原始文件名 new_file_name = obj.key else: # 如果文件名中没有MAC地址,直接使用原始文件名 new_file_name = obj.key file_path = os.path.join(download_path, new_file_name) create_dir_if_not_exists(os.path.dirname(file_path)) try: bucket.get_object_to_file(obj.key, file_path) log_text.insert(tk.END, f'下载 {obj.key} \n') log_text.see(tk.END) log_text.update() file_count += 1 except Exception as e: log_text.insert(tk.END, f'下载 {obj.key} 失败: {str(e)}\n') log_text.see(tk.END) log_text.update() return file_count def start_download(): file_name_part = entry_date.get() if not file_name_part: messagebox.showerror("错误", "请输入文件名中包含的日期") return download_path = entry_download_path.get() if not download_path: messagebox.showerror("错误", "请选择保存下载文件的路径") return auth = oss2.Auth('123', '456') bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '789') log_text.insert(tk.END, "开始下载文件...\n") file_count = download_matching_files(bucket, file_name_part, download_path, mac_to_name_dict, log_text) messagebox.showinfo("完成", f"总共下载的文件数:{file_count}") log_text.insert(tk.END, f"下载完成,总共下载的文件数:{file_count}\n") def open_download_folder(): if entry_download_path.get(): path = entry_download_path.get() if os.path.isdir(path): if os.name == 'nt': os.startfile(path) else: subprocess.Popen(['xdg-open', path]) else: print("路径不是有效的文件夹") else: print("请先选择下载路径") def choose_directory(): download_path = filedialog.askdirectory() if download_path: entry_download_path.delete(0, tk.END) entry_download_path.insert(0, download_path) def choose_mac_to_name_dict(): global mac_to_name_dict filename = filedialog.askopenfilename(title="选择MAC地址对照表文件", filetypes=[("JSON files", "*.json")]) if not filename: filename = os.path.join(os.path.dirname(__file__), 'mac.json') try: # 检测文件编码 with open(filename, 'rb') as f: rawdata = f.read() encoding = chardet.detect(rawdata)['encoding'] # 使用检测到的编码读取文件 with open(filename, 'r', encoding=encoding) as f: mac_to_name_dict = json.load(f) messagebox.showinfo("成功", "MAC地址对照表已更新") except FileNotFoundError: messagebox.showerror("错误", f"未找到文件: {filename}") except Exception as e: messagebox.showerror("错误", f"加载MAC地址对照表失败: {str(e)}") def show_login_dialog(): login_window = tk.Toplevel(root) login_window.title("登录") login_window.geometry("300x200") tk.Label(login_window, text="用户名:").pack(pady=5) username_entry = tk.Entry(login_window) username_entry.pack(pady=5) tk.Label(login_window, text="密码:").pack(pady=5) password_entry = tk.Entry(login_window, show='*') password_entry.pack(pady=5) def check_login(): username = username_entry.get() password = password_entry.get() if username == "admin" and password == "admin": # 修改为实际用户名和密码 login_window.destroy() else: messagebox.showerror("错误", "用户名或密码错误") password_entry.bind('<Return>', lambda event: check_login()) # 绑定回车事件 login_window.protocol("WM_DELETE_WINDOW", lambda: None) # 禁用窗口关闭按钮 tk.Button(login_window, text="登录", command=check_login).pack(pady=10) login_window.transient(root) login_window.grab_set() root.wait_window(login_window) root = tk.Tk() root.title("OSS屏幕文件下载器-专用") show_login_dialog() tk.Label(root, text="文件名中包含的日期:").grid(row=0, column=0) entry_date = tk.Entry(root, foreground='dim gray') entry_date.insert(0, "例:2024-01-01") entry_date.grid(row=0, column=1) entry_date.bind("<FocusIn>", on_entry_focus_in) entry_date.bind("<FocusOut>", on_entry_focus_out) tk.Label(root, text="下载文件保存路径:").grid(row=1, column=0) entry_download_path = tk.Entry(root) entry_download_path.grid(row=1, column=1) tk.Button(root, text="选择下载路径", command=choose_directory).grid(row=1, column=2) tk.Button(root, text="下载文件", command=start_download).grid(row=2, column=0, columnspan=3) log_text = ScrolledText(root, wrap=tk.WORD, width=50, height=10) log_text.grid(row=3, column=0, columnspan=3) # 添加编辑MAC地址对照表的按钮 # tk.Button(root, text="编辑MAC地址对照表", command=edit_mac_to_name_dict).grid(row=4, column=0, columnspan=3) # 添加选择MAC地址对照表的按钮 tk.Button(root, text="选择MAC地址对照表", command=choose_mac_to_name_dict).grid(row=5, column=0, columnspan=3) root.mainloop() # pyinstaller --noconsole --onefile main-2.py
更新了:
1、登录界面可以在不登录的情况下关闭的BUG
2、可以导入mac地址对照表,如果不导入则用程序默认的进行文件名的替换
增加这个按钮可以扩展以后新增mac对照表。