diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 46a2aad..d64f2ae 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2734,6 +2734,36 @@ class fudai_analyse: douyin_fudai_detail.close_fudai_detail(self) except Exception: pass + # 连续失败过多时,按需切换直播间 + try: + needswitch = bool(getattr(self, "needswitch", True)) + except Exception: + needswitch = True + if needswitch: + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,切换直播间", + ) + except Exception: + pass + try: + self.swipe_up_to_next_live() + except Exception: + pass + else: + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,单间模式不切换", + ) + except Exception: + pass + # 重置计数器,避免影响后续直播间 + try: + self._consecutive_attend_failures = 0 + except Exception: + pass return # 步骤1:检查是否是第二次打开福袋详情 diff --git a/douyin/fudai/core/fudai_attend.py b/douyin/fudai/core/fudai_attend.py index 5903b45..4200532 100644 --- a/douyin/fudai/core/fudai_attend.py +++ b/douyin/fudai/core/fudai_attend.py @@ -1609,6 +1609,14 @@ def attend_choujiang(analyser) -> bool: ) return True + # 未命中任何已知按钮文案时,避免死循环:限次重试并稍作等待 + click_times += 1 + if click_times < 2: + try: + analyser.operation.delay(0.2) + except Exception: + pass + analyser._log_kv( "参与", "参与抽奖失败:并行OCR结果无效", diff --git a/douyin/reservation/douyin_reservation_handler.py b/douyin/reservation/douyin_reservation_handler.py index 472e2a3..c202f19 100644 --- a/douyin/reservation/douyin_reservation_handler.py +++ b/douyin/reservation/douyin_reservation_handler.py @@ -524,10 +524,6 @@ class ReservationHandler: target_room_title = str(item.get("room_title") or "").strip() target_room_title_raw = str(item.get("room_title_raw") or "").strip() - # 记录开始时间,防止总时间过长卡死 - seek_start_time = time.time() - max_seek_duration = 30.0 # 最大30秒 - # 输出目标直播间的位置和标题信息 try: analyser._log_kv( # type: ignore[attr-defined] @@ -631,18 +627,6 @@ class ReservationHandler: except Exception: pass return False - # 检查总时间是否超过上限 - if time.time() - seek_start_time > max_seek_duration: - try: - analyser._log_kv( - operation_type, - f"[{operation_type}] _seek_reservation_room:总时间超过上限,停止回退", - elapsed_seconds=time.time() - seek_start_time, - max_duration=max_seek_duration, - ) - except Exception: - pass - return False elif pos_diff < 0: # 目标在上方(target_pos < cur_pos),需要向上回退(减少 room_pos) back_times = min(abs(pos_diff), max_back) if max_back > 0 else abs(pos_diff) @@ -695,18 +679,6 @@ class ReservationHandler: ) except Exception: pass - # 检查总时间是否超过上限 - if time.time() - seek_start_time > max_seek_duration: - try: - analyser._log_kv( - operation_type, - f"[{operation_type}] _seek_reservation_room:总时间超过上限,停止回退", - elapsed_seconds=time.time() - seek_start_time, - max_duration=max_seek_duration, - ) - except Exception: - pass - return False # 回退时设置标志,跳过记录标题(因为之前已经记录过了) try: analyser._skip_record_title_during_seek = True diff --git a/douyin/stage/douyin_stage.py b/douyin/stage/douyin_stage.py index 68644fc..4fd7591 100644 --- a/douyin/stage/douyin_stage.py +++ b/douyin/stage/douyin_stage.py @@ -332,6 +332,118 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t if not in_live_room: _log("不在直播间,执行步骤3(OCR检测推荐页/关注列表/直播列表)") + # 购物红包像素命中:不在直播间也必须进行OCR复查 + if pixel_state.get("has_redpacket_bg", False): + step_redpacket_ocr_start = time.time() + red_ratio = None + red_threshold = None + try: + redpacket_detail = ( + pixel_details.get("has_redpacket_bg", {}) + if isinstance(pixel_details, dict) + else {} + ) + red_ratio = redpacket_detail.get("red_ratio") + red_threshold = redpacket_detail.get("effective_threshold") + except Exception: + red_ratio = None + red_threshold = None + + _log( + "步骤2.5[购物红包OCR复查]-开始", + 说明="不在直播间也执行红包OCR复查", + 红色比例=( + round(float(red_ratio), 4) if red_ratio is not None else None + ), + 阈值=( + round(float(red_threshold), 4) + if red_threshold is not None + else None + ), + ) + + redpacket_verified = False + try: + if time.time() - step_redpacket_ocr_start < max_step_time: + cut_start = time.time() + analyser.cut_pic( + *SHOPPING_REDPACKET_AREA, + "", + "診斷購物紅包-非直播间复查", + ) + cut_elapsed = (time.time() - cut_start) * 1000 + _log("步骤2.5[购物红包OCR复查]-截图完成", 耗时毫秒=cut_elapsed) + + if time.time() - step_redpacket_ocr_start < max_step_time: + ocr_start = time.time() + name_rp0 = preprocess_for_ocr( + analyser, "診斷購物紅包-非直播间复查" + ) + redpacket_text = str( + analyser.operation.analyse_pic_word(name_rp0, 0) or "" + ) + ocr_elapsed = (time.time() - ocr_start) * 1000 + _log( + "步骤2.5[购物红包OCR复查]-OCR模式0", + 耗时毫秒=ocr_elapsed, + 文本长度=len(redpacket_text), + 文本预览=redpacket_text[:50] if redpacket_text else "", + ) + + redpacket_keywords = ("购物红包", "邀你抢", "抢红包") + if enable_ocr_mode1 and ( + not redpacket_text + or not any( + kw in redpacket_text for kw in redpacket_keywords + ) + ): + if time.time() - step_redpacket_ocr_start < max_step_time: + ocr_start_1 = time.time() + name_rp1 = preprocess_for_ocr( + analyser, "診斷購物紅包-非直播间复查" + ) + redpacket_text = str( + analyser.operation.analyse_pic_word(name_rp1, 1) + or "" + ) + ocr_elapsed_1 = (time.time() - ocr_start_1) * 1000 + _log( + "步骤2.5[购物红包OCR复查]-OCR模式1", + 耗时毫秒=ocr_elapsed_1, + 文本长度=len(redpacket_text), + 文本预览=( + redpacket_text[:50] if redpacket_text else "" + ), + ) + + if redpacket_text and any( + kw in redpacket_text for kw in redpacket_keywords + ): + redpacket_verified = True + except Exception as e: + redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000 + _log( + "步骤2.5[购物红包OCR复查]-异常", + 耗时毫秒=redpacket_elapsed, + 异常类型=type(e).__name__, + 异常信息=str(e), + ) + + if redpacket_verified: + total_elapsed = (time.time() - diag_start) * 1000 + redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000 + _log_summary( + "阶段诊断完成(购物红包弹窗-非直播间复查)", + 阶段="SHOPPING_REDPACKET_POPUP", + 原因="像素检测到背景+OCR确认(不在直播间)", + 总耗时毫秒=total_elapsed, + 红包检测耗时毫秒=redpacket_elapsed, + ) + return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(不在直播间)" + else: + redpacket_elapsed = (time.time() - step_redpacket_ocr_start) * 1000 + _log("步骤2.5[购物红包OCR复查]-未命中", 耗时毫秒=redpacket_elapsed) + # 步骤3:检测推荐页面、关注列表、直播列表(OCR标签栏,整合在一起检测) step_start = time.time() remaining_time_before = max_diag_time - (time.time() - diag_start)