Spaces:
Sleeping
Sleeping
Create upload.py
Browse files
upload.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# 定义登录和上传的URL
|
6 |
+
LOGIN_URL = "https://surbao-sur.onrender.com/api/auth/login"
|
7 |
+
UPLOAD_URL = "https://surbao-sur.onrender.com/api/fs/form"
|
8 |
+
RENAME_URL = "https://surbao-sur.onrender.com/api/fs/rename"
|
9 |
+
LIST_URL = "https://surbao-sur.onrender.com/api/fs/list"
|
10 |
+
|
11 |
+
def get_token(username, password):
|
12 |
+
"""通过登录获取 token"""
|
13 |
+
try:
|
14 |
+
response = requests.post(
|
15 |
+
LOGIN_URL,
|
16 |
+
json={"username": username, "password": password},
|
17 |
+
headers={"Content-Type": "application/json"}
|
18 |
+
)
|
19 |
+
if response.status_code != 200:
|
20 |
+
print(f"请求失败: {response.status_code}")
|
21 |
+
print("响应内容:", response.text)
|
22 |
+
return None
|
23 |
+
|
24 |
+
data = response.json()
|
25 |
+
if data.get("code") == 200 and "data" in data and "token" in data["data"]:
|
26 |
+
print("Token retrieved successfully!")
|
27 |
+
return data["data"]["token"]
|
28 |
+
else:
|
29 |
+
print("无法获取 token:", data)
|
30 |
+
return None
|
31 |
+
except Exception as e:
|
32 |
+
print(f"发生异常: {e}")
|
33 |
+
return None
|
34 |
+
|
35 |
+
def getlist(token):
|
36 |
+
"""列出文件列表"""
|
37 |
+
try:
|
38 |
+
headers = {"Authorization": token}
|
39 |
+
response = requests.post(
|
40 |
+
LIST_URL,
|
41 |
+
headers=headers,
|
42 |
+
json={"path": "/filen/openwebui", "refresh": True}
|
43 |
+
)
|
44 |
+
print("响应内容:", response.text)
|
45 |
+
except Exception as e:
|
46 |
+
print(f"发生异常: {e}")
|
47 |
+
|
48 |
+
def rename_file(token, file_name):
|
49 |
+
"""重命名文件"""
|
50 |
+
try:
|
51 |
+
headers = {"Authorization": token}
|
52 |
+
response = requests.post(
|
53 |
+
RENAME_URL,
|
54 |
+
headers=headers,
|
55 |
+
json={"name": "webui_old.db", "path": f"/filen/openwebui/{file_name}"}
|
56 |
+
)
|
57 |
+
if response.status_code == 200:
|
58 |
+
print("文件重命名成功!")
|
59 |
+
print("响应内容:", response.text)
|
60 |
+
else:
|
61 |
+
print(f"文件重命名失败: {response.status_code}")
|
62 |
+
print("响应内容:", response.text)
|
63 |
+
except Exception as e:
|
64 |
+
print(f"文件重命名时发生异常: {e}")
|
65 |
+
|
66 |
+
def upload_file(token, file_path, file_name):
|
67 |
+
"""上传文件"""
|
68 |
+
try:
|
69 |
+
headers = {
|
70 |
+
"Authorization": token,
|
71 |
+
"File-Path": f"/filen/openwebui/{file_name}",
|
72 |
+
"As-Task": "true"
|
73 |
+
}
|
74 |
+
with open(file_path, "rb") as file:
|
75 |
+
files = {"file": file}
|
76 |
+
response = requests.put(UPLOAD_URL, headers=headers, files=files)
|
77 |
+
|
78 |
+
if response.status_code == 200:
|
79 |
+
print("文件上传成功!")
|
80 |
+
print("响应内容:", response.text)
|
81 |
+
else:
|
82 |
+
print(f"文件上传失败: {response.status_code}")
|
83 |
+
print("响应内容:", response.text)
|
84 |
+
except Exception as e:
|
85 |
+
print(f"文件上传时发生异常: {e}")
|
86 |
+
|
87 |
+
def run_script():
|
88 |
+
"""主脚本逻辑"""
|
89 |
+
username = os.environ.get("USERNAME")
|
90 |
+
password = os.environ.get("PASSWORD")
|
91 |
+
file_path = "/app/backend/data/webui.db"
|
92 |
+
file_name = "webui.db"
|
93 |
+
|
94 |
+
token = get_token(username, password)
|
95 |
+
if token:
|
96 |
+
getlist(token)
|
97 |
+
rename_file(token, file_name)
|
98 |
+
upload_file(token, file_path, file_name)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
while True:
|
102 |
+
print("运行脚本中...")
|
103 |
+
run_script()
|
104 |
+
print("脚本执行完成,等待12小时...")
|
105 |
+
time.sleep(3600) # 等待 12 小时(12 小时 * 60 分钟 * 60 秒)
|