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