From f9fb8478d62b5eba43c9a1e21b12e6847ffd7b85 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 25 Jun 2026 06:57:25 +0800 Subject: [PATCH] =?UTF-8?q?@=20=E4=BF=AE=E5=A4=8D=EF=BC=9A=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=8F=AF=E7=A7=BB=E6=A4=8D=E6=80=A7=E3=80=81=E5=AF=86?= =?UTF-8?q?=E9=92=A5=E6=8C=81=E4=B9=85=E5=8C=96=E3=80=81=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E7=BC=BA=E5=A4=B1API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 硬编码路径改为环境变量 + 项目相对路径 - secret_key 持久化到 .secret_key 文件,避免重启失效 - 新增 /api/status 路由,支持前端轮询状态 - 新增 /api/reset 路由,支持前端重置会话 - .gitignore 添加 .secret_key 排除 @ --- .gitignore | 1 + app.py | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 020cefb..b9c0fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ env/ # --- Environment & Config --- .env .user.ini +.secret_key # --- Logs --- logs/ diff --git a/app.py b/app.py index d45beea..7e65f64 100644 --- a/app.py +++ b/app.py @@ -17,10 +17,9 @@ import requests from flask import Flask, render_template, request, jsonify, send_file from playwright.async_api import async_playwright -# 配置日志 -LOG_DIR = '/www/wwwlogs/douyin_cookie' -if not os.path.exists(LOG_DIR): - os.makedirs(LOG_DIR, exist_ok=True) +# 配置日志 — 优先使用环境变量,否则使用项目相对路径 +LOG_DIR = os.environ.get('LOG_DIR', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs')) +os.makedirs(LOG_DIR, exist_ok=True) handler = RotatingFileHandler( f'{LOG_DIR}/app.log', @@ -33,12 +32,22 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) app = Flask(__name__) -app.secret_key = os.urandom(24) +# 持久化 secret_key:优先使用环境变量,否则从文件读取,都不存在则生成并保存 +_secret_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.secret_key') +_secret_key = os.environ.get('SECRET_KEY') +if not _secret_key and os.path.exists(_secret_key_path): + with open(_secret_key_path, 'rb') as _f: + _secret_key = _f.read() +if not _secret_key: + _secret_key = os.urandom(24) + with open(_secret_key_path, 'wb') as _f: + _f.write(_secret_key) +app.secret_key = _secret_key app.logger.addHandler(handler) app.logger.setLevel(logging.INFO) -# 静态文件目录 -STATIC_DIR = '/www/wwwroot/douyin_cookie_extractor/static' +# 静态文件目录 — 优先使用环境变量,否则使用项目相对路径 +STATIC_DIR = os.environ.get('STATIC_DIR', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')) os.makedirs(STATIC_DIR, exist_ok=True) # 全局状态 @@ -434,5 +443,32 @@ def test_proxy(): except Exception as e: return jsonify({"status": "error", "message": str(e)}) +@app.route('/api/status', methods=['GET']) +def api_status(): + """返回当前会话状态,供前端轮询""" + elapsed = "" + if login_session.status in ("qr_ready", "scanning", "success") and login_session.start_time: + elapsed_sec = int(time.time() - login_session.start_time) + elapsed = f" (已等待 {elapsed_sec}s)" + return jsonify({ + "status": login_session.status, + "message": login_session.message + elapsed, + "proxy_used": login_session.proxy_used + }) + +@app.route('/api/reset', methods=['POST']) +def api_reset(): + """强制重置当前会话""" + try: + run_async_sync(cleanup_session()) + except Exception as e: + app.logger.warning(f"重置会话异常: {e}") + login_session.status = "idle" + login_session.message = "会话已重置" + login_session.cookies = None + login_session.start_time = 0 + login_session.proxy_used = None + return jsonify({"status": "success", "message": "会话已重置"}) + if __name__ == '__main__': app.run(debug=False, host='0.0.0.0', port=5001) \ No newline at end of file