fix ui
This commit is contained in:
parent
a6bf93ebbc
commit
a604050d6c
@ -49,6 +49,7 @@ class PixivSpider:
|
||||
self.headers = None
|
||||
self.current_ranking_data = []
|
||||
self.failed_works = []
|
||||
self.log_messages = []
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
"""设置Rich界面组件"""
|
||||
@ -71,54 +72,44 @@ class PixivSpider:
|
||||
TextColumn("[bold blue]{task.description}"),
|
||||
BarColumn(),
|
||||
TextColumn("{task.percentage:>3.0f}%"),
|
||||
TextColumn("[bold green]{task.fields[speed]}"),
|
||||
console=self.console,
|
||||
expand=True,
|
||||
transient=False # 保持进度条显示
|
||||
)
|
||||
|
||||
# 设置日志面板 - 滚动显示在进度条上方
|
||||
self.log_messages = []
|
||||
self.layout["main"].update(
|
||||
Panel(
|
||||
Group(*self.log_messages),
|
||||
title="下载状态",
|
||||
border_style="green"
|
||||
)
|
||||
transient=False
|
||||
)
|
||||
|
||||
# 设置进度条任务
|
||||
self.main_task_id = self.progress.add_task(
|
||||
"总体进度",
|
||||
total=self.TOTAL_IMAGES,
|
||||
speed="0.00 t / 秒"
|
||||
total=self.TOTAL_IMAGES
|
||||
)
|
||||
|
||||
def _update_log(self, message: str) -> None:
|
||||
"""更新日志显示"""
|
||||
# 检查是否为重复消息
|
||||
def _update_log(self, message: str, speed: float = 0.0) -> None:
|
||||
"""更新日志信息"""
|
||||
if not self.log_messages or message != self.log_messages[-1]:
|
||||
self.log_messages.append(message)
|
||||
self.log_messages.insert(0, message)
|
||||
if len(self.log_messages) > 100:
|
||||
self.log_messages = self.log_messages[-100:]
|
||||
|
||||
try:
|
||||
# 构建消息显示,确保顺序正确
|
||||
messages = self.log_messages[-30:] # 只显示最新的30条
|
||||
|
||||
# 更新日志面板
|
||||
self.layout["main"].update(
|
||||
Panel(
|
||||
Group(*messages),
|
||||
title="下载状态",
|
||||
subtitle=f"显示最新 {len(messages)}/{len(self.log_messages)} 条消息",
|
||||
border_style="green",
|
||||
padding=(0, 1)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
# 防止界面更新错误影响主程序
|
||||
print(f"界面更新出错: {e}")
|
||||
self.log_messages = self.log_messages[:100]
|
||||
|
||||
messages = self.log_messages[:10]
|
||||
|
||||
# 清空控制台
|
||||
self.console.clear()
|
||||
|
||||
# 重新渲染布局
|
||||
self.console.print(self.layout)
|
||||
|
||||
# 更新日志面板
|
||||
log_content = "\n".join(messages)
|
||||
log_panel = Panel(
|
||||
log_content,
|
||||
title="下载状态",
|
||||
subtitle=f"显示最新 {len(messages)}/{len(self.log_messages)} 条消息, 速度: {speed:.2f} t/s",
|
||||
border_style="green",
|
||||
padding=(1, 2),
|
||||
expand=False
|
||||
)
|
||||
self.layout["main"].update(log_panel)
|
||||
|
||||
|
||||
def _setup_session(self) -> None:
|
||||
"""设置请求会话"""
|
||||
@ -177,6 +168,7 @@ class PixivSpider:
|
||||
# 处理排行榜页面
|
||||
# 保存开始时间用于计算速度
|
||||
start_time = time.time()
|
||||
last_update_time = start_time
|
||||
completed_works = 0
|
||||
|
||||
for page in range(1, 11):
|
||||
@ -188,17 +180,24 @@ class PixivSpider:
|
||||
else:
|
||||
completed_works += 1
|
||||
# 计算实际速度(作品/秒)
|
||||
elapsed_time = max(1, int(time.time() - start_time))
|
||||
speed = completed_works / elapsed_time
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
|
||||
# 更新进度和速度
|
||||
# 每秒更新一次速度
|
||||
completed_works += 1
|
||||
# 计算实际速度(作品/秒)
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
|
||||
# 每次下载图片后更新速度
|
||||
if elapsed_time > 0: # 避免除以零错误
|
||||
speed = completed_works / elapsed_time
|
||||
self._update_log(f"当前速度: {speed:.2f} t / s", speed)
|
||||
# 更新进度
|
||||
self.progress.update(
|
||||
self.main_task_id,
|
||||
completed=completed_works, # 使用绝对值而不是增量
|
||||
refresh=True, # 强制刷新显示
|
||||
completed=completed_works
|
||||
)
|
||||
# 单独设置速度字段
|
||||
self.progress.tasks[self.main_task_id].fields["speed"] = f"{speed:.2f} t / 秒"
|
||||
# 更新UI显示
|
||||
self.layout["progress"].update(self.progress)
|
||||
|
||||
@ -208,6 +207,6 @@ class PixivSpider:
|
||||
|
||||
# 清理失败作品的记录
|
||||
for work_id in self.failed_works:
|
||||
self.redis.client.delete(work_id)
|
||||
self.redis.client().delete(work_id)
|
||||
|
||||
self._update_log('[green]爬虫运行完成[/green]')
|
||||
|
Loading…
Reference in New Issue
Block a user