diff --git a/services/screenshots.py b/services/screenshots.py index 6923ab7..b1cd7d0 100644 --- a/services/screenshots.py +++ b/services/screenshots.py @@ -39,6 +39,36 @@ def _resolve_wkhtmltoimage_path() -> str | None: 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: """确保有可用的登录 cookies(通过 API 登录刷新)""" try: @@ -89,7 +119,12 @@ def take_screenshot_wkhtmltoimage( cmd.extend(["--quality", str(_WKHTMLTOIMAGE_QUALITY)]) if cookies_path: - cmd.extend(["--cookie-jar", 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]) if proxy_server: cmd.extend(["--proxy", proxy_server])