diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 0885728..1f27f9b 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -3079,54 +3079,8 @@ class fudai_analyse: pass # 检查是否在锁定期内,且时间窗口未到,避免重复打开福袋详情 - try: - now_ts = time.time() - focus_until = float(getattr(self, "reservation_focus_until_ts", 0.0) or 0.0) - queue_full_waiting = bool( - getattr(self, "queue_full_waiting_at_earliest", False) or False - ) - - if focus_until > 0 and now_ts < focus_until and queue_full_waiting: - # 在锁定期内,且队列满等待中,检查时间窗口 - if self.reservations and len(self.reservations) > 0: - earliest = self.reservations[0] - try: - ft = float(earliest.get("future_ts") or 0.0) - if ft > 0: - join_ahead = float( - getattr( - self, "high_value_fudai_join_ahead_seconds", 100 - ) - or 120 - ) - time_window_start_ts = ft - join_ahead - - # 如果时间窗口未到,避免重复打开福袋详情 - if now_ts < time_window_start_ts: - try: - self._log_kv( - "预约", - "[预约] 锁定期内时间窗口未到,避免重复打开福袋详情", - focus_until=int(focus_until), - now_ts=int(now_ts), - time_window_start=int(time_window_start_ts), - remain_seconds=int( - time_window_start_ts - now_ts - ), - ) - except Exception: - pass - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_live_room_has_or_not_fudai完成(锁定期内时间窗口未到)", - elapsed_seconds=round(elapsed, 2), - ) - return - except Exception: - pass - except Exception: - pass + if fudai_detail_blocks.should_skip_search_in_lock_period(self, start_time): + return room_pos_ref: List[int] = [0] opened = douyin_flow_control.find_and_open_fudai_detail(self, room_pos_ref) diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index dfe12c9..38dc0c3 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -532,6 +532,72 @@ def reset_fudai_detail_state_after_attend(analyser: Any) -> None: except Exception: pass + +def should_skip_search_in_lock_period(analyser: Any, start_time: float) -> bool: + """检查是否处于锁定期且时间窗口未到,若是则跳过本次福袋搜索。 + + 返回 True 表示应当跳过搜索(主流程提前 return)。 + + 锁定期 + 队列满等待 + 时间窗口未到三者同时成立才会跳过;其他情况返回 False。 + """ + try: + now_ts = time.time() + focus_until = float(getattr(analyser, "reservation_focus_until_ts", 0.0) or 0.0) + queue_full_waiting = bool( + getattr(analyser, "queue_full_waiting_at_earliest", False) or False + ) + if not (focus_until > 0 and now_ts < focus_until and queue_full_waiting): + return False + + reservations = getattr(analyser, "reservations", None) + if not reservations or len(reservations) == 0: + return False + + earliest = reservations[0] + try: + ft = float(earliest.get("future_ts") or 0.0) + except Exception: + return False + if ft <= 0: + return False + + try: + join_ahead = float( + getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 120 + ) + except Exception: + join_ahead = 120.0 + + time_window_start_ts = ft - join_ahead + if now_ts >= time_window_start_ts: + return False + + try: + analyser._log_kv( + "预约", + "[预约] 锁定期内时间窗口未到,避免重复打开福袋详情", + focus_until=int(focus_until), + now_ts=int(now_ts), + time_window_start=int(time_window_start_ts), + remain_seconds=int(time_window_start_ts - now_ts), + ) + except Exception: + pass + + try: + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai完成(锁定期内时间窗口未到)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass + return True + except Exception: + return False + + """last_status == "error" 且 reason == "task_count_zero" 的处理。 返回 True 表示已处理(连续失败 3 次切换直播间,或单次失败跳过), diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index dcaaf81..2b03ed0 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -342,3 +342,57 @@ def test_reset_state_clears_all_flags(): assert a._fudai_detail_first_read is False assert a._last_fudai_detail_tuple is None assert a._fudai_detail_task_count_from_stage is None + + +# ---- should_skip_search_in_lock_period ---- # + +def test_skip_search_no_lock_period_returns_false(): + a = MagicMock() + a.reservation_focus_until_ts = 0.0 + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False + + +def test_skip_search_lock_period_but_not_queue_full_returns_false(): + a = MagicMock() + a.reservation_focus_until_ts = _time.time() + 100 + a.queue_full_waiting_at_earliest = False + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False + + +def test_skip_search_no_reservations_returns_false(): + a = MagicMock() + a.reservation_focus_until_ts = _time.time() + 100 + a.queue_full_waiting_at_earliest = True + a.reservations = [] + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, _time.time()) is False + + +def test_skip_search_time_window_started_returns_false(): + """时间窗口已经开始(now >= ft - join_ahead)→ 不跳过""" + now = _time.time() + a = MagicMock() + a.reservation_focus_until_ts = now + 200 + a.queue_full_waiting_at_earliest = True + a.reservations = [{"future_ts": now + 50}] # ft - 100 = now - 50 < now + a.high_value_fudai_join_ahead_seconds = 100 + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is False + + +def test_skip_search_time_window_not_started_returns_true(): + """时间窗口尚未开始(now < ft - join_ahead)→ 跳过""" + now = _time.time() + a = MagicMock() + a.reservation_focus_until_ts = now + 500 + a.queue_full_waiting_at_earliest = True + a.reservations = [{"future_ts": now + 200}] # ft - 100 = now + 100 > now + a.high_value_fudai_join_ahead_seconds = 100 + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is True + + +def test_skip_search_invalid_ft_returns_false(): + now = _time.time() + a = MagicMock() + a.reservation_focus_until_ts = now + 500 + a.queue_full_waiting_at_earliest = True + a.reservations = [{"future_ts": 0}] # 无效 + assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is False