common.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """公共工具:异常、停顿、步骤重试。"""
  2. from __future__ import annotations
  3. import random
  4. import time
  5. class SkipVideoError(Exception):
  6. """当前视频不满足采集条件(如一级评论不足),应跳过并切下一条。"""
  7. AI_SUMMARY_PROMPT = "用20~30个字总结该视频内容"
  8. def human_pause(min_s: float = 1.5, max_s: float = 3.5) -> None:
  9. seconds = random.uniform(min_s, max_s)
  10. print(f"等待 {seconds:.1f}s ...")
  11. time.sleep(seconds)
  12. def run_step(name: str, action) -> bool:
  13. for attempt in (1, 2):
  14. try:
  15. print(f"[{name}] 开始(第 {attempt} 次)")
  16. action()
  17. print(f"[{name}] 完成")
  18. return True
  19. except Exception as exc:
  20. print(f"[{name}] 失败: {exc}")
  21. if attempt == 1:
  22. print(f"[{name}] 准备重试一次...")
  23. human_pause(2.0, 4.0)
  24. else:
  25. print(f"[{name}] 重试仍失败,跳过该步骤,浏览器保持打开。")
  26. return False