import random import requests import os from time import time, sleep def remove_file(remove_url): """ 删除服务器上的文件 Args: remove_url (str): 删除接口的完整URL (例如 "https://plailish.wliwan.com/remove?path=/topics/incomg_vs_living.html") Returns: requests.Response: 服务器的响应对象 """ try: print(f"正在删除服务器上的文件,URL: {remove_url}...") response = requests.post(remove_url, timeout=30) print(f"服务器响应状态码: {response.status_code}") # print(f"服务器响应内容: {response.text}") response.raise_for_status() # 如果状态码不是200,会抛出异常 print("文件删除成功!") return response except requests.exceptions.RequestException as e: print(f"删除过程中出现错误: {e}") return None except Exception as e: print(f"发生未知错误: {e}") return None def upload_file(file_path, upload_url): """ 上传文件到指定的URL Args: file_path (str): 要上传的本地文件路径 upload_url (str): 上传接口的完整URL (例如 "https://plailish.wliwan.com/upload?path=/topics") Returns: requests.Response: 服务器的响应对象 """ # 检查本地文件是否存在 if not os.path.exists(file_path): print(f"错误:文件 {file_path} 不存在。") return None # 准备文件数据 # 注意:这里的 'file' 是常见的字段名,但具体需要查看接口文档 # 有时也可能是 'upload_file', 'image' 等。如果上传失败,请尝试修改这个键名。 file_name = os.path.basename(file_path) files = { 'file': (file_name, open(file_path, 'rb'), 'application/octet-stream') } # 准备其他可能的表单数据 # data = { # 'description': '这是一个测试文件', # } # 准备请求头,有些接口需要特定的头信息 headers = { # 'Authorization': 'Bearer YOUR_TOKEN', # 如果接口需要认证 # 'User-Agent': 'Mozilla/5.0' # 模拟浏览器 } try: # 发送POST请求上传文件 # 接口很可能要求使用 POST 方法(根据405错误推断) print(f"正在上传文件 {file_name} 到 {upload_url}...") # 注意:params 参数可以用来添加URL参数,但这里已经在 upload_url 中包含了 response = requests.post(upload_url, files=files, headers=headers, timeout=30) # response = requests.post(upload_url, files=files, data=data, headers=headers) # 如果有额外数据 # 打印服务器响应 print(f"服务器响应状态码: {response.status_code}") # print(f"服务器响应内容: {response.text}") # 检查是否上传成功 response.raise_for_status() # 如果状态码不是200,会抛出异常 print("文件上传成功!") return response except requests.exceptions.RequestException as e: print(f"上传过程中出现错误: {e}") return None except Exception as e: print(f"发生未知错误: {e}") return None # --- 使用示例 --- # 获取当前时间戳,作为上次更新时间的初始值 last_update_time = time() - 10 # 初始化为10秒前,确保第一次运行时会检测到更新 def main(): global last_update_time # 1. 遍历web\\topics\\目录下html文件,找到最新的一个 topics_dir = "web\\topics\\" html_files = [f for f in os.listdir(topics_dir) if f.endswith('.html')] if not html_files: print("没有找到任何HTML文件。请确保 web\\topics\\ 目录下有HTML文件。") exit(1) local_file = os.path.join(topics_dir, max(html_files, key=lambda f: os.path.getctime(os.path.join(topics_dir, f)))) update_time = os.path.getmtime(local_file) if update_time <= last_update_time: return date_modified = time() - update_time last_update_time = update_time print(f"找到最新的HTML文件: {local_file} (修改时间: {date_modified:.2f}秒前)") file_name = os.path.basename(local_file) path="/topics/" + file_name # 3. 设置上传的目标URL upload_url = "http://liwan.uunat.com/upload?path=/topics" remove_url = "http://liwan.uunat.com/rm?path=" + path remove_file(remove_url) # 4. 执行上传 upload_file(local_file, upload_url) if __name__ == "__main__": while True: main() print("等待500ms后再次检查更新"+"."*random.randint(1, 5)+"\r", end="") print("等待500ms后再次检查更新 \r", end="") sleep(0.5) # 每500毫秒检查一次