fix: 心跳同时补发网站上的学习日志
This commit is contained in:
@@ -604,6 +604,8 @@ class StudySession:
|
|||||||
self._proxy = parse_proxy(proxy)
|
self._proxy = parse_proxy(proxy)
|
||||||
self._employee_id = employee_id
|
self._employee_id = employee_id
|
||||||
self._use_pool = use_pool
|
self._use_pool = use_pool
|
||||||
|
self._referer = EUNIV + "/"
|
||||||
|
self._submit_logs = 0
|
||||||
self._client: httpx.AsyncClient | None = None
|
self._client: httpx.AsyncClient | None = None
|
||||||
|
|
||||||
async def __aenter__(self) -> "StudySession":
|
async def __aenter__(self) -> "StudySession":
|
||||||
@@ -727,18 +729,44 @@ class StudySession:
|
|||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
async def post_site_log(self, name: str, body: dict) -> None:
|
||||||
|
if getattr(self, "_client", None) is None:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
await self._http(
|
||||||
|
"POST",
|
||||||
|
f"{EUNIV}/kng/services/KngComService.svc/{name}",
|
||||||
|
headers={
|
||||||
|
"Content-Type": "text/json",
|
||||||
|
"Origin": EUNIV,
|
||||||
|
"Referer": self._referer or (EUNIV + "/"),
|
||||||
|
"Accept": "application/json, text/javascript, */*",
|
||||||
|
"User-Agent": USER_AGENT,
|
||||||
|
},
|
||||||
|
content=json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
log.info("网站日志 %s 没有发出", name)
|
||||||
|
|
||||||
async def submit_video_heartbeat(self, kid: str, pkg_id: str,
|
async def submit_video_heartbeat(self, kid: str, pkg_id: str,
|
||||||
study_time: int, view_schedule: int) -> dict:
|
study_time: int, view_schedule: int) -> dict:
|
||||||
param = study_progress_fields(
|
param = study_progress_fields(
|
||||||
kid, pkg_id, page_size=1, study_time=study_time,
|
kid, pkg_id, page_size=1, study_time=study_time,
|
||||||
kind=0, view_schedule=view_schedule,
|
kind=0, view_schedule=view_schedule,
|
||||||
)
|
)
|
||||||
encryption = await self.get_encrypt_request(json.dumps(param, separators=(",", ":")))
|
plain = json.dumps(param, ensure_ascii=False, separators=(",", ":"))
|
||||||
|
encryption = await self.get_encrypt_request(plain)
|
||||||
param["encryption"] = encryption
|
param["encryption"] = encryption
|
||||||
param["multiple"] = 1
|
param["multiple"] = 1
|
||||||
param["realHour"] = study_time
|
param["realHour"] = study_time
|
||||||
resp = await self._api("POST", f"{STUDY_API}/v1/study/submitpro",
|
resp = await self._api("POST", f"{STUDY_API}/v1/study/submitpro",
|
||||||
content=json.dumps(param, separators=(",", ":")))
|
content=json.dumps(param, separators=(",", ":")))
|
||||||
|
self._submit_logs = getattr(self, "_submit_logs", 0) + 1
|
||||||
|
await self.post_site_log("CreateSubmitLog", {
|
||||||
|
"kngId": as_uuid(kid),
|
||||||
|
"apiUrl": f"视频心跳,知识 {as_uuid(kid)},第 {self._submit_logs} 次,实际 {study_time} 秒,位置 {view_schedule}",
|
||||||
|
"context": plain,
|
||||||
|
})
|
||||||
text = resp.text.strip()
|
text = resp.text.strip()
|
||||||
return resp.json() if text else {}
|
return resp.json() if text else {}
|
||||||
|
|
||||||
@@ -749,11 +777,18 @@ class StudySession:
|
|||||||
kid, pkg_id, page_size=page_size, study_time=study_time,
|
kid, pkg_id, page_size=page_size, study_time=study_time,
|
||||||
kind=1, view_schedule=view_schedule,
|
kind=1, view_schedule=view_schedule,
|
||||||
)
|
)
|
||||||
encryption = await self.get_encrypt_request(json.dumps(param, separators=(",", ":")))
|
plain = json.dumps(param, ensure_ascii=False, separators=(",", ":"))
|
||||||
|
encryption = await self.get_encrypt_request(plain)
|
||||||
param["multiple"] = 1
|
param["multiple"] = 1
|
||||||
param["realHour"] = study_time
|
param["realHour"] = study_time
|
||||||
await self._api("POST", f"{STUDY_API}/v1/study/submit?encryption={encryption}",
|
await self._api("POST", f"{STUDY_API}/v1/study/submit?encryption={encryption}",
|
||||||
content=json.dumps(param, separators=(",", ":")))
|
content=json.dumps(param, separators=(",", ":")))
|
||||||
|
self._submit_logs = getattr(self, "_submit_logs", 0) + 1
|
||||||
|
await self.post_site_log("CreateSubmitLog", {
|
||||||
|
"kngId": as_uuid(kid),
|
||||||
|
"apiUrl": f"文档心跳,知识 {as_uuid(kid)},第 {self._submit_logs} 次,实际 {study_time} 秒",
|
||||||
|
"context": plain,
|
||||||
|
})
|
||||||
|
|
||||||
async def submit_scorm_heartbeat(self, kid: str, pkg_id: str,
|
async def submit_scorm_heartbeat(self, kid: str, pkg_id: str,
|
||||||
study_time: int, sco_id: str = "",
|
study_time: int, sco_id: str = "",
|
||||||
@@ -762,13 +797,22 @@ class StudySession:
|
|||||||
kid, pkg_id, page_size=-1 if end else 1, study_time=study_time,
|
kid, pkg_id, page_size=-1 if end else 1, study_time=study_time,
|
||||||
kind=2, view_schedule=0, end=end, chapter_id=sco_id, for_encrypt=True,
|
kind=2, view_schedule=0, end=end, chapter_id=sco_id, for_encrypt=True,
|
||||||
)
|
)
|
||||||
encryption = await self.get_encrypt_request(json.dumps(encrypt_param, separators=(",", ":")))
|
plain = json.dumps(encrypt_param, ensure_ascii=False, separators=(",", ":"))
|
||||||
|
encryption = await self.get_encrypt_request(plain)
|
||||||
param = study_progress_fields(
|
param = study_progress_fields(
|
||||||
kid, pkg_id, page_size=-1 if end else 1, study_time=study_time,
|
kid, pkg_id, page_size=-1 if end else 1, study_time=study_time,
|
||||||
kind=2, view_schedule=0, end=end, chapter_id=sco_id,
|
kind=2, view_schedule=0, end=end, chapter_id=sco_id,
|
||||||
)
|
)
|
||||||
|
param["multiple"] = 1
|
||||||
|
param["realHour"] = study_time
|
||||||
await self._api("POST", f"{STUDY_API}/v1/study/submit?encryption={encryption}",
|
await self._api("POST", f"{STUDY_API}/v1/study/submit?encryption={encryption}",
|
||||||
content=json.dumps(param, separators=(",", ":")))
|
content=json.dumps(param, separators=(",", ":")))
|
||||||
|
self._submit_logs = getattr(self, "_submit_logs", 0) + 1
|
||||||
|
await self.post_site_log("CreateSubmitLog", {
|
||||||
|
"kngId": as_uuid(kid),
|
||||||
|
"apiUrl": f"Scorm 心跳,知识 {as_uuid(kid)},第 {self._submit_logs} 次,实际 {study_time} 秒",
|
||||||
|
"context": plain,
|
||||||
|
})
|
||||||
|
|
||||||
async def scorm_count(self, kid: str) -> int:
|
async def scorm_count(self, kid: str) -> int:
|
||||||
resp = await self._api("GET", f"{STUDY_API}/v1/kng/scorm/count/",
|
resp = await self._api("GET", f"{STUDY_API}/v1/kng/scorm/count/",
|
||||||
@@ -930,6 +974,16 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
|
|||||||
view = 0
|
view = 0
|
||||||
started = False
|
started = False
|
||||||
stall = _Stall()
|
stall = _Stall()
|
||||||
|
if not getattr(session, "_play_logged", False):
|
||||||
|
session._play_logged = True
|
||||||
|
poster = getattr(session, "post_site_log", None)
|
||||||
|
if poster is not None:
|
||||||
|
await poster("CreateActionLog", {
|
||||||
|
"kngId": as_uuid(kid),
|
||||||
|
"backNo": f"{int(time.time() * 1000)}_1",
|
||||||
|
"type": "play",
|
||||||
|
"context": "开始播放",
|
||||||
|
})
|
||||||
while not _halted(stop, hours):
|
while not _halted(stop, hours):
|
||||||
try:
|
try:
|
||||||
progress = await session.update_progress(kid, pkg_id)
|
progress = await session.update_progress(kid, pkg_id)
|
||||||
@@ -1460,7 +1514,10 @@ class AutoStudyWorker:
|
|||||||
_set_user_status(employee_id, course=title)
|
_set_user_status(employee_id, course=title)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
params = await _open_play(session, _play_path(kind, path, kid))
|
play_path = _play_path(kind, path, kid)
|
||||||
|
session._referer = f"{EUNIV}{play_path}"
|
||||||
|
session._play_logged = False
|
||||||
|
params = await _open_play(session, play_path)
|
||||||
if ctype == "package":
|
if ctype == "package":
|
||||||
_touch(worker, course=title, progress=0, ok=True)
|
_touch(worker, course=title, progress=0, ok=True)
|
||||||
await _study_package(session, kid, intervals, auto_mode,
|
await _study_package(session, kid, intervals, auto_mode,
|
||||||
|
|||||||
Reference in New Issue
Block a user