This commit is contained in:
27942
2026-01-26 23:26:53 +08:00
parent 137ee13870
commit f7627e8f3b
2 changed files with 99 additions and 45 deletions

View File

@@ -230,32 +230,33 @@ class WorkerThread(QThread):
if video_file_paths:
self.log_message.emit(f"找到 {len(video_file_paths)} 个视频文件,开始批量上传...")
self.progress.emit(30)
result = pdd.action1(folder_path=video_file_paths)
# 定义实时回调函数:每处理完一个视频就立即通知 GUI 更新状态
def on_video_done(result_item):
try:
payload = dict(result_item)
payload["user_id"] = config.get("多多id", "")
# 确保index字段正确传递
if "index" not in payload or not payload.get("index"):
for vid_file in video_file_paths:
if str(vid_file.get('path')) == str(payload.get('path')):
payload["index"] = vid_file.get("index", config.get("序号", ""))
break
if "index" not in payload or not payload.get("index"):
payload["index"] = config.get("序号", "")
# 立即通知 GUI 更新状态
self.item_result.emit(payload)
self.log_message.emit(f" {'' if payload.get('ok') else ''} {payload.get('name', '')} 已更新状态")
except Exception as e:
self.log_message.emit(f" 回调处理异常: {e}")
result = pdd.action1(folder_path=video_file_paths, on_item_done=on_video_done)
ok = bool(result.get("ok")) if isinstance(result, dict) else True
if not ok:
fails = [r for r in (result.get("results") or []) if not r.get("ok")]
self.log_message.emit(f"发布校验失败条数: {len(fails)}")
for r in fails[:20]:
self.log_message.emit(f"{r.get('name')} | {r.get('reason')}")
# 逐条回传结果给GUI更新表格状态
if isinstance(result, dict):
for r in (result.get("results") or []):
try:
payload = dict(r)
payload["user_id"] = config.get("多多id", "")
# 确保index字段正确传递从原始文件信息中获取
if "index" not in payload or not payload.get("index"):
# 如果payload中没有index尝试从对应的视频文件中获取
for vid_file in video_file_paths:
if vid_file.get('path') == payload.get('path') or str(vid_file.get('path')) == str(payload.get('path')):
payload["index"] = vid_file.get("index", config.get("序号", ""))
break
# 如果还是找不到,使用配置中的序号
if "index" not in payload or not payload.get("index"):
payload["index"] = config.get("序号", "")
self.item_result.emit(payload)
except Exception:
pass
self.log_message.emit(f"批量上传视频完成,共处理 {len(video_file_paths)} 个视频")
else:
self.log_message.emit("未找到视频文件,跳过视频上传")
@@ -4022,7 +4023,7 @@ class MainWindow(QMainWindow):
count = task.get('count', 1)
config_indices = task.get('config_indices', [])
# 更新进度
# 更新进度日志
if success:
if task_type == 'batch_video':
self.log_text.append(f" ✓ 批量上传 {count} 个视频完成")
@@ -4030,27 +4031,32 @@ class MainWindow(QMainWindow):
self.log_text.append(f" ✓ 单个视频上传完成")
elif task_type == 'image_folder':
self.log_text.append(f" ✓ 图片文件夹上传完成")
# 立即更新表格为"已完成",并增加成功条数
for cfg_idx in config_indices:
self._update_table_status(cfg_idx, "已完成", is_config_index=True)
self.batch_success_count += len(config_indices)
else:
self.log_text.append(f" ✗ 任务失败: {message}")
# 立即更新表格为"失败",并增加失败条数
for cfg_idx in config_indices:
self._update_table_status(cfg_idx, "失败", is_config_index=True)
self.batch_failed_count += len(config_indices)
# 更新统计(完成一条就更新一条,成功/失败卡片实时刷新)
self.batch_processed += count
self.batch_pending_tasks = max(self.batch_pending_tasks - count, 0)
self.set_status_cards(
pending=self.batch_pending_tasks,
success=self.batch_success_count,
failed=self.batch_failed_count
)
self.set_running_progress(self.batch_processed, self.batch_total_tasks)
# 对于 batch_video 类型,状态和计数已经在 _on_worker_item_result 中实时更新了
# 所以这里不再重复更新,只需要处理 single_video 和 image_folder 类型
if task_type != 'batch_video':
if success:
# 更新表格为"已完成",并增加成功条数
for cfg_idx in config_indices:
self._update_table_status(cfg_idx, "已完成", is_config_index=True)
self.batch_success_count += len(config_indices)
else:
# 更新表格为"失败",并增加失败条数
for cfg_idx in config_indices:
self._update_table_status(cfg_idx, "失败", is_config_index=True)
self.batch_failed_count += len(config_indices)
# 更新统计single_video 和 image_folder 需要在这里更新)
self.batch_processed += count
self.batch_pending_tasks = max(self.batch_pending_tasks - count, 0)
self.set_status_cards(
pending=self.batch_pending_tasks,
success=self.batch_success_count,
failed=self.batch_failed_count
)
self.set_running_progress(self.batch_processed, self.batch_total_tasks)
# 移动到下一个任务
self.current_batch_task_index += 1
@@ -4114,7 +4120,7 @@ class MainWindow(QMainWindow):
self._row_map_by_user_index = m
def _on_worker_item_result(self, payload):
"""接收自动化侧逐条结果"""
"""接收自动化侧逐条结果 - 每处理完一条就立即更新状态和计数"""
try:
if not isinstance(payload, dict):
return
@@ -4129,6 +4135,8 @@ class MainWindow(QMainWindow):
# 全局搜索匹配的多多ID和序号
found_config_idx = -1
updated_count = 0 # 记录更新了多少条配置
if idx: # 如果有序号优先通过多多ID和序号匹配
for i, cfg in enumerate(self.configs):
if str(cfg.get("多多id")) == user_id and str(cfg.get("序号")) == idx:
@@ -4155,11 +4163,13 @@ class MainWindow(QMainWindow):
for cfg_idx in task_config_indices:
if cfg_idx in matching_indices:
self._update_table_status(cfg_idx, "已完成" if ok else "失败", is_config_index=True)
updated_count += 1
found_config_idx = -2 # 标记为已处理(多个配置)
if found_config_idx >= 0:
# 单个配置匹配成功,更新状态
self._update_table_status(found_config_idx, "已完成" if ok else "失败", is_config_index=True)
updated_count = 1
label = name if name else payload.get("path", "") or ""
self.log_text.append(f"[结果] {user_id}-{idx}: {'' if ok else ''} {label} {reason}")
elif found_config_idx == -2:
@@ -4168,6 +4178,28 @@ class MainWindow(QMainWindow):
self.log_text.append(f"[结果] {user_id}-{idx}: {'' if ok else ''} {label} {reason}")
else:
self.log_text.append(f"[结果] {user_id}-{idx}: ok={ok} {reason} (未在列表中找到匹配项)")
# 立即更新成功/失败计数和状态卡片(实时反馈)
if updated_count > 0:
if ok:
self.batch_success_count += updated_count
else:
self.batch_failed_count += updated_count
# 更新待处理数量
self.batch_pending_tasks = max(self.batch_pending_tasks - updated_count, 0)
# 立即刷新状态卡片显示
self.set_status_cards(
pending=self.batch_pending_tasks,
success=self.batch_success_count,
failed=self.batch_failed_count
)
# 更新进度
self.batch_processed += updated_count
self.set_running_progress(self.batch_processed, self.batch_total_tasks)
except Exception as e:
logger.warning(f"处理单条结果失败: {e}")

32
main.py
View File

@@ -853,9 +853,14 @@ class Pdd:
creator_tab.close()
return {"ok": publish_ok, "reason": publish_reason}
def action1(self, folder_path=None, input_delay=0):
def action1(self, folder_path=None, input_delay=0, on_item_done=None):
"""
批量上传视频,针对每个视频单独处理详情、定时任务和绑定任务
Args:
folder_path: 视频文件列表
input_delay: 输入延迟时间
on_item_done: 回调函数,每处理完一个视频后调用,参数为 dict: {index, path, name, ok, reason}
"""
logger.info("=" * 50)
logger.info("开始执行 action1 方法(批量上传模式)")
@@ -1166,14 +1171,23 @@ class Pdd:
import traceback
traceback.print_exc()
results.append({
result_item = {
"index": str(video_info.get("index", "")),
"path": str(video_path),
"name": video_name,
"ok": bool(ok),
"reason": str(reason),
})
}
results.append(result_item)
logger.info(f" ✓ 视频 {video_name} 处理完成ok={ok}")
# 实时回调通知 GUI 更新状态(每处理完一个视频就通知)
if on_item_done and callable(on_item_done):
try:
on_item_done(result_item)
except Exception as e:
logger.warning(f"回调通知失败: {e}")
time.sleep(2) # 每个视频处理间隔
except Exception as e:
@@ -1182,13 +1196,21 @@ class Pdd:
import traceback
traceback.print_exc()
try:
results.append({
error_result = {
"index": str(video_info.get("index", "")),
"path": str(video_info.get("path", "")),
"name": getattr(video_info.get("path", None), "name", "unknown"),
"ok": False,
"reason": f"处理异常: {e}",
})
}
results.append(error_result)
# 实时回调通知 GUI 更新状态(失败情况)
if on_item_done and callable(on_item_done):
try:
on_item_done(error_result)
except Exception:
pass
except Exception:
pass
continue