fix: 学时统一从我的学时页面按日期统计
This commit is contained in:
+28
-30
@@ -1308,50 +1308,48 @@ class StudySession:
|
||||
return m.group(1) if m else ""
|
||||
|
||||
async def month_history_minutes(self, now: datetime | None = None) -> int:
|
||||
from euniv.client import history_month_side, history_rows_from_page, period_form, period_has_next, training_form
|
||||
from euniv.client import _minutes_of, history_rows_from_page, month_span, period_form, period_has_next, period_search_form, training_form
|
||||
from euniv.study_window import CN_TZ
|
||||
|
||||
current = now or datetime.now(CN_TZ)
|
||||
year, month = current.year, current.month
|
||||
if current.tzinfo is None:
|
||||
current = current.replace(tzinfo=CN_TZ)
|
||||
else:
|
||||
current = current.astimezone(CN_TZ)
|
||||
start, end = month_span(current.year, current.month)
|
||||
url = f"{EUNIV}/study/myperiod.htm"
|
||||
headers = {"User-Agent": USER_AGENT, "Referer": EUNIV + "/sty/index.htm", "Origin": EUNIV}
|
||||
resp = await self._http("GET", url, headers=headers)
|
||||
html = resp.text
|
||||
nav = training_form(html).get("pageNav", "")
|
||||
if nav and not nav.endswith("|100"):
|
||||
resp = await self._http("POST", url, data=period_form(html, "current", "1|0|100"), headers=headers)
|
||||
html = resp.text
|
||||
total = 0
|
||||
seen: set[tuple] = set()
|
||||
|
||||
def absorb(page_html: str) -> bool:
|
||||
nonlocal total
|
||||
for row in history_rows_from_page(page_html):
|
||||
key = (row.get("title"), row.get("studied_at"), row.get("minutes"), row.get("path"))
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
side = history_month_side(str(row.get("studied_at") or ""), year, month)
|
||||
if side == "before":
|
||||
return True
|
||||
minutes = row.get("minutes")
|
||||
if side == "in" and isinstance(minutes, int) and not isinstance(minutes, bool):
|
||||
total += minutes
|
||||
return False
|
||||
|
||||
if "spMonthlyPeriod" not in html and 'class="alter"' not in html:
|
||||
raise RuntimeError("学时历史没有获取到")
|
||||
if absorb(html):
|
||||
return total
|
||||
resp = await self._http("POST", url, data=period_search_form(html, start, end), headers=headers)
|
||||
html = resp.text
|
||||
if not training_form(html).get("pageNav", "").endswith("|100"):
|
||||
form = period_form(html, "current", "1|0|100")
|
||||
form["txtStartDate"] = start
|
||||
form["txtEndDate"] = end
|
||||
resp = await self._http("POST", url, data=form, headers=headers)
|
||||
html = resp.text
|
||||
rows = history_rows_from_page(html)
|
||||
seen = {(row.get("title"), row.get("studied_at"), row.get("minutes"), row.get("path")) for row in rows}
|
||||
for page in range(2, 51):
|
||||
if not period_has_next(html, page - 1):
|
||||
break
|
||||
before = len(seen)
|
||||
resp = await self._http("POST", url, data=period_form(html, str(page)), headers=headers)
|
||||
form = period_form(html, str(page))
|
||||
form["txtStartDate"] = start
|
||||
form["txtEndDate"] = end
|
||||
resp = await self._http("POST", url, data=form, headers=headers)
|
||||
html = resp.text
|
||||
if absorb(html) or len(seen) == before:
|
||||
fresh = [
|
||||
row for row in history_rows_from_page(html)
|
||||
if (row.get("title"), row.get("studied_at"), row.get("minutes"), row.get("path")) not in seen
|
||||
]
|
||||
if not fresh:
|
||||
break
|
||||
return total
|
||||
rows.extend(fresh)
|
||||
seen.update((row.get("title"), row.get("studied_at"), row.get("minutes"), row.get("path")) for row in fresh)
|
||||
return _minutes_of(rows, current.year, current.month)
|
||||
|
||||
def for_course(self) -> "CourseSession":
|
||||
return CourseSession(self)
|
||||
|
||||
Reference in New Issue
Block a user