fix: 心跳摘要改为在线 GetEncryptRequest 不再写死密钥

This commit is contained in:
2026-11-15 01:40:12 +08:00
parent caa79d79b7
commit ecd774507f
5 changed files with 344 additions and 56 deletions
+12 -17
View File
@@ -3,7 +3,6 @@
from __future__ import annotations
import base64
import hashlib
import io
import json
import random
@@ -102,9 +101,12 @@ def _remember_site(account_id: str, client: httpx.Client) -> None:
_site_sessions[account_id] = (time.time(), dumped)
def _load_cookies(client: httpx.Client, dumped: list[tuple[str, str, str, str]]) -> None:
def _load_cookies(client: httpx.Client | httpx.AsyncClient, dumped: list[tuple[str, str, str, str]]) -> None:
for name, value, domain, path in dumped:
client.cookies.set(name, value, domain=domain, path=path)
kwargs: dict[str, str] = {"path": path or "/"}
if domain:
kwargs["domain"] = domain
client.cookies.set(name, value, **kwargs)
def _by_closeness(items: list[dict], key: str) -> list[dict]:
@@ -365,14 +367,6 @@ def jigsaw_offset(big_png: bytes, small_png: bytes, top: int) -> int:
return best_x
STUDY_MD5_KEY = "60db8c0a-72d8-4b8f-bb45-c4c3f29a2696"
def study_encryption(body_json: str) -> str:
"""播放页 RteApiSite.js 的摘要:MD5(进度 JSON + 固定 key),与 GetEncryptRequest 相同。"""
return hashlib.md5((body_json + STUDY_MD5_KEY).encode("utf-8")).hexdigest()
class SessionExpired(RuntimeError):
"""学习平台凭证失效,需要重新登录。"""
@@ -918,6 +912,8 @@ class EunivClient:
def course_page_params(self, path: str) -> dict:
"""打开播放页,解析 JS 变量。返回 knowledgeType, submitProTimes, validtimeSpan, standardStudyHours, pageSize 等。"""
html = self._site_page(path)
from euniv.encrypt import note_play_html
note_play_html(html, EUNIV)
def _var(name: str, default: str = "") -> str:
m = re.search(rf'(?:var\s+)?{name}\s*=\s*["\']?([^"\';\r\n]+)', html)
return m.group(1).strip() if m else default
@@ -954,7 +950,9 @@ class EunivClient:
return resp.json()
def get_encrypt_request(self, body_json: str) -> str:
return study_encryption(body_json)
from euniv.encrypt import fetch_encryption
self._ensure_site()
return fetch_encryption(self.client, body_json, origin=EUNIV, user_agent=USER_AGENT)
def submit_video_heartbeat(self, knowledge_id: str, package_id: str,
study_time: int, view_schedule: int,
@@ -1038,11 +1036,8 @@ class EunivClient:
"studyChapterIds": sco_id,
"viewSchedule": 0,
}
if end:
encryption = study_encryption(json.dumps(param, separators=(",", ":")))
else:
encrypt_body = json.dumps(param, separators=(",", ":"))
encryption = self.get_encrypt_request(encrypt_body)
encrypt_body = json.dumps(param, separators=(",", ":"))
encryption = self.get_encrypt_request(encrypt_body)
param["multiple"] = 1
param["realHour"] = study_time
resp = self._request("POST", f"{STUDY_API}/v1/study/submit?encryption={encryption}",