fix: 探测整批代理加上总时限,学时翻页先判重再累加
This commit is contained in:
+20
-14
@@ -439,6 +439,24 @@ def history_hour_totals(items: list[dict], now: datetime | None = None) -> dict[
|
||||
}
|
||||
|
||||
|
||||
def take_history_page(rows: list, seen: set[tuple], year: int, month: int, page_size: int) -> tuple[int, bool]:
|
||||
"""先判断这一页是否重复。返回 (计入的分钟, 是否停止翻页)。"""
|
||||
if not rows:
|
||||
return 0, True
|
||||
mark = (
|
||||
len(rows),
|
||||
rows[0].get("studied_at"),
|
||||
rows[0].get("title"),
|
||||
rows[-1].get("studied_at"),
|
||||
rows[-1].get("title"),
|
||||
)
|
||||
if mark in seen:
|
||||
return 0, True
|
||||
seen.add(mark)
|
||||
add, done = fold_month_minutes(rows, year, month)
|
||||
return add, bool(done or len(rows) != page_size)
|
||||
|
||||
|
||||
def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = HISTORY_PAGE_SIZE) -> tuple[int, bool]:
|
||||
"""倒序分页累加本月分钟,遇到上个月即停。重复页直接返回,不再累加。"""
|
||||
offset = 0
|
||||
@@ -450,21 +468,9 @@ def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int =
|
||||
if offset == 0:
|
||||
raise RuntimeError("学时历史没有获取到")
|
||||
return total, False
|
||||
if not rows:
|
||||
return total, False
|
||||
mark = (
|
||||
len(rows),
|
||||
rows[0].get("studied_at"),
|
||||
rows[0].get("title"),
|
||||
rows[-1].get("studied_at"),
|
||||
rows[-1].get("title"),
|
||||
)
|
||||
if mark in seen:
|
||||
return total, False
|
||||
seen.add(mark)
|
||||
add, done = fold_month_minutes(rows, year, month)
|
||||
add, stop = take_history_page(rows, seen, year, month, page_size)
|
||||
total += add
|
||||
if done or len(rows) != page_size:
|
||||
if stop:
|
||||
return total, False
|
||||
offset += page_size
|
||||
return total, True
|
||||
|
||||
Reference in New Issue
Block a user