From 5d3d58a2f91eadc49c5788b1e127c1f95ab51fbc Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 23 Jun 2026 12:07:36 +0800 Subject: [PATCH] fix: retry reservation direct attend icon scan --- .../direct_attend.py | 75 +++++++++++++++++++ .../test_reservation_direct_attend_markers.py | 34 +++++++++ 2 files changed, 109 insertions(+) diff --git a/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py b/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py index 5b5ebf0..c96d114 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/direct_attend.py @@ -108,6 +108,17 @@ def _direct_attend_open_max_wait_seconds(analyser: Any) -> float: return value +def _direct_attend_retry_open_max_wait_seconds(analyser: Any, base_seconds: float) -> float: + try: + global_wait = float(getattr(analyser, "fudai_icon_max_wait_seconds", 4.5) or 4.5) + except Exception: + global_wait = 4.5 + retry_wait = min(global_wait, 4.5) + if retry_wait < base_seconds: + retry_wait = base_seconds + return retry_wait + + def _reservation_key(item: Dict[str, Any]) -> str: try: ft = int(float(item.get("future_ts") or 0.0)) @@ -584,6 +595,70 @@ def handle_reservation_direct_attend(analyser: Any) -> None: except Exception: pass + if not opened: + retry_open_max_wait_seconds = _direct_attend_retry_open_max_wait_seconds( + analyser, + open_max_wait_seconds, + ) + try: + remain_after_first_scan = future_ts - time.time() if future_ts > 0 else 0.0 + except Exception: + remain_after_first_scan = 0.0 + retry_allowed = ( + retry_open_max_wait_seconds > open_max_wait_seconds + and future_ts > 0 + and remain_after_first_scan + >= (min_action_seconds + retry_open_max_wait_seconds + 3.0) + ) + if retry_allowed: + try: + analyser._log_kv( + "预约", + "[预约] 预约直参首轮未找到福袋,剩余时间充足,延长一次稳定重扫", + reason="direct_attend_icon_retry_after_verified_return", + first_open_max_wait_seconds=round(open_max_wait_seconds, 3), + retry_open_max_wait_seconds=round( + retry_open_max_wait_seconds, + 3, + ), + remain_seconds_before_retry=round( + remain_after_first_scan, + 3, + ), + ) + except Exception: + pass + try: + analyser.operation.delay(0.3) + except Exception: + time.sleep(0.3) + retry_open_start_ts = time.time() + opened = douyin_flow_control.find_and_open_fudai_detail( + analyser, + room_pos_ref, + max_wait_seconds=retry_open_max_wait_seconds, + ) + try: + analyser._log_kv( + "预约", + "[预约] 预约直参延长重扫打开详情耗时", + direct_open_retry_elapsed=round( + time.time() - retry_open_start_ts, + 3, + ), + open_max_wait_seconds=round( + retry_open_max_wait_seconds, + 3, + ), + remain_seconds_before_retry=round( + remain_after_first_scan, + 3, + ), + opened=bool(opened), + ) + except Exception: + pass + if not opened: _log_direct_attend_marker(analyser, "fallback", reason="fudai_icon_not_found") failure_count = _record_direct_attend_failure( diff --git a/tests/test_reservation_direct_attend_markers.py b/tests/test_reservation_direct_attend_markers.py index 9e1ea04..f294de2 100644 --- a/tests/test_reservation_direct_attend_markers.py +++ b/tests/test_reservation_direct_attend_markers.py @@ -140,6 +140,40 @@ def test_direct_attend_uses_short_open_budget_for_both_scans(): assert call.kwargs["max_wait_seconds"] == 2.25 +def test_direct_attend_extends_first_icon_scan_when_time_budget_allows(): + from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import ( + handle_reservation_direct_attend, + ) + + analyser = _mk_analyser() + analyser.fudai_icon_max_wait_seconds = 7.0 + analyser.reservation_direct_attend_open_max_wait_seconds = 2.5 + analyser.reservations[0]["future_ts"] = time.time() + 90.0 + + with patch( + "douyin.core.douyin_flow_control.find_and_open_fudai_detail", + side_effect=[False, True, False], + ) as finder, patch( + "douyin.core.path_config.get_screenshot_path", + return_value="C:/mock/screenshot.png", + ), patch( + "PIL.Image.open", + ) as mock_open, patch( + "douyin.detection.douyin_pixel.detect_attend_button_red_area", + return_value=(True, {"mock": True}), + ): + mock_image = MagicMock() + mock_open.return_value = mock_image + handle_reservation_direct_attend(analyser) + + assert finder.call_count == 3 + assert finder.call_args_list[0].kwargs["max_wait_seconds"] == 2.5 + assert finder.call_args_list[1].kwargs["max_wait_seconds"] == 4.5 + assert finder.call_args_list[2].kwargs["max_wait_seconds"] == 2.5 + analyser.operation.click.assert_called() + assert not _has_log_reason(analyser, "fudai_icon_not_found") + + def test_direct_attend_logs_fixed_failure_marker_when_button_still_exists(): from douyin.reservation.douyin_reservation_handler_pkg.direct_attend import ( handle_reservation_direct_attend,