From ba787a513b734089e83ce2ae72cff392d87434b3 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Thu, 19 Nov 2026 02:48:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8C=89=E6=97=A5=E6=9C=9F=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E4=BF=9D=E7=95=99=E7=BF=BB=E9=A1=B5=E7=8A=B6?= =?UTF-8?q?=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/client.py | 71 ++++++++++++++++++++++++++++++++------- backend/tests/test_api.py | 11 ++++++ 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 77ea723..114549a 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -375,6 +375,50 @@ def day_span(when: datetime) -> tuple[str, str]: return text, text +def ajax_hidden_fields(payload: str) -> dict[str, str]: + """读出我的学时翻页回包里的视图状态。回包不是整页,隐藏域在末尾的分段里。""" + fields: dict[str, str] = {} + text = payload or "" + index = 0 + size = len(text) + while index < size: + bar = text.find("|", index) + if bar < 0: + break + length_text = text[index:bar] + if not length_text.isdigit(): + index = bar + 1 + continue + length = int(length_text) + index = bar + 1 + bar = text.find("|", index) + if bar < 0: + break + kind = text[index:bar] + index = bar + 1 + bar = text.find("|", index) + if bar < 0: + break + field_id = text[index:bar] + index = bar + 1 + if index + length > size: + break + content = text[index:index + length] + index += length + if index < size and text[index] == "|": + index += 1 + if kind == "hiddenField": + fields[field_id] = content + return fields + + +def _carry_state(form: dict[str, str], html: str) -> dict[str, str]: + for key, value in ajax_hidden_fields(html).items(): + if key.startswith("__"): + form[key] = value + return form + + def period_search_form(html: str, start: str, end: str) -> dict[str, str]: form = training_form(html) form["txtStartDate"] = start @@ -385,7 +429,7 @@ def period_search_form(html: str, start: str, end: str) -> dict[str, str]: form["__EVENTARGUMENT"] = "" form["__ASYNCPOST"] = "true" form["btnSearch"] = "搜索" - return form + return _carry_state(form, html) def period_form(html: str, argument: str, page_nav: str | None = None) -> dict[str, str]: @@ -396,7 +440,7 @@ def period_form(html: str, argument: str, page_nav: str | None = None) -> dict[s form["__ASYNCPOST"] = "true" if page_nav is not None: form["pageNav"] = page_nav - return form + return _carry_state(form, html) def period_has_next(html: str, page: int) -> bool: @@ -1396,15 +1440,6 @@ class EunivClient: def _rows_for_range(self, start: str | None = None, end: str | None = None) -> list[dict]: """读取我的学时表格。给出日期时先按这个范围搜索,再按每页 100 条取完。""" html = self._open_period() - if start: - html = self._post_period(period_search_form(html, start, end or start)) - nav = training_form(html).get("pageNav", "") - if not nav.endswith("|100"): - form = period_form(html, "current", "1|0|100") - if start: - form["txtStartDate"] = start - form["txtEndDate"] = end or start - html = self._post_period(form) items: list[dict] = [] seen: set[tuple] = set() @@ -1419,7 +1454,19 @@ class EunivClient: added += 1 return added - take(html) + if start: + html = self._post_period(period_search_form(html, start, end or start)) + take(html) + nav = training_form(html).get("pageNav", "") + if not nav.endswith("|100"): + form = period_form(html, "current", "1|0|100") + if start: + form["txtStartDate"] = start + form["txtEndDate"] = end or start + widened = self._post_period(form) + if history_rows_from_page(widened): + html = widened + take(html) limited = False for page in range(2, 51): if not period_has_next(html, page - 1): diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index da802e3..c75776b 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1212,6 +1212,17 @@ def test_history_course_path_follows_type_and_keeps_explicit_url() -> None: assert named["path"] == "/kng/view/package/abc.html" +def test_ajax_reply_keeps_the_next_page_state() -> None: + from euniv.client import ajax_hidden_fields, period_form + + html = "1|#||4|5|updatePanel|upList||8|hiddenField|__VIEWSTATEGENERATOR|A3C7C079|3|hiddenField|__VIEWSTATE|abc|" + assert ajax_hidden_fields(html)["__VIEWSTATE"] == "abc" + assert ajax_hidden_fields(html)["__VIEWSTATEGENERATOR"] == "A3C7C079" + form = period_form(html, "2") + assert form["__VIEWSTATE"] == "abc" + assert form["__EVENTARGUMENT"] == "2" + + def test_month_span_covers_the_whole_month() -> None: from euniv.client import day_span, month_span, period_search_form