fix: 浏览内容进度实时显示

This commit is contained in:
2025-12-16 21:19:48 +08:00
parent 2abb9ab494
commit 1b707fdace
18 changed files with 89 additions and 41 deletions

View File

@@ -64,6 +64,7 @@ class APIBrowser:
self.log_callback = log_callback
self.stop_flag = False
self._closed = False # 防止重复关闭
self.last_total_records = 0
# 设置代理
if proxy_config and proxy_config.get("server"):
@@ -267,6 +268,7 @@ class APIBrowser:
# 获取总页数
total_pages = 1
next_page_url = None
total_records = 0
page_content = soup.find(id='PageContent')
if page_content:
@@ -282,6 +284,10 @@ class APIBrowser:
if next_href:
next_page_url = f"{BASE_URL}/admin/{next_href}"
try:
self.last_total_records = int(total_records or 0)
except Exception:
self.last_total_records = 0
return articles, total_pages, next_page_url
except Exception as e:
@@ -338,14 +344,19 @@ class APIBrowser:
except:
return False
def browse_content(self, browse_type: str,
should_stop_callback: Optional[Callable] = None) -> APIBrowseResult:
def browse_content(
self,
browse_type: str,
should_stop_callback: Optional[Callable] = None,
progress_callback: Optional[Callable] = None,
) -> APIBrowseResult:
"""
浏览内容并标记已读
Args:
browse_type: 浏览类型 (应读/注册前未读)
should_stop_callback: 检查是否应该停止的回调函数
progress_callback: 进度回调(可选),用于实时上报已浏览内容数量
Returns:
浏览结果
@@ -386,6 +397,24 @@ class APIBrowser:
if next_url:
base_url = next_url
total_records = int(getattr(self, "last_total_records", 0) or 0)
last_report_ts = 0.0
def report_progress(force: bool = False):
nonlocal last_report_ts
if not progress_callback:
return
now_ts = time.time()
if not force and now_ts - last_report_ts < 1.0:
return
last_report_ts = now_ts
try:
progress_callback({"total_items": total_records, "browsed_items": total_items})
except Exception:
pass
report_progress(force=True)
# 处理所有页面
while True:
if should_stop_callback and should_stop_callback():
@@ -398,6 +427,7 @@ class APIBrowser:
title = article['title'][:30]
total_items += 1
report_progress()
# 获取附件
attachments = self.get_article_attachments(article['href'])
@@ -425,6 +455,7 @@ class APIBrowser:
time.sleep(0.2)
report_progress(force=True)
self.log(f"[API] 浏览完成: {total_items} 条内容,{total_attachments} 个附件")
result.success = True