import requests
import os
import json
import tkinter as tk
from tkinter import messagebox
定义保存配置的文件名
CONFIG_FILE = "config.json"
读取配置
def load_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
return json.load(f)
return {}
保存配置
def save_config(session_id, access_token, url):
config = {
"zsxqsessionid": session_id,
"zsxq_access_token": access_token,
"url": url
}
with open(CONFIG_FILE, "w") as f:
json.dump(config, f)
下载文件的函数
def download_files():
config = load_config()
session_id = config.get("zsxqsessionid")
access_token = config.get("zsxq_access_token")
url = config.get("url")
if not session_id or not access_token or not url:
messagebox.showerror("Error", "请确保已输入所有信息并保存。")
return
headers = {
'cookie': f'zsxqsessionid={session_id}; zsxq_access_token={access_token}',
'accept': 'application/json, text/plain, */*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
messagebox.showerror("Error", f"获取主题失败: {response.status_code}")
return
data = response.json()
for topic in data.get("resp_data", {}).get("topics", []):
if "files" in topic.get("talk", {}):
for file in topic["talk"]["files"]:
if "file_id" in file:
file_id = file["file_id"]
download_url_api = f"https://api.zsxq.com/v2/files/{file_id}/download_url"
try:
download_response = requests.get(download_url_api, headers=headers)
download_response.raise_for_status()
download_data = download_response.json()
if download_data.get("succeeded"):
file_url = download_data["resp_data"]["download_url"]
file_name = file["name"]
file_response = requests.get(file_url, headers=headers)
file_response.raise_for_status()
if os.path.exists(file_name):
base, extension = os.path.splitext(file_name)
counter = 1
while os.path.exists(file_name):
file_name = f"{base}_{counter}{extension}"
counter += 1
with open(file_name, "wb") as file_obj:
file_obj.write(file_response.content)
print(f"Downloaded: {file_name}")
else:
print(f"获取文件 ID {file_id} 的下载链接失败")
except requests.RequestException as e:
print(f"获取文件 ID {file_id} 的下载链接失败: {e}")
保存配置的回调
def save_button_click():
session_id = session_id_entry.get()
access_token = access_token_entry.get()
url = url_entry.get()
save_config(session_id, access_token, url)
messagebox.showinfo("Info", "配置已保存!")
创建 UI
root = tk.Tk()
root.title("下载配置")
tk.Label(root, text="zsxqsessionid:").grid(row=0, column=0)
session_id_entry = tk.Entry(root)
session_id_entry.grid(row=0, column=1)
tk.Label(root, text="zsxq_access_token:").grid(row=1, column=0)
access_token_entry = tk.Entry(root)
access_token_entry.grid(row=1, column=1)
tk.Label(root, text="URL:").grid(row=2, column=0)
url_entry = tk.Entry(root)
url_entry.grid(row=2, column=1)
save_button = tk.Button(root, text="保存配置", command=save_button_click)
save_button.grid(row=3, column=0, columnspan=2)
download_button = tk.Button(root, text="下载文件", command=download_files)
download_button.grid(row=4, column=0, columnspan=2)
加载现有配置
config = load_config()
session_id_entry.insert(0, config.get("zsxqsessionid", ""))
access_token_entry.insert(0, config.get("zsxq_access_token", ""))
url_entry.insert(0, config.get("url", ""))
root.mainloop()