feat: 学习进度轮询、学时依据与首页代理弹窗
This commit is contained in:
+134
-17
@@ -155,6 +155,117 @@ def _history_api_item(row: object) -> dict | None:
|
||||
}
|
||||
|
||||
|
||||
HISTORY_PAGE_SIZE = 100
|
||||
|
||||
|
||||
def history_search_body(pageable: bool, offset: int = 0, limit: int = HISTORY_PAGE_SIZE) -> dict:
|
||||
body: dict = {"pageable": pageable}
|
||||
if pageable:
|
||||
page = offset // limit + 1 if limit else 1
|
||||
body.update(
|
||||
{
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"page": page,
|
||||
"pageSize": limit,
|
||||
"start": offset,
|
||||
"length": limit,
|
||||
}
|
||||
)
|
||||
return body
|
||||
|
||||
|
||||
def history_items_from_payload(payload: object) -> list[dict] | None:
|
||||
if not isinstance(payload, dict):
|
||||
return None
|
||||
rows = payload.get("datas")
|
||||
if rows is None:
|
||||
rows = payload.get("data")
|
||||
if isinstance(rows, dict):
|
||||
rows = rows.get("datas") or rows.get("list") or rows.get("records") or rows.get("items")
|
||||
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 history_stamp(studied_at: str) -> datetime | None:
|
||||
text = (studied_at or "").strip().replace("T", " ")
|
||||
if not text:
|
||||
return None
|
||||
text = text[:19]
|
||||
for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d"):
|
||||
try:
|
||||
return datetime.strptime(text, fmt)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def history_month_side(studied_at: str, year: int, month: int) -> str:
|
||||
stamp = history_stamp(studied_at)
|
||||
if stamp is None:
|
||||
return "unknown"
|
||||
if (stamp.year, stamp.month) == (year, month):
|
||||
return "in"
|
||||
if (stamp.year, stamp.month) < (year, month):
|
||||
return "before"
|
||||
return "after"
|
||||
|
||||
|
||||
def fold_month_minutes(items: list[dict], year: int, month: int) -> tuple[int, bool]:
|
||||
total = 0
|
||||
reached_before = False
|
||||
for item in items:
|
||||
side = history_month_side(str(item.get("studied_at") or ""), year, month)
|
||||
if side == "in":
|
||||
minutes = item.get("minutes")
|
||||
if isinstance(minutes, int):
|
||||
total += minutes
|
||||
elif side == "before":
|
||||
reached_before = True
|
||||
break
|
||||
return total, reached_before
|
||||
|
||||
|
||||
def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = HISTORY_PAGE_SIZE) -> int:
|
||||
"""倒序分页累加本月分钟,遇到上个月即停。"""
|
||||
offset = 0
|
||||
total = 0
|
||||
first_mark: tuple | None = None
|
||||
for _ in range(50):
|
||||
rows = fetch_page(offset, page_size)
|
||||
if rows is None:
|
||||
if offset == 0:
|
||||
raise RuntimeError("学时历史没有获取到")
|
||||
return total
|
||||
if not rows:
|
||||
return total
|
||||
add, done = fold_month_minutes(rows, year, month)
|
||||
total += add
|
||||
if done:
|
||||
return total
|
||||
if len(rows) != page_size:
|
||||
return total
|
||||
mark = (
|
||||
len(rows),
|
||||
rows[0].get("studied_at"),
|
||||
rows[0].get("title"),
|
||||
rows[-1].get("studied_at"),
|
||||
rows[-1].get("title"),
|
||||
)
|
||||
if offset == 0:
|
||||
first_mark = mark
|
||||
elif mark == first_mark:
|
||||
return total
|
||||
offset += page_size
|
||||
return total
|
||||
|
||||
|
||||
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):
|
||||
@@ -798,33 +909,39 @@ class EunivClient:
|
||||
def study_history(self) -> list[dict]:
|
||||
"""读取「我的学时」列表:来源、获得学时(分钟)、日期。"""
|
||||
try:
|
||||
items = self._study_history_api()
|
||||
items = self._study_history_search(pageable=False)
|
||||
except Exception:
|
||||
items = None
|
||||
if items is not None:
|
||||
return items
|
||||
return self._study_history_page()
|
||||
|
||||
def _study_history_api(self) -> list[dict] | None:
|
||||
def month_history_minutes(self, now: datetime | None = None) -> int:
|
||||
"""倒序分页统计本月学时分钟,遇到上个月即停。"""
|
||||
from euniv.study_window import CN_TZ
|
||||
|
||||
current = now or datetime.now(CN_TZ)
|
||||
year, month = current.year, current.month
|
||||
|
||||
def fetch_page(offset: int, limit: int) -> list[dict] | None:
|
||||
return self._study_history_search(pageable=True, offset=offset, limit=limit)
|
||||
|
||||
try:
|
||||
return accumulate_month_minutes(fetch_page, year, month)
|
||||
except Exception:
|
||||
items = self.study_history()
|
||||
total, _ = fold_month_minutes(items, year, month)
|
||||
return total
|
||||
|
||||
def _study_history_search(
|
||||
self, *, pageable: bool, offset: int = 0, limit: int = HISTORY_PAGE_SIZE
|
||||
) -> list[dict] | None:
|
||||
response = self._request(
|
||||
"POST",
|
||||
f"{STUDY_API}/v1/styhis/studyhistory/daily/search",
|
||||
json={"pageable": False},
|
||||
json=history_search_body(pageable, offset, limit),
|
||||
)
|
||||
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
|
||||
return history_items_from_payload(response.json())
|
||||
|
||||
def _study_history_page(self) -> list[dict]:
|
||||
text = self._study_period_page()
|
||||
|
||||
Reference in New Issue
Block a user