Spaces:
Running
Running
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 秒) |