diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 5765916..eebfff1 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1775,126 +1775,8 @@ class fudai_analyse: # 如果不是高价值福袋,或者不是倒计时原因,筛选不通过,直接跳过 if skip: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:第一次打开时筛选不通过,直接跳过", - skip_reason=skip_reason, - ) - except Exception: - pass - # 关闭详情页 - try: - 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:筛选不通过后切换直播间", - skip_reason=skip_reason, - ) - except Exception: - pass - try: - # 优先使用封装好的上滑切房方法 - if hasattr(self, "swipe_up_to_next_live"): - self.swipe_up_to_next_live() - else: - # 兜底:直接调用底层 swipe(基于 1080x2400 基准坐标) - try: - rx = int( - getattr( - self, "resolution_ratio_x", 1080 - ) - or 1080 - ) - ry = int( - getattr( - self, "resolution_ratio_y", 2400 - ) - or 2400 - ) - except Exception: - rx, ry = 1080, 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, - ) - 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), + fudai_detail_blocks.handle_filter_skip_close_and_switch( + self, start_time, skip_reason ) return except Exception as e: diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index 53032d8..231cc14 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -10,6 +10,46 @@ import time from typing import Any +def handle_filter_skip_close_and_switch(analyser: Any, start_time: float, skip_reason: str) -> None: + """筛选不通过的善后:关闭详情页 + 按 needswitch 切换直播间 + 清理任务数量。 + + 复用 _force_swipe_to_next_live_with_adb_fallback 切换逻辑。 + """ + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:第一次打开时筛选不通过,直接跳过", + skip_reason=skip_reason, + ) + except Exception: + pass + + try: + from douyin.fudai import douyin_fudai_detail + + douyin_fudai_detail.close_fudai_detail(analyser) + except Exception: + pass + + try: + needswitch = bool(getattr(analyser, "needswitch", True)) + except Exception: + needswitch = True + + if needswitch: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:筛选不通过后切换直播间", + skip_reason=skip_reason, + ) + except Exception: + pass + _force_swipe_to_next_live_with_adb_fallback(analyser) + + cleanup_and_log_completion(analyser, start_time, "筛选不通过") + + def handle_second_open_attend(analyser: Any, start_time: float, step1_start: float) -> bool: """处理"第二次打开福袋详情"分支:直接点击参与,跳过验证和内容识别。 diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index 7d4b32e..780fc55 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -475,3 +475,44 @@ def test_second_open_attend_exception_returns_false_for_fallthrough(): a, _time.time(), _time.time() ) assert result is False + + +# ---- handle_filter_skip_close_and_switch ---- # + +def test_filter_skip_with_switch_calls_close_and_swipe(): + a = MagicMock() + a.needswitch = True + a._fudai_detail_task_count_from_stage = 5 + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd: + fudai_detail_blocks.handle_filter_skip_close_and_switch( + a, _time.time(), "value_too_low" + ) + cd.assert_called_once_with(a) + a.swipe_up_to_next_live.assert_called_once() + assert a._fudai_detail_task_count_from_stage is None + + +def test_filter_skip_no_switch_only_closes(): + a = MagicMock() + a.needswitch = False + with patch("douyin.fudai.douyin_fudai_detail.close_fudai_detail") as cd: + fudai_detail_blocks.handle_filter_skip_close_and_switch( + a, _time.time(), "single_room_mode" + ) + cd.assert_called_once_with(a) + a.swipe_up_to_next_live.assert_not_called() + + +def test_filter_skip_close_failure_swallowed(): + a = MagicMock() + a.needswitch = True + with patch( + "douyin.fudai.douyin_fudai_detail.close_fudai_detail", + side_effect=RuntimeError("boom"), + ): + # 不应抛错 + fudai_detail_blocks.handle_filter_skip_close_and_switch( + a, _time.time(), "x" + ) + # 关闭失败但仍执行切换 + a.swipe_up_to_next_live.assert_called_once()