fix: preserve data granularity and recover tasks safely

This commit is contained in:
Codex
2026-10-11 14:18:56 +08:00
parent 23f3a15a15
commit 603184549b
29 changed files with 4557 additions and 418 deletions
+17 -6
View File
@@ -60,14 +60,24 @@ class AuthService:
self.browser_thread = None
try:
self._load(self.vault.load())
except RuntimeError:
except (RuntimeError, OSError, ValueError):
self.message = "保存的 Cookie 无法解密,请重新登录"
def _load(self, cookies):
if isinstance(cookies, dict):
cookies = [{"name": name, "value": value, "domain": ".chsi.com.cn", "path": "/"} for name, value in cookies.items()]
with self.transport.lock:
self.transport.session.cookies.clear()
for name, value in cookies.items():
self.transport.session.cookies.set(name, value, domain=".chsi.com.cn", path="/")
for cookie in cookies:
domain = str(cookie.get("domain", ".chsi.com.cn"))
if domain.lstrip(".") != "chsi.com.cn" and not domain.endswith(".chsi.com.cn"):
continue
expires = cookie.get("expires")
self.transport.session.cookies.set(cookie["name"], cookie["value"], domain=domain, path=cookie.get("path", "/"), secure=bool(cookie.get("secure")), expires=int(expires) if expires and expires > 0 else None)
def _snapshot(self):
with self.transport.lock:
return [{"name": c.name, "value": c.value, "domain": c.domain, "path": c.path, "secure": c.secure, "expires": c.expires} for c in self.transport.session.cookies]
def status(self):
return {"saved": self.vault.path.exists(), "valid": self.valid, "message": self.message, "browser_state": self.browser_state, "browser_message": self.browser_message}
@@ -91,7 +101,8 @@ class AuthService:
return self.status()
def check(self):
response = self.transport.request("POST", "https://yz.chsi.com.cn/zsml/rs/dws.do", data={"ssdm": "11", "curPage": 1, "start": 0, "pageSize": 1, "dwlxs[0]": "all"})
# The school/major indexes can be public while direction details require login.
response = self.transport.request("POST", "https://yz.chsi.com.cn/zsml/rs/yjfxs.do", data={"dwdm": "10001", "zydm": "081200", "zymc": "计算机科学与技术", "curPage": 1, "start": 0, "pageSize": 1, "dwlxs[0]": "all"})
try:
body = response.json()
except ValueError:
@@ -101,7 +112,7 @@ class AuthService:
self.valid = body.get("flag") is True and isinstance(body.get("msg"), dict)
self.message = "研招目录接口验证成功" if self.valid else "登录未生效或数据源限流,请在浏览器完成登录后重试"
if self.valid:
self.vault.save(self.transport.session.cookies.get_dict())
self.vault.save(self._snapshot())
return self.status()
def login(self, username, password):
@@ -144,7 +155,7 @@ class AuthService:
while time.monotonic() < deadline:
page.wait_for_timeout(2000)
if "yz.chsi.com.cn" in page.url:
cookies = {c["name"]: c["value"] for c in context.cookies() if "chsi.com.cn" in c["domain"]}
cookies = [c for c in context.cookies() if c["domain"].lstrip(".") == "chsi.com.cn" or c["domain"].endswith(".chsi.com.cn")]
if cookies:
self._load(cookies)
if self.check()["valid"]: