diff --git a/douyin/detection/detection_pkg/finder.py b/douyin/detection/detection_pkg/finder.py index a4eaf51..c150f09 100644 --- a/douyin/detection/detection_pkg/finder.py +++ b/douyin/detection/detection_pkg/finder.py @@ -118,82 +118,31 @@ def check_have_fudai_with_ready_screenshot( break # 每次循环都重新截图(除了第一次如果已经提供了截图) - if not (screenshot_ready and loop == 0): - try: - analyser._shot_and_diagnose( - "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False - ) - except Exception: - # 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性(避免半写入/截断图片导致后续 PIL 打开失败) - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - # 简单验证:文件存在、大小足够且能由 PIL 打开/加载 - valid = False - try: - if ( - os.path.exists(pic1_path) - and os.path.getsize(pic1_path) >= 1024 - ): - from PIL import Image as PILImage + from douyin.detection.detection_pkg.finder_screenshot import ( + capture_screenshot_for_loop, + handle_screenshot_load_failure, + load_screenshot_with_retry, + ) - with PILImage.open(pic1_path) as _im: - _im.verify() - valid = True - except Exception: - valid = False - if not valid: - # 再尝试一次直接截图作为最后回退 - try: - analyser.operation.get_screenshot( - analyser.device_id, use_service=False - ) - except Exception: - pass - except Exception: - pass + capture_screenshot_for_loop(analyser, screenshot_ready, loop, pic1_path) # 读取截图 - pic = None - try: - pic = Image.open(pic1_path) - pic.load() - except Exception: - # 如果读取失败,尝试重新截图 - try: - analyser.operation.get_screenshot(analyser.device_id, use_service=False) - pic = Image.open(pic1_path) - pic.load() - except Exception: - pass + pic = load_screenshot_with_retry(analyser, pic1_path) # 如果截图读取失败,等待后继续下一次循环 if pic is None: loop += 1 consecutive_failures += 1 - # 如果连续失败太多次,提前退出 - if consecutive_failures >= max_consecutive_failures: - try: - analyser._log_kv( - "[找福袋图标] 警告", - "连续截图失败次数过多,提前退出", - consecutive_failures=consecutive_failures, - elapsed_s=round(time.time() - func_start, 2), - ) - except Exception: - pass + action = handle_screenshot_load_failure( + analyser, + consecutive_failures, + max_consecutive_failures, + func_start, + max_wait_s, + retry_delay_s, + ) + if action == "break": break - # 检查是否还有剩余时间 - elapsed = time.time() - func_start - if elapsed < max_wait_s: - remaining_time = max_wait_s - elapsed - sleep_time = min(retry_delay_s, remaining_time) - if sleep_time > 0: - try: - analyser.operation.delay(sleep_time) - except Exception: - time.sleep(sleep_time) continue pic_new = pic.convert("RGBA") diff --git a/douyin/detection/detection_pkg/finder_screenshot.py b/douyin/detection/detection_pkg/finder_screenshot.py new file mode 100644 index 0000000..5baed51 --- /dev/null +++ b/douyin/detection/detection_pkg/finder_screenshot.py @@ -0,0 +1,111 @@ +"""find_fudai 辅助:截图获取 + 验证 + 重试 + +从 douyin/detection/detection_pkg/finder.py:check_have_fudai_with_ready_screenshot 中提取。 +""" +from __future__ import annotations + +import os +import time +from typing import Any, Optional, Tuple + + +def capture_screenshot_for_loop(analyser: Any, screenshot_ready: bool, loop: int, pic1_path: str) -> None: + """每次循环开始时获取截图(除了第一次如果已提供)。 + + 流程: + 1. 调用 _shot_and_diagnose 截图(diagnose=False) + 2. 失败时调用 get_screenshot 并用 PIL 验证文件完整性 + 3. 如验证失败再尝试一次截图作为最后回退 + """ + if screenshot_ready and loop == 0: + return + + try: + analyser._shot_and_diagnose( + "找福袋图标", "判定是否存在福袋", analyser.device_id, diagnose=False + ) + except Exception: + # 如果截图失败,尝试直接调用 get_screenshot 并验证文件可读性 + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + valid = False + try: + if ( + os.path.exists(pic1_path) + and os.path.getsize(pic1_path) >= 1024 + ): + from PIL import Image as PILImage + + with PILImage.open(pic1_path) as _im: + _im.verify() + valid = True + except Exception: + valid = False + if not valid: + # 再尝试一次直接截图作为最后回退 + try: + analyser.operation.get_screenshot( + analyser.device_id, use_service=False + ) + except Exception: + pass + except Exception: + pass + + +def load_screenshot_with_retry(analyser: Any, pic1_path: str): + """读取截图,失败时重新截图后重试。返回 PIL Image 或 None。""" + from PIL import Image + + pic = None + try: + pic = Image.open(pic1_path) + pic.load() + except Exception: + try: + analyser.operation.get_screenshot(analyser.device_id, use_service=False) + pic = Image.open(pic1_path) + pic.load() + except Exception: + pass + return pic + + +def handle_screenshot_load_failure( + analyser: Any, + consecutive_failures: int, + max_consecutive_failures: int, + func_start: float, + max_wait_s: float, + retry_delay_s: float, +) -> str: + """截图读取失败时的处理: + + - 连续失败超过阈值 → 写警告日志,返回 "break"(外层 while 应 break) + - 否则等待 retry_delay_s 秒后返回 "continue"(外层 while 应 continue) + """ + if consecutive_failures >= max_consecutive_failures: + try: + analyser._log_kv( + "[找福袋图标] 警告", + "连续截图失败次数过多,提前退出", + consecutive_failures=consecutive_failures, + elapsed_s=round(time.time() - func_start, 2), + ) + except Exception: + pass + return "break" + + # 检查是否还有剩余时间 + elapsed = time.time() - func_start + if elapsed < max_wait_s: + remaining_time = max_wait_s - elapsed + sleep_time = min(retry_delay_s, remaining_time) + if sleep_time > 0: + try: + analyser.operation.delay(sleep_time) + except Exception: + time.sleep(sleep_time) + return "continue"