import os import re import subprocess import oss2 import tkinter as tk from tkinter import filedialog, messagebox, simpledialog from tkinter.scrolledtext import ScrolledText import json # MAC地址对照表文件名 MAC_TO_NAME_DICT_FILE = 'mac_to_name_dict.json' 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, 'r') 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() # 加载对照表到内存中 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: file_name = mac_to_name(obj.key, mac_to_name_dict) file_path = os.path.join(download_path, 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} 到 {file_path}\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 edit_mac_to_name_dict(): global mac_to_name_dict new_dict_str = simpledialog.askstring("编辑MAC地址对照表", "请输入MAC地址对照表,格式为JSON字符串。", initialvalue=json.dumps(mac_to_name_dict, indent=4)) if new_dict_str: try: new_dict = json.loads(new_dict_str) if isinstance(new_dict, dict): mac_to_name_dict = new_dict save_mac_to_name_dict(mac_to_name_dict) messagebox.showinfo("成功", "MAC地址对照表已更新") else: messagebox.showerror("错误", "输入的格式不正确,请输入一个字典") except json.JSONDecodeError: messagebox.showerror("错误", "输入的字符串不是有效的JSON格式") def choose_mac_to_name_dict(): global mac_to_name_dict filename = filedialog.askopenfilename(title="选择MAC地址对照表文件", filetypes=[("JSON files", "*.json")]) if filename: try: mac_to_name_dict = load_mac_to_name_dict(filename) messagebox.showinfo("成功", "MAC地址对照表已更新") except Exception as e: messagebox.showerror("错误", f"加载MAC地址对照表失败: {str(e)}") root = tk.Tk() root.title("OSS屏幕文件下载器-SINGSONG专用") 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()
标签:name,OSS,mac,截屏,dict,file,entry,path From: https://www.cnblogs.com/lyt263/p/18290552