feat: 心跳间隔改由每个用户自己设置
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""每个账号自己的心跳间隔。默认自动获取,手动值只决定隔多久发一次。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
AUTO = "auto"
|
||||
MANUAL = "manual"
|
||||
VIDEO_DEFAULT = 30
|
||||
DOCUMENT_DEFAULT = 120
|
||||
SCORM_DEFAULT = 120
|
||||
VIDEO_MAX = 300
|
||||
OTHER_MAX = 600
|
||||
|
||||
|
||||
class HeartbeatError(ValueError):
|
||||
"""心跳间隔不符合要求。"""
|
||||
|
||||
|
||||
def _mode(value: object) -> str:
|
||||
text = str(value or "").strip().lower()
|
||||
if text not in (AUTO, MANUAL):
|
||||
raise HeartbeatError("心跳间隔须为自动获取或手动设定")
|
||||
return text
|
||||
|
||||
|
||||
def _seconds(value: object, high: int, label: str) -> int:
|
||||
if isinstance(value, bool) or not isinstance(value, int) or not 0 <= value <= high:
|
||||
raise HeartbeatError(f"{label}须为 0 到 {high} 的整数")
|
||||
return value
|
||||
|
||||
|
||||
def mode_of(account) -> str:
|
||||
raw = str(getattr(account, "heartbeat_mode", AUTO) or AUTO).strip().lower()
|
||||
return raw if raw in (AUTO, MANUAL) else AUTO
|
||||
|
||||
|
||||
def video_of(account) -> int:
|
||||
return _stored(getattr(account, "heartbeat_video_seconds", VIDEO_DEFAULT), VIDEO_DEFAULT, VIDEO_MAX)
|
||||
|
||||
|
||||
def document_of(account) -> int:
|
||||
return _stored(getattr(account, "heartbeat_document_seconds", DOCUMENT_DEFAULT), DOCUMENT_DEFAULT, OTHER_MAX)
|
||||
|
||||
|
||||
def scorm_of(account) -> int:
|
||||
return _stored(getattr(account, "heartbeat_scorm_seconds", SCORM_DEFAULT), SCORM_DEFAULT, OTHER_MAX)
|
||||
|
||||
|
||||
def _stored(value: object, default: int, high: int) -> int:
|
||||
try:
|
||||
number = int(value) # type: ignore[arg-type]
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
if number < 0 or number > high:
|
||||
return default
|
||||
return number
|
||||
|
||||
|
||||
def view_of(account) -> dict:
|
||||
return {
|
||||
"heartbeat_mode": mode_of(account),
|
||||
"heartbeat_video_seconds": video_of(account),
|
||||
"heartbeat_document_seconds": document_of(account),
|
||||
"heartbeat_scorm_seconds": scorm_of(account),
|
||||
}
|
||||
|
||||
|
||||
def apply(account, mode: object | None, video: object | None, document: object | None, scorm: object | None) -> None:
|
||||
chosen = mode_of(account) if mode is None else _mode(mode)
|
||||
video_value = video_of(account) if video is None else _seconds(video, VIDEO_MAX, "视频心跳间隔")
|
||||
document_value = document_of(account) if document is None else _seconds(document, OTHER_MAX, "文档心跳间隔")
|
||||
scorm_value = scorm_of(account) if scorm is None else _seconds(scorm, OTHER_MAX, "Scorm 心跳间隔")
|
||||
account.heartbeat_mode = chosen
|
||||
account.heartbeat_video_seconds = video_value
|
||||
account.heartbeat_document_seconds = document_value
|
||||
account.heartbeat_scorm_seconds = scorm_value
|
||||
|
||||
|
||||
def intervals_of(account) -> tuple[dict, bool]:
|
||||
"""返回等待秒和是否自动获取。申报的 studyTime 仍由播放页决定。"""
|
||||
return {
|
||||
"video": video_of(account),
|
||||
"document": document_of(account),
|
||||
"scorm": scorm_of(account),
|
||||
}, mode_of(account) == AUTO
|
||||
Reference in New Issue
Block a user