Spaces:
Runtime error
Runtime error
| from flask import Flask, render_template_string, jsonify | |
| import subprocess | |
| import threading | |
| app = Flask(__name__) | |
| # HTML 页面 | |
| HTML_PAGE = """ | |
| <!DOCTYPE html> | |
| <html lang="zh"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>手动同步</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; margin: 20px; } | |
| #logs { width: 100%; height: 300px; border: 1px solid #ccc; padding: 10px; overflow-y: scroll; background: #f4f4f4; } | |
| button { padding: 10px 15px; font-size: 16px; } | |
| </style> | |
| <script> | |
| function syncNow() { | |
| let btn = document.getElementById("sync-btn"); | |
| btn.disabled = true; | |
| fetch('/start_sync', { method: 'POST' }) | |
| .then(response => response.json()) | |
| .then(data => { | |
| alert(data.message); | |
| btn.disabled = false; | |
| updateLogs(); | |
| }); | |
| } | |
| function updateLogs() { | |
| fetch('/logs') | |
| .then(response => response.text()) | |
| .then(text => { | |
| document.getElementById("logs").innerText = text; | |
| }); | |
| } | |
| setInterval(updateLogs, 2000); | |
| </script> | |
| </head> | |
| <body> | |
| <h2>手动同步管理</h2> | |
| <button id="sync-btn" onclick="syncNow()">立即同步</button> | |
| <pre id="logs">正在加载日志...</pre> | |
| </body> | |
| </html> | |
| """ | |
| # Web 界面 | |
| def sync_page(): | |
| return render_template_string(HTML_PAGE) | |
| # 启动同步 | |
| def start_sync(): | |
| def run_sync(): | |
| subprocess.run(["/bin/bash", "sync_now.sh"]) | |
| threading.Thread(target=run_sync, daemon=True).start() | |
| return jsonify({"status": "success", "message": "手动同步开始,请等待..."}) | |
| # 获取日志内容 | |
| def get_logs(): | |
| try: | |
| with open("/tmp/sync_log.txt", "r", encoding="utf-8") as f: | |
| return f.read() | |
| except FileNotFoundError: | |
| return "暂无日志..." | |
| # 启动 Flask | |
| if __name__ == '__main__': | |
| app.run(host="0.0.0.0", port=8080) |