diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 3bc1d41..e832044 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2408,266 +2408,8 @@ class fudai_analyse: return # 检查是否是优惠券弹窗、观看任务或识别错误:如果检测到,应该直接跳过,不继续处理 - try: - last_status = getattr(self, "_last_fudai_detail_status", "") - if last_status == "coupon_popup": - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:检测到优惠券弹窗,已关闭,跳过后续处理", - ) - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(优惠券弹窗)", - elapsed_seconds=round(elapsed, 2), - ) - return - elif last_status == "watch_task": - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:检测到'观看X分钟'任务,已在OCR阶段跳过,无需后续处理", - ) - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(观看任务)", - elapsed_seconds=round(elapsed, 2), - ) - return - elif last_status == "validation_failed": - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:福袋详情数据验证失败,识别出错,跳过后续处理", - ) - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(验证失败)", - elapsed_seconds=round(elapsed, 2), - ) - return - elif last_status == "error": - # 检查是否是任务数量为0的错误 - try: - last_extra = getattr(self, "_last_fudai_detail_extra", {}) - if ( - isinstance(last_extra, dict) - and last_extra.get("reason") == "task_count_zero" - ): - # 记录连续失败次数 - consecutive_failures = ( - getattr(self, "_fudai_detail_consecutive_failures", 0) or 0 - ) - consecutive_failures += 1 - try: - self._fudai_detail_consecutive_failures = ( - consecutive_failures - ) - except Exception: - pass - - # 如果连续失败3次,强制关闭详情页并切换直播间,避免卡死 - if consecutive_failures >= 3: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:连续3次识别到任务数量为0,强制关闭详情页并切换直播间,避免卡死", - consecutive_failures=consecutive_failures, - ) - except Exception: - pass - # 强制关闭详情页 - try: - douyin_fudai_detail.close_fudai_detail(self) - self.operation.delay(0.5) - except Exception: - pass - # 切换直播间 - try: - needswitch = bool(getattr(self, "needswitch", True)) - except Exception: - needswitch = True - if needswitch: - try: - if hasattr(self, "swipe_up_to_next_live"): - self.swipe_up_to_next_live() - else: - rx = int( - getattr(self, "resolution_ratio_x", 1080) - or 1080 - ) - ry = int( - getattr(self, "resolution_ratio_y", 2400) - or 2400 - ) - start_x = 540 * rx // 1080 - start_y = 1200 * ry // 2400 - end_x = 540 * rx // 1080 - end_y = 600 * ry // 2400 - # 直接使用 ADB 内部滑动命令 - try: - if hasattr(self.operation, "_adb_path"): - adb_cmd = self.operation._adb_path() - else: - adb_cmd = ( - os.environ.get( - "ADB_EXECUTABLE", "adb" - ) - or "adb" - ) - if adb_cmd.startswith( - '"' - ) and adb_cmd.endswith('"'): - adb_cmd = adb_cmd[1:-1] - cmd = [ - adb_cmd, - "-s", - str(self.device_id), - "shell", - "input", - "swipe", - str(int(start_x)), - str(int(start_y)), - str(int(end_x)), - str(int(end_y)), - "300", - ] - subprocess.run( - cmd, - timeout=5.0, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=False, - ) - except Exception: - # 回退到 operation.swipe - self.operation.swipe( - self.device_id, - start_x, - start_y, - end_x, - end_y, - 300, - ) - self.operation.delay(0.5) - except Exception: - pass - # 重置失败计数 - try: - self._fudai_detail_consecutive_failures = 0 - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(连续失败)", - elapsed_seconds=round(elapsed, 2), - ) - return - else: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:检测到任务数量为0,跳过后续处理", - consecutive_failures=consecutive_failures, - ) - except Exception: - pass - - # 清除任务数量标志 - try: - self._fudai_detail_task_count_from_stage = None - try: - self._log_kv( - "福袋详情", - "_handle_in_fudai_detail:清除任务数量标志", - log_type="DEBUG", - ) - except Exception: - pass - except Exception: - pass - - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_in_fudai_detail完成(任务数量为0)", - elapsed_seconds=round(elapsed, 2), - ) - return - except Exception: - pass - except Exception: - pass + if _hd_helpers.check_last_status_branch(self, start_time): + return # 如果成功读取,重置失败计数 try: diff --git a/douyin/core/fudai_modules/handle_detail_helpers.py b/douyin/core/fudai_modules/handle_detail_helpers.py index 9cc1367..a1fd071 100644 --- a/douyin/core/fudai_modules/handle_detail_helpers.py +++ b/douyin/core/fudai_modules/handle_detail_helpers.py @@ -94,6 +94,247 @@ def handle_consecutive_failures_check(self: "fudai_analyse") -> bool: return True +def check_last_status_branch( + self: "fudai_analyse", start_time: float +) -> bool: + """检查 self._last_fudai_detail_status,对 4 种已知状态做相应处理。 + + 支持的状态: + - coupon_popup 检测到优惠券弹窗 + - watch_task 检测到"观看 X 分钟"任务 + - validation_failed 数据验证失败 + - error 其他错误(特别处理 task_count_zero 子情况:连续 3 次强制切直播间) + + 返回 True 表示已处理某状态,caller 应当 `return`; + 返回 False 表示状态正常或不在已知列表,继续后续步骤。 + """ + import os + import subprocess + import time + + from douyin.fudai import douyin_fudai_detail + + try: + last_status = getattr(self, "_last_fudai_detail_status", "") + except Exception: + last_status = "" + + if last_status == "coupon_popup": + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:检测到优惠券弹窗,已关闭,跳过后续处理", + ) + except Exception: + pass + _clear_task_count_flag(self) + elapsed = time.time() - start_time + self._log_kv( + "流程", + "_handle_in_fudai_detail完成(优惠券弹窗)", + elapsed_seconds=round(elapsed, 2), + ) + return True + + if last_status == "watch_task": + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:检测到'观看X分钟'任务,已在OCR阶段跳过,无需后续处理", + ) + except Exception: + pass + _clear_task_count_flag(self) + elapsed = time.time() - start_time + self._log_kv( + "流程", + "_handle_in_fudai_detail完成(观看任务)", + elapsed_seconds=round(elapsed, 2), + ) + return True + + if last_status == "validation_failed": + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:福袋详情数据验证失败,识别出错,跳过后续处理", + ) + except Exception: + pass + _clear_task_count_flag(self) + elapsed = time.time() - start_time + self._log_kv( + "流程", + "_handle_in_fudai_detail完成(验证失败)", + elapsed_seconds=round(elapsed, 2), + ) + return True + + if last_status == "error": + return _handle_error_status(self, start_time) + + # 未知 / 空状态 → 继续后续步骤 + return False + + +def _clear_task_count_flag(self: "fudai_analyse") -> None: + """清除任务数量标志,多个 last_status 分支都用到。""" + try: + self._fudai_detail_task_count_from_stage = None + try: + self._log_kv( + "福袋详情", + "_handle_in_fudai_detail:清除任务数量标志", + log_type="DEBUG", + ) + except Exception: + pass + except Exception: + pass + + +def _handle_error_status(self: "fudai_analyse", start_time: float) -> bool: + """last_status == "error" 时的处理。 + + 特殊子情况:task_count_zero。连续 3 次时强制关详情 + 切直播间避免卡死。 + 其它 error 不在此函数处理,返回 False。 + """ + import os + import subprocess + import time + + from douyin.fudai import douyin_fudai_detail + + try: + last_extra = getattr(self, "_last_fudai_detail_extra", {}) + if not ( + isinstance(last_extra, dict) + and last_extra.get("reason") == "task_count_zero" + ): + return False + + # 累加连续失败计数 + consecutive_failures = ( + getattr(self, "_fudai_detail_consecutive_failures", 0) or 0 + ) + consecutive_failures += 1 + try: + self._fudai_detail_consecutive_failures = consecutive_failures + except Exception: + pass + + # 连续失败 < 3:仅记录并返回 + if consecutive_failures < 3: + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:检测到任务数量为0,跳过后续处理", + consecutive_failures=consecutive_failures, + ) + except Exception: + pass + _clear_task_count_flag(self) + elapsed = time.time() - start_time + self._log_kv( + "流程", + "_handle_in_fudai_detail完成(任务数量为0)", + elapsed_seconds=round(elapsed, 2), + ) + return True + + # 连续失败 >= 3:强制关详情 + 切直播间 + try: + self._log_kv( + "流程", + "_handle_in_fudai_detail:连续3次识别到任务数量为0,强制关闭详情页并切换直播间,避免卡死", + consecutive_failures=consecutive_failures, + ) + except Exception: + pass + + try: + douyin_fudai_detail.close_fudai_detail(self) + self.operation.delay(0.5) + except Exception: + pass + + try: + needswitch = bool(getattr(self, "needswitch", True)) + except Exception: + needswitch = True + if needswitch: + try: + if hasattr(self, "swipe_up_to_next_live"): + self.swipe_up_to_next_live() + else: + rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080) + ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400) + start_x = 540 * rx // 1080 + start_y = 1200 * ry // 2400 + end_x = 540 * rx // 1080 + end_y = 600 * ry // 2400 + # 直接使用 ADB 内部滑动命令 + try: + if hasattr(self.operation, "_adb_path"): + adb_cmd = self.operation._adb_path() + else: + adb_cmd = ( + os.environ.get("ADB_EXECUTABLE", "adb") or "adb" + ) + if adb_cmd.startswith('"') and adb_cmd.endswith('"'): + adb_cmd = adb_cmd[1:-1] + cmd = [ + adb_cmd, + "-s", + str(self.device_id), + "shell", + "input", + "swipe", + str(int(start_x)), + str(int(start_y)), + str(int(end_x)), + str(int(end_y)), + "300", + ] + subprocess.run( + cmd, + timeout=5.0, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=False, + ) + except Exception: + # 回退到 operation.swipe + self.operation.swipe( + self.device_id, + start_x, + start_y, + end_x, + end_y, + 300, + ) + self.operation.delay(0.5) + except Exception: + pass + + # 重置失败计数 + 清除任务数量标志 + try: + self._fudai_detail_consecutive_failures = 0 + except Exception: + pass + _clear_task_count_flag(self) + + elapsed = time.time() - start_time + self._log_kv( + "流程", + "_handle_in_fudai_detail完成(连续失败)", + elapsed_seconds=round(elapsed, 2), + ) + return True + except Exception: + return False + + def check_invalid_detail_fallback( self: "fudai_analyse", detail, start_time: float ) -> bool: diff --git a/tests/test_handle_detail_helpers.py b/tests/test_handle_detail_helpers.py index 6a0eae3..c1f13e5 100644 --- a/tests/test_handle_detail_helpers.py +++ b/tests/test_handle_detail_helpers.py @@ -329,3 +329,91 @@ class TestPrepareForAttend: result = hd.prepare_for_attend(p, ("a", "b", "c", "d", 0, (0, 0, 0))) assert result == 0.0 + +# ============================================================ +# check_last_status_branch +# ============================================================ + +class TestCheckLastStatusBranch: + def _mk_for_status(self, last_status: str = "", last_extra=None): + s = _mk_self() + s._last_fudai_detail_status = last_status + if last_extra is not None: + s._last_fudai_detail_extra = last_extra + s._fudai_detail_task_count_from_stage = 5 + s._fudai_detail_consecutive_failures = 0 + s.device_id = "MOCK" + s.operation = MagicMock() + s.operation.delay = MagicMock(return_value=None) + s.operation.swipe = MagicMock(return_value=None) + return s + + def test_empty_status_returns_false(self): + s = self._mk_for_status("") + assert hd.check_last_status_branch(s, 0.0) is False + + def test_unknown_status_returns_false(self): + s = self._mk_for_status("some_other_status") + assert hd.check_last_status_branch(s, 0.0) is False + + def test_coupon_popup_returns_true_clears_task_count(self): + s = self._mk_for_status("coupon_popup") + assert hd.check_last_status_branch(s, 0.0) is True + assert s._fudai_detail_task_count_from_stage is None + + def test_watch_task_returns_true_clears_task_count(self): + s = self._mk_for_status("watch_task") + assert hd.check_last_status_branch(s, 0.0) is True + assert s._fudai_detail_task_count_from_stage is None + + def test_validation_failed_returns_true_clears_task_count(self): + s = self._mk_for_status("validation_failed") + assert hd.check_last_status_branch(s, 0.0) is True + assert s._fudai_detail_task_count_from_stage is None + + def test_error_without_task_count_zero_reason_returns_false(self): + """error 但 reason 不是 task_count_zero → 不在此处理。""" + s = self._mk_for_status("error", last_extra={"reason": "other_error"}) + assert hd.check_last_status_branch(s, 0.0) is False + + def test_error_task_count_zero_first_failure(self): + """task_count_zero 第 1 次:仅累加计数 + 清标志,不切直播间。""" + s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"}) + s._fudai_detail_consecutive_failures = 0 + s.swipe_up_to_next_live = MagicMock(return_value=None) + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"): + result = hd.check_last_status_branch(s, 0.0) + assert result is True + assert s._fudai_detail_consecutive_failures == 1 + s.swipe_up_to_next_live.assert_not_called() + + def test_error_task_count_zero_third_failure_swipes(self): + """task_count_zero 第 3 次:强制关详情 + 切直播间 + 重置计数。""" + s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"}) + s._fudai_detail_consecutive_failures = 2 # 加 1 后达到 3 + s.needswitch = True + s.swipe_up_to_next_live = MagicMock(return_value=None) + with patch( + "douyin.fudai.douyin_fudai_detail.close_fudai_detail" + ) as mock_close: + result = hd.check_last_status_branch(s, 0.0) + assert result is True + mock_close.assert_called_once() + s.swipe_up_to_next_live.assert_called_once() + # 计数应被重置 + assert s._fudai_detail_consecutive_failures == 0 + # 任务数量标志被清除 + assert s._fudai_detail_task_count_from_stage is None + + def test_error_task_count_zero_third_failure_no_switch_when_needswitch_false(self): + """needswitch=False 时第 3 次也不切直播间。""" + s = self._mk_for_status("error", last_extra={"reason": "task_count_zero"}) + s._fudai_detail_consecutive_failures = 2 + s.needswitch = False + s.swipe_up_to_next_live = MagicMock(return_value=None) + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail"): + result = hd.check_last_status_branch(s, 0.0) + assert result is True + s.swipe_up_to_next_live.assert_not_called() + assert s._fudai_detail_consecutive_failures == 0 +