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