|
|
@@ -85,10 +85,42 @@ _videos_by_id: Dict[int, Dict[str, Any]] = _index_videos(_videos)
|
|
|
_app_config = _load_config(CONFIG_PATH)
|
|
|
FLASK_BASE = _app_config.get('flask_api_base', os.environ.get('FLASK_API_BASE', 'http://192.168.254.242:5050'))
|
|
|
|
|
|
+# 登录配置
|
|
|
+ADMIN_USERNAME = 'admin'
|
|
|
+ADMIN_PASSWORD = 'HNYZ0821'
|
|
|
+_session = requests.Session()
|
|
|
+_has_logged_in = False
|
|
|
+
|
|
|
+def _ensure_login():
|
|
|
+ global _has_logged_in
|
|
|
+ if _has_logged_in:
|
|
|
+ return
|
|
|
+
|
|
|
+ # 尝试常用登录路径 (先尝试 /auth/login, 再尝试 /login)
|
|
|
+ for login_path in ['/auth/login', '/login']:
|
|
|
+ url = f"{FLASK_BASE}{login_path}"
|
|
|
+ try:
|
|
|
+ # 只有当响应不是 404 时才认为是有效的登录端点
|
|
|
+ resp = _session.post(url, data={
|
|
|
+ 'username': ADMIN_USERNAME,
|
|
|
+ 'password': ADMIN_PASSWORD
|
|
|
+ }, timeout=5)
|
|
|
+
|
|
|
+ if resp.status_code != 404:
|
|
|
+ resp.raise_for_status()
|
|
|
+ logger.info(f"登录成功: {url}")
|
|
|
+ _has_logged_in = True
|
|
|
+ return
|
|
|
+ except Exception as e:
|
|
|
+ logger.warning(f"尝试登录 {url} 失败: {e}")
|
|
|
+
|
|
|
+ logger.error("无法登录 Flask API,后续请求可能会失败")
|
|
|
+
|
|
|
def _flask_get(path: str) -> Dict[str, Any]:
|
|
|
+ _ensure_login()
|
|
|
url = f"{FLASK_BASE}{path}"
|
|
|
try:
|
|
|
- resp = requests.get(url, timeout=3)
|
|
|
+ resp = _session.get(url, timeout=3)
|
|
|
resp.raise_for_status()
|
|
|
return resp.json()
|
|
|
except Exception as e:
|
|
|
@@ -96,9 +128,10 @@ def _flask_get(path: str) -> Dict[str, Any]:
|
|
|
return {"success": False, "message": str(e)}
|
|
|
|
|
|
def _flask_post(path: str, json_body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
|
+ _ensure_login()
|
|
|
url = f"{FLASK_BASE}{path}"
|
|
|
try:
|
|
|
- resp = requests.post(url, json=json_body or {}, timeout=3)
|
|
|
+ resp = _session.post(url, json=json_body or {}, timeout=3)
|
|
|
resp.raise_for_status()
|
|
|
return resp.json()
|
|
|
except Exception as e:
|