fix: wkhtmltoimage使用安全cookie
This commit is contained in:
@@ -39,6 +39,36 @@ def _resolve_wkhtmltoimage_path() -> str | None:
|
|||||||
return os.environ.get("WKHTMLTOIMAGE_PATH") or shutil.which("wkhtmltoimage")
|
return os.environ.get("WKHTMLTOIMAGE_PATH") or shutil.which("wkhtmltoimage")
|
||||||
|
|
||||||
|
|
||||||
|
def _read_cookie_pairs(cookies_path: str) -> list[tuple[str, str]]:
|
||||||
|
if not cookies_path or not os.path.exists(cookies_path):
|
||||||
|
return []
|
||||||
|
pairs = []
|
||||||
|
try:
|
||||||
|
with open(cookies_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
parts = line.split("\t")
|
||||||
|
if len(parts) < 7:
|
||||||
|
continue
|
||||||
|
name = parts[5].strip()
|
||||||
|
value = parts[6].strip()
|
||||||
|
if name:
|
||||||
|
pairs.append((name, value))
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
|
||||||
|
def _select_cookie_pairs(pairs: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
||||||
|
preferred_names = {"ASP.NET_SessionId", ".ASPXAUTH"}
|
||||||
|
preferred = [(name, value) for name, value in pairs if name in preferred_names and value]
|
||||||
|
if preferred:
|
||||||
|
return preferred
|
||||||
|
return [(name, value) for name, value in pairs if name and value and name.isascii() and value.isascii()]
|
||||||
|
|
||||||
|
|
||||||
def _ensure_login_cookies(account, proxy_config, log_callback) -> bool:
|
def _ensure_login_cookies(account, proxy_config, log_callback) -> bool:
|
||||||
"""确保有可用的登录 cookies(通过 API 登录刷新)"""
|
"""确保有可用的登录 cookies(通过 API 登录刷新)"""
|
||||||
try:
|
try:
|
||||||
@@ -89,6 +119,11 @@ def take_screenshot_wkhtmltoimage(
|
|||||||
cmd.extend(["--quality", str(_WKHTMLTOIMAGE_QUALITY)])
|
cmd.extend(["--quality", str(_WKHTMLTOIMAGE_QUALITY)])
|
||||||
|
|
||||||
if cookies_path:
|
if cookies_path:
|
||||||
|
cookie_pairs = _select_cookie_pairs(_read_cookie_pairs(cookies_path))
|
||||||
|
if cookie_pairs:
|
||||||
|
for name, value in cookie_pairs:
|
||||||
|
cmd.extend(["--cookie", name, value])
|
||||||
|
else:
|
||||||
cmd.extend(["--cookie-jar", cookies_path])
|
cmd.extend(["--cookie-jar", cookies_path])
|
||||||
|
|
||||||
if proxy_server:
|
if proxy_server:
|
||||||
|
|||||||
Reference in New Issue
Block a user