diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 5e005b3..359b2ae 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -130,12 +130,14 @@ class fudai_analyse: self, target_title, target_title_raw ) - def _record_room_title_and_pos(self) -> None: + def _record_room_title_and_pos(self) -> bool: """记录当前直播间的标题和位置到运行时映射表。 用于预约回退校验时验证位置信息。 """ - douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(self) + return bool( + douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(self) + ) def click(self, x: int, y: int) -> None: """兼容原版 fudai_analyse.click:转发到底层 operation.click。""" diff --git a/douyin/core/fudai_modules/fudai_room_utils.py b/douyin/core/fudai_modules/fudai_room_utils.py index e8e9d19..01a4183 100644 --- a/douyin/core/fudai_modules/fudai_room_utils.py +++ b/douyin/core/fudai_modules/fudai_room_utils.py @@ -27,13 +27,15 @@ class RoomUtilsMixin: self, target_title, target_title_raw ) - def _record_room_title_and_pos(self) -> None: + def _record_room_title_and_pos(self) -> bool: """记录当前直播间的标题和位置到运行时映射表。 用于预约回退校验时验证位置信息。 """ from douyin.reservation import douyin_reservation_handler - douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(self) + return bool( + douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(self) + ) def _seek_reservation_room(self, item: dict[str, Any]) -> bool: """根据预约记录里的 pos_seen 与当前 room_pos 进行回退,靠近目标直播间。 diff --git a/douyin/fudai/core/coords.py b/douyin/fudai/core/coords.py index 0171d76..7ec05d2 100644 --- a/douyin/fudai/core/coords.py +++ b/douyin/fudai/core/coords.py @@ -125,7 +125,7 @@ DETAIL_UI_V2_0 = { "version": "v2_0", "content": (365, 1368, 1040, 1465), "value": (300, 1518, 820, 1695), - "countdown_full": (760, 1265, 1040, 1345), + "countdown_full": (760, 1265, 1040, 1398), "task_list": (60, 1840, 1030, 2005), "task_count": (860, 1770, 1035, 1875), "participants": (520, 1325, 760, 1415), diff --git a/douyin/fudai/douyin_fudai_core_pkg/cut_regions.py b/douyin/fudai/douyin_fudai_core_pkg/cut_regions.py index 5c1d5d7..263d389 100644 --- a/douyin/fudai/douyin_fudai_core_pkg/cut_regions.py +++ b/douyin/fudai/douyin_fudai_core_pkg/cut_regions.py @@ -68,9 +68,9 @@ def cut_detail_regions( x1, y1, x2, y2 = countdown_box w = max(1, int(x2 - x1)) h = max(1, int(y2 - y1)) - # 纵向只取倒计时 chip 的数字行,避免把下方商品标题混入分钟/秒 OCR。 - digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.18))) - digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.62)))) + # 纵向只取倒计时 chip 的数字行,避免把上方黑底或下方商品标题混入分钟/秒 OCR。 + digit_y1 = min(y2 - 2, y1 + max(1, int(h * 0.34))) + digit_y2 = min(y2, max(digit_y1 + 2, y1 + max(2, int(h * 0.92)))) mm_x1 = x1 mm_x2 = min(x2, x1 + max(1, int(w * 0.26))) ss_x1 = min(x2 - 1, x1 + max(1, int(w * 0.38))) diff --git a/douyin/reservation/douyin_reservation_handler_pkg/handler.py b/douyin/reservation/douyin_reservation_handler_pkg/handler.py index 839a1d2..90cfedc 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/handler.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/handler.py @@ -28,7 +28,7 @@ class ReservationHandler: return _find_room_pos_by_title(analyser, target_title, target_title_raw) @staticmethod # type: ignore[misc] - def record_room_title_and_pos(analyser: Any) -> None: + def record_room_title_and_pos(analyser: Any) -> bool: return _record_room_title_and_pos(analyser) @staticmethod # type: ignore[misc] diff --git a/douyin/reservation/douyin_reservation_handler_pkg/record_title.py b/douyin/reservation/douyin_reservation_handler_pkg/record_title.py index 1bb6deb..aa96ffe 100644 --- a/douyin/reservation/douyin_reservation_handler_pkg/record_title.py +++ b/douyin/reservation/douyin_reservation_handler_pkg/record_title.py @@ -264,10 +264,37 @@ def record_room_title_and_pos(analyser: Any) -> bool: # 记录到映射表(包含福袋信息)- 仅记录位置,不包含标题(标题由并行OCR提供) try: - # 检查是否有福袋 - has_fudai = bool(getattr(analyser, "_current_fudai_content", "")) - fudai_content = str(getattr(analyser, "_current_fudai_content", "")) - fudai_detail = getattr(analyser, "_last_fudai_detail_tuple", None) + # 检查是否有福袋。详情验证失败时只记录房间标题,不把无效详情写入回退依据。 + detail_status = str(getattr(analyser, "_last_fudai_detail_status", "") or "") + invalid_detail_statuses = { + "validation_failed", + "coupon_popup", + "watch_task", + "error", + } + has_valid_detail = detail_status not in invalid_detail_statuses + has_fudai = bool(getattr(analyser, "_current_fudai_content", "")) and has_valid_detail + fudai_content = ( + str(getattr(analyser, "_current_fudai_content", "")) + if has_valid_detail + else "" + ) + fudai_detail = ( + getattr(analyser, "_last_fudai_detail_tuple", None) + if has_valid_detail + else None + ) + if not has_valid_detail: + try: + analyser._log_kv( # type: ignore[attr-defined] + "预约", + "[预约] 跳过无效福袋详情写入映射表", + room_pos=room_pos, + detail_status=detail_status, + reason="invalid_fudai_detail_status", + ) + except Exception: + pass # 添加调试日志 analyser._log_kv( # type: ignore[attr-defined] diff --git a/tests/test_cut_regions.py b/tests/test_cut_regions.py index 610478d..ee323d3 100644 --- a/tests/test_cut_regions.py +++ b/tests/test_cut_regions.py @@ -104,10 +104,23 @@ class TestCutDetailRegions: mm_call = a.cut_pic.call_args_list[1] ss_call = a.cut_pic.call_args_list[2] - assert mm_call.args[1] == 1340 - assert mm_call.args[3] == 1371 - assert ss_call.args[1] == 1340 - assert ss_call.args[3] == 1371 + assert mm_call.args[1] == 1351 + assert mm_call.args[3] == 1392 + assert ss_call.args[1] == 1351 + assert ss_call.args[3] == 1392 + + def test_v2_two_task_tall_profile_uses_lower_digit_band(self): + a = _mk_analyser() + profile = {"countdown_full": (760, 1265, 1040, 1398)} + + cut_detail_regions(a, renwu=2, profile=profile) + + mm_call = a.cut_pic.call_args_list[1] + ss_call = a.cut_pic.call_args_list[2] + assert mm_call.args[1] >= 1310 + assert mm_call.args[3] >= 1385 + assert ss_call.args[1] == mm_call.args[1] + assert ss_call.args[3] == mm_call.args[3] def test_save_debug_images_disabled_default(self): a = _mk_analyser() diff --git a/tests/test_fudai_detail_ui_v2.py b/tests/test_fudai_detail_ui_v2.py index 649e8da..c0a3e62 100644 --- a/tests/test_fudai_detail_ui_v2.py +++ b/tests/test_fudai_detail_ui_v2.py @@ -30,7 +30,7 @@ def test_v2_profile_keeps_legacy_and_adds_expected_regions(monkeypatch): assert DETAIL_UI_LEGACY["content"] == (389, 1273, 1000, 1534) assert DETAIL_UI_V2_0["content"] == (365, 1368, 1040, 1465) assert DETAIL_UI_V2_0["value"] == (300, 1518, 820, 1695) - assert DETAIL_UI_V2_0["countdown_full"] == (760, 1265, 1040, 1345) + assert DETAIL_UI_V2_0["countdown_full"] == (760, 1265, 1040, 1398) assert DETAIL_UI_V2_0["task_list"] == (60, 1840, 1030, 2005) assert DETAIL_UI_V2_0["task_count"] == (860, 1770, 1035, 1875) assert DETAIL_UI_V2_0["participants"] == (520, 1325, 760, 1415) @@ -175,6 +175,33 @@ def test_v2_two_task_value_region_covers_reference_value_line(): assert crop_has_reference_line_text(sample) > 0.03 +def test_v2_two_task_countdown_region_covers_lower_countdown_line(): + from pathlib import Path + from douyin.fudai.core.coords import DETAIL_UI_V2_0 + + samples = [ + Path("tests/fixtures/fudai_detail_v2_two_task_redmi_2999_regions.png"), + Path("tests/fixtures/fudai_detail_v2_two_task_guohao_1199_regions.png"), + ] + missing = [sample for sample in samples if not sample.exists()] + if missing: + pytest.skip("v2 two-task countdown samples are unavailable") + + countdown_box = DETAIL_UI_V2_0["countdown_full"] + assert countdown_box[1] <= 1328 + assert countdown_box[3] >= 1398 + + def crop_has_countdown_text(sample: Path) -> float: + img = Image.open(sample).convert("RGB") + crop = img.crop((760, 1328, min(countdown_box[2], 1040), min(countdown_box[3], 1398))) + pixels = list(crop.getdata()) + bright = sum(1 for r, g, b in pixels if r > 185 and g > 185 and b > 185) + return bright / max(1, len(pixels)) + + for sample in samples: + assert crop_has_countdown_text(sample) > 0.02 + + def test_ocr_switch_defaults_and_legacy_disable_compat(): from douyin.fudai.douyin_fudai_core_pkg.ocr_switches import ( FudaiDetailOcrSwitches, diff --git a/tests/test_record_title_mapping.py b/tests/test_record_title_mapping.py index b66d883..f1b8d4d 100644 --- a/tests/test_record_title_mapping.py +++ b/tests/test_record_title_mapping.py @@ -173,3 +173,67 @@ def test_no_fudai_room_mapping_does_not_inherit_previous_fudai_content(): assert info["has_fudai"] is False assert info["fudai_content"] == "" assert info["fudai_detail"] is None + + +def test_validation_failed_detail_is_not_recorded_as_valid_fudai_mapping(): + from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod + + a = _mk_analyser() + a.room_pos = 6 + a.current_room_title = "作业帮学万" + a.current_room_title_raw = "作业帮学万" + a._room_title_identified_positions = {6} + a._current_fudai_content = "免费试用" + a._last_fudai_detail_tuple = ( + "免费试用", + "00:15", + 0, + 0, + 0.0, + (100.0, 115.0), + ) + a._last_fudai_detail_status = "validation_failed" + + with patch.object(mod, "_dump_room_pos_map_to_state", return_value=None): + consumed = mod.record_room_title_and_pos(a) + + assert consumed is False + info = a._room_pos_to_title_map[6] + assert info["title"] == "作业帮学万" + assert info["has_fudai"] is False + assert info["fudai_content"] == "" + assert info["fudai_detail"] is None + + +def test_reservation_handler_propagates_record_consumed_swipe(): + from douyin.reservation.douyin_reservation_handler_pkg.handler import ( + ReservationHandler, + ) + + a = _mk_analyser() + + with patch( + "douyin.reservation.douyin_reservation_handler_pkg.handler._record_room_title_and_pos", + return_value=True, + ): + assert ReservationHandler.record_room_title_and_pos(a) is True + + +def test_room_utils_record_wrapper_propagates_consumed_swipe(monkeypatch): + from douyin.core.fudai_modules.fudai_room_utils import RoomUtilsMixin + + class _A(RoomUtilsMixin): + pass + + def fake_record(_analyser): + return True + + from douyin.reservation import douyin_reservation_handler + + monkeypatch.setattr( + douyin_reservation_handler.ReservationHandler, + "record_room_title_and_pos", + staticmethod(fake_record), + ) + + assert _A()._record_room_title_and_pos() is True