replace screenshot pipeline and update admin
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
API 浏览器 - 用纯 HTTP 请求实现浏览功能
|
||||
比 Playwright 快 30-60 倍
|
||||
比传统浏览器自动化快 30-60 倍
|
||||
"""
|
||||
|
||||
import requests
|
||||
@@ -44,6 +44,27 @@ except Exception:
|
||||
_API_DIAGNOSTIC_SLOW_MS = max(0, _API_DIAGNOSTIC_SLOW_MS)
|
||||
|
||||
_cookie_domain_fallback = urlsplit(BASE_URL).hostname or "postoa.aidunsoft.com"
|
||||
_COOKIE_JAR_MAX_AGE_SECONDS = 24 * 60 * 60
|
||||
|
||||
|
||||
def get_cookie_jar_path(username: str) -> str:
|
||||
"""获取截图用的 cookies 文件路径(Netscape Cookie 格式)"""
|
||||
import hashlib
|
||||
|
||||
os.makedirs(COOKIES_DIR, exist_ok=True)
|
||||
filename = hashlib.sha256(username.encode()).hexdigest()[:32] + ".cookies.txt"
|
||||
return os.path.join(COOKIES_DIR, filename)
|
||||
|
||||
|
||||
def is_cookie_jar_fresh(cookie_path: str, max_age_seconds: int = _COOKIE_JAR_MAX_AGE_SECONDS) -> bool:
|
||||
"""判断 cookies 文件是否存在且未过期"""
|
||||
if not cookie_path or not os.path.exists(cookie_path):
|
||||
return False
|
||||
try:
|
||||
file_age = time.time() - os.path.getmtime(cookie_path)
|
||||
return file_age <= max(0, int(max_age_seconds or 0))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
_api_browser_instances: "weakref.WeakSet[APIBrowser]" = weakref.WeakSet()
|
||||
|
||||
@@ -102,38 +123,37 @@ class APIBrowser:
|
||||
"""记录日志"""
|
||||
if self.log_callback:
|
||||
self.log_callback(message)
|
||||
def save_cookies_for_playwright(self, username: str):
|
||||
"""保存cookies供Playwright使用"""
|
||||
import os
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
os.makedirs(COOKIES_DIR, exist_ok=True)
|
||||
|
||||
# 安全修复:使用SHA256代替MD5作为文件名哈希
|
||||
filename = hashlib.sha256(username.encode()).hexdigest()[:32] + '.json'
|
||||
cookies_path = os.path.join(COOKIES_DIR, filename)
|
||||
|
||||
def save_cookies_for_screenshot(self, username: str):
|
||||
"""保存 cookies 供 wkhtmltoimage 使用(Netscape Cookie 格式)"""
|
||||
cookies_path = get_cookie_jar_path(username)
|
||||
try:
|
||||
# 获取requests session的cookies
|
||||
cookies_list = []
|
||||
lines = [
|
||||
"# Netscape HTTP Cookie File",
|
||||
"# This file was generated by zsglpt",
|
||||
]
|
||||
for cookie in self.session.cookies:
|
||||
cookies_list.append({
|
||||
'name': cookie.name,
|
||||
'value': cookie.value,
|
||||
'domain': cookie.domain or _cookie_domain_fallback,
|
||||
'path': cookie.path or '/',
|
||||
})
|
||||
|
||||
# Playwright storage_state 格式
|
||||
storage_state = {
|
||||
'cookies': cookies_list,
|
||||
'origins': []
|
||||
}
|
||||
|
||||
with open(cookies_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(storage_state, f)
|
||||
|
||||
domain = cookie.domain or _cookie_domain_fallback
|
||||
include_subdomains = "TRUE" if domain.startswith(".") else "FALSE"
|
||||
path = cookie.path or "/"
|
||||
secure = "TRUE" if getattr(cookie, "secure", False) else "FALSE"
|
||||
expires = int(getattr(cookie, "expires", 0) or 0)
|
||||
lines.append(
|
||||
"\t".join(
|
||||
[
|
||||
domain,
|
||||
include_subdomains,
|
||||
path,
|
||||
secure,
|
||||
str(expires),
|
||||
cookie.name,
|
||||
cookie.value,
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
with open(cookies_path, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
|
||||
self.log(f"[API] Cookies已保存供截图使用")
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user