feat: 立即开始停止学习并整理学时历史与界面
This commit is contained in:
@@ -116,6 +116,45 @@ def _by_closeness(items: list[dict], key: str) -> list[dict]:
|
||||
return ordered
|
||||
|
||||
|
||||
_HISTORY_ROW_RE = re.compile(
|
||||
r'<tr class="alter">\s*'
|
||||
r'<td class="text-left" title="([^"]*)">([\s\S]*?)</td>\s*'
|
||||
r'<td class="text-center">\s*(\d+)\s*</td>\s*'
|
||||
r'<td class="text-center">\s*([^<]+?)\s*</td>',
|
||||
re.I,
|
||||
)
|
||||
|
||||
|
||||
def _history_api_item(row: object) -> dict | None:
|
||||
if not isinstance(row, dict):
|
||||
return None
|
||||
title = str(row.get("targetName") or row.get("title") or row.get("kngName") or "").strip()
|
||||
studied_at = str(row.get("studyDate") or row.get("studiedAt") or row.get("createTime") or "").strip()
|
||||
raw_minutes = row.get("duration")
|
||||
if raw_minutes is None:
|
||||
raw_minutes = row.get("studyHour")
|
||||
minutes: int | None = None
|
||||
if isinstance(raw_minutes, bool):
|
||||
minutes = None
|
||||
elif isinstance(raw_minutes, (int, float)):
|
||||
minutes = int(raw_minutes)
|
||||
elif isinstance(raw_minutes, str) and raw_minutes.strip():
|
||||
match = re.search(r"(\d+)", raw_minutes)
|
||||
minutes = int(match.group(1)) if match else None
|
||||
path = str(row.get("url") or row.get("knowledgeUrl") or row.get("targetUrl") or "").strip()
|
||||
knowledge_id = str(row.get("knowledgeId") or row.get("kngId") or "").replace("-", "")
|
||||
if not path and len(knowledge_id) == 32:
|
||||
path = f"/kng/view/document/{knowledge_id}.html"
|
||||
if not title and not studied_at:
|
||||
return None
|
||||
return {
|
||||
"title": title,
|
||||
"minutes": minutes,
|
||||
"studied_at": studied_at,
|
||||
"path": path,
|
||||
}
|
||||
|
||||
|
||||
def parse_training_cards(page: str, status: str) -> list[dict]:
|
||||
items = []
|
||||
for block in re.findall(r"<li class='el-plan-li-3'[\s\S]*?</li>", page):
|
||||
@@ -756,6 +795,57 @@ class EunivClient:
|
||||
def month_hours(self) -> str:
|
||||
return self._period_minutes().get("month", "")
|
||||
|
||||
def study_history(self) -> list[dict]:
|
||||
"""读取「我的学时」列表:来源、获得学时(分钟)、日期。"""
|
||||
try:
|
||||
items = self._study_history_api()
|
||||
except Exception:
|
||||
items = None
|
||||
if items is not None:
|
||||
return items
|
||||
return self._study_history_page()
|
||||
|
||||
def _study_history_api(self) -> list[dict] | None:
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{STUDY_API}/v1/styhis/studyhistory/daily/search",
|
||||
json={"pageable": False},
|
||||
)
|
||||
payload = response.json()
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
rows = payload.get("datas")
|
||||
if rows is None:
|
||||
rows = payload.get("data")
|
||||
if not isinstance(rows, list):
|
||||
return None
|
||||
items = []
|
||||
for row in rows:
|
||||
item = _history_api_item(row)
|
||||
if item is not None:
|
||||
items.append(item)
|
||||
return items
|
||||
|
||||
def _study_history_page(self) -> list[dict]:
|
||||
text = self._study_period_page()
|
||||
items = []
|
||||
for match in _HISTORY_ROW_RE.finditer(text):
|
||||
title = unescape(match.group(1) or "").strip()
|
||||
cell = match.group(2) or ""
|
||||
minutes = int(match.group(3))
|
||||
studied_at = unescape(match.group(4) or "").strip()
|
||||
path_match = re.search(r"GoToSource\('([^']+)'", cell)
|
||||
path = path_match.group(1) if path_match else ""
|
||||
items.append(
|
||||
{
|
||||
"title": title,
|
||||
"minutes": minutes,
|
||||
"studied_at": studied_at,
|
||||
"path": path,
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
def _period_minutes(self) -> dict[str, str]:
|
||||
text = self._study_period_page()
|
||||
found = {}
|
||||
|
||||
Reference in New Issue
Block a user