From ba9ac4cb0e0891b6836d6bfba899570b2ef4c62d Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 23 Jun 2026 09:57:06 +0800 Subject: [PATCH] fix: prejoin fansclub before reservation --- .../core/fudai_filter_pkg/join_decision.py | 97 ++++++++++++++ douyin/reservation/douyin_reservation.py | 43 ++++--- douyin/reservation/reservation_logic/add.py | 11 ++ .../reservation/reservation_logic/record.py | 10 ++ tests/test_join_decision_reservation.py | 36 ++++++ tests/test_reservation_record.py | 120 ++++++++++++++++++ 6 files changed, 300 insertions(+), 17 deletions(-) diff --git a/douyin/fudai/core/fudai_filter_pkg/join_decision.py b/douyin/fudai/core/fudai_filter_pkg/join_decision.py index f8ee248..f7e829c 100644 --- a/douyin/fudai/core/fudai_filter_pkg/join_decision.py +++ b/douyin/fudai/core/fudai_filter_pkg/join_decision.py @@ -54,6 +54,97 @@ def _has_valid_countdown(detail: Tuple) -> bool: return False +def _prejoin_fansclub_before_reservation( + analyser: Any, + button_text: str, + remain_seconds: float | int | None, +) -> bool: + """高价值预约前尽量先完成粉丝团任务,回退时减少临开奖动作。""" + text = str(button_text or "").strip() + try: + setattr(analyser, "_reservation_prejoined_fansclub", False) + setattr(analyser, "_reservation_prejoin_fansclub_button_text", text) + setattr(analyser, "_reservation_prejoin_fansclub_ts", 0.0) + except Exception: + pass + + if not text or "粉丝团" not in text: + return False + + if not bool(getattr(analyser, "auto_join_fansclub", False)): + try: + analyser._log_kv( + "预约", + "预约前粉丝团预处理跳过:未启用自动加入粉丝团", + button_text=text[:80], + reason="auto_join_fansclub_disabled", + ) + except Exception: + pass + return False + + try: + analyser._log_kv( + "预约", + "预约前粉丝团预处理:检测到粉丝团任务,先尝试加入", + button_text=text[:80], + remain_seconds=( + int(remain_seconds) if remain_seconds is not None else None + ), + reason="fansclub_required_before_high_value_reservation", + ) + except Exception: + pass + + try: + from douyin.fudai.core.fudai_attend_pkg.fansclub_handler import ( + handle_fansclub_branch, + ) + + ok = bool(handle_fansclub_branch(analyser, text)) + try: + setattr(analyser, "_reservation_prejoined_fansclub", ok) + setattr(analyser, "_reservation_prejoin_fansclub_button_text", text) + setattr(analyser, "_reservation_prejoin_fansclub_ts", time.time()) + except Exception: + pass + try: + analyser._log_kv( + "预约", + "预约前粉丝团预处理完成", + result=ok, + button_text=text[:80], + ) + except Exception: + pass + _emit_flow_flag( + analyser, + "reservation_prejoin_fansclub", + "done" if ok else "failed", + button_text=text[:80], + ) + return ok + except Exception as exc: + try: + analyser._log_kv( + "预约", + "预约前粉丝团预处理异常,继续保留预约", + error=str(exc), + error_type=type(exc).__name__, + button_text=text[:80], + level="WARNING", + ) + except Exception: + pass + _emit_flow_flag( + analyser, + "reservation_prejoin_fansclub", + "error", + error_type=type(exc).__name__, + ) + return False + + def basic_join_decision(analyser: Any, detail: Tuple) -> bool: """基础版参与决策:根据配置筛选福袋,满足条件则尝试参与抽奖或记录高价值预约。 @@ -750,6 +841,12 @@ def basic_join_decision(analyser: Any, detail: Tuple) -> bool: except Exception: pass + _prejoin_fansclub_before_reservation( + analyser, + button_text, + remain_seconds, + ) + queue_full = bool( reservation_record.record_high_value_reservation( analyser=analyser, diff --git a/douyin/reservation/douyin_reservation.py b/douyin/reservation/douyin_reservation.py index 88a4fb5..f46aca9 100644 --- a/douyin/reservation/douyin_reservation.py +++ b/douyin/reservation/douyin_reservation.py @@ -165,23 +165,32 @@ def write_reservations_state( ft_str = datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") if ft else "" except Exception: ft_str = "" - items.append( - { - "prize_value_yuan": float(r.get("prize_value_yuan") or 0), - "content": str(r.get("content") or ""), - "future_ts": ft, - "future_str": ft_str, - "pos_seen": int(r.get("pos_seen") or 0), - "room_title": str(r.get("room_title") or ""), - "room_title_raw": str(r.get("room_title_raw") or ""), - "task_count": int(r.get("task_count") or 2), - "time_text": str(r.get("time_text") or ""), - "participants": int(r.get("participants") or 0), - "copies": int(r.get("copies") or 0), - "button_text": str(r.get("button_text") or ""), - "button_status": str(r.get("button_status") or ""), - } - ) + item = { + "prize_value_yuan": float(r.get("prize_value_yuan") or 0), + "content": str(r.get("content") or ""), + "future_ts": ft, + "future_str": ft_str, + "pos_seen": int(r.get("pos_seen") or 0), + "room_title": str(r.get("room_title") or ""), + "room_title_raw": str(r.get("room_title_raw") or ""), + "task_count": int(r.get("task_count") or 2), + "time_text": str(r.get("time_text") or ""), + "participants": int(r.get("participants") or 0), + "copies": int(r.get("copies") or 0), + "button_text": str(r.get("button_text") or ""), + "button_status": str(r.get("button_status") or ""), + } + if "prejoined_fansclub_before_reservation" in r: + item["prejoined_fansclub_before_reservation"] = bool( + r.get("prejoined_fansclub_before_reservation") + ) + item["prejoin_fansclub_button_text"] = str( + r.get("prejoin_fansclub_button_text") or "" + ) + item["prejoin_fansclub_ts"] = float( + r.get("prejoin_fansclub_ts") or 0.0 + ) + items.append(item) data = { "updated_ts": time.time(), diff --git a/douyin/reservation/reservation_logic/add.py b/douyin/reservation/reservation_logic/add.py index c456b4f..7120035 100644 --- a/douyin/reservation/reservation_logic/add.py +++ b/douyin/reservation/reservation_logic/add.py @@ -271,6 +271,14 @@ def add_reservation_core_logic( pass reservations[same_idx].update(dict(item)) else: + if item.get("prejoined_fansclub_before_reservation"): + reservations[same_idx]["prejoined_fansclub_before_reservation"] = True + reservations[same_idx]["prejoin_fansclub_button_text"] = str( + item.get("prejoin_fansclub_button_text") or "" + ) + reservations[same_idx]["prejoin_fansclub_ts"] = float( + item.get("prejoin_fansclub_ts") or 0.0 + ) try: analyser._log_kv( "预约", @@ -283,6 +291,9 @@ def add_reservation_core_logic( if ft > 0 else "未知" ), + prejoined_fansclub_preserved=bool( + item.get("prejoined_fansclub_before_reservation") + ), ) except Exception: pass diff --git a/douyin/reservation/reservation_logic/record.py b/douyin/reservation/reservation_logic/record.py index 3049a20..57d09a2 100644 --- a/douyin/reservation/reservation_logic/record.py +++ b/douyin/reservation/reservation_logic/record.py @@ -172,6 +172,16 @@ def record_high_value_reservation( ), "button_status": str(getattr(analyser, "_fudai_button_status", "") or ""), } + if bool(getattr(analyser, "_reservation_prejoined_fansclub", False)): + item["prejoined_fansclub_before_reservation"] = True + item["prejoin_fansclub_button_text"] = str( + getattr(analyser, "_reservation_prejoin_fansclub_button_text", "") + or "" + ) + item["prejoin_fansclub_ts"] = float( + getattr(analyser, "_reservation_prejoin_fansclub_ts", 0.0) + or 0.0 + ) except Exception as e: if _RESERVATION_ERROR_UTILS_AVAILABLE: log_reservation_error( diff --git a/tests/test_join_decision_reservation.py b/tests/test_join_decision_reservation.py index b3a84f7..e7e881e 100644 --- a/tests/test_join_decision_reservation.py +++ b/tests/test_join_decision_reservation.py @@ -50,6 +50,42 @@ def test_high_value_countdown_gt_records_reservation_and_returns_true(): analyser.swipe_up_to_next_live.assert_called_once() +def test_high_value_reservation_prejoins_fansclub_before_recording(): + analyser = _mk_analyser() + analyser.auto_join_fansclub = True + analyser.button_text_raw = "加入购物粉丝团并关注主播" + future_ts = time.time() + 300 + detail = ("高价值福袋", "05:00", 10, 1, 1999.0, (time.time(), future_ts, 300)) + events = [] + + def _fake_prejoin(_analyser, _button_text): + events.append("prejoin") + return True + + def _fake_record(**_kwargs): + events.append("record") + return False + + with patch( + "douyin.fudai.core.fudai_filter_pkg.join_decision.should_skip_fudai_by_config", + return_value=(True, "countdown_gt_118"), + ), patch( + "douyin.fudai.core.fudai_attend_pkg.fansclub_handler.handle_fansclub_branch", + side_effect=_fake_prejoin, + ) as mock_prejoin, patch( + "douyin.reservation.reservation_logic.record.record_high_value_reservation", + side_effect=_fake_record, + ) as mock_record: + ok = basic_join_decision(analyser, detail) + + assert ok is True + assert events == ["prejoin", "record"] + mock_prejoin.assert_called_once_with(analyser, "加入购物粉丝团并关注主播") + mock_record.assert_called_once() + assert analyser._reservation_prejoined_fansclub is True + analyser.swipe_up_to_next_live.assert_called_once() + + def test_high_value_countdown_within_join_window_attends(): analyser = _mk_analyser() future_ts = time.time() + 30 # <= join_ahead_seconds diff --git a/tests/test_reservation_record.py b/tests/test_reservation_record.py index e71ceee..da2db3c 100644 --- a/tests/test_reservation_record.py +++ b/tests/test_reservation_record.py @@ -45,3 +45,123 @@ def test_record_high_value_reservation_preserves_button_fields(): assert ok is False assert reservations[0]["button_text"] == "一键发表评论" assert reservations[0]["button_status"] == "需要评论" + + +def test_record_high_value_reservation_preserves_prejoined_fansclub_marker(): + from douyin.reservation.reservation_logic.record import record_high_value_reservation + + analyser = SimpleNamespace( + _log_kv=MagicMock(return_value=None), + _fudai_button_text="加入购物粉丝团并关注主播", + _fudai_button_status="需要加入粉丝团", + _reservation_prejoined_fansclub=True, + _reservation_prejoin_fansclub_button_text="加入购物粉丝团并关注主播", + _reservation_prejoin_fansclub_ts=123.0, + ) + reservations = [] + + ok = record_high_value_reservation( + analyser=analyser, + fudai_content_text="高价值福袋", + prize_value_yuan=159.0, + lastsecond=200, + future_timestamp=time.time() + 600, + x=162, + needswitch=True, + wait_minutes=0, + post_switch_delay_s=0.0, + room_pos=5, + reservations=reservations, + reservation_focus_until_ts_ref=[0.0], + close_detail_callback=lambda: None, + swipe_up_switch_callback=lambda: None, + swipe_down_back_callback=lambda: None, + add_reservation_callback=lambda items, item: items.append(item), + sort_reservations_callback=lambda: None, + write_state_callback=lambda: None, + room_title="交个朋友", + room_title_raw="交个朋友", + task_count=2, + time_text="03:21", + participants=0, + copies=1, + ) + + assert ok is False + assert reservations[0]["prejoined_fansclub_before_reservation"] is True + assert reservations[0]["prejoin_fansclub_button_text"] == "加入购物粉丝团并关注主播" + assert reservations[0]["prejoin_fansclub_ts"] == 123.0 + + +def test_write_reservations_state_preserves_prejoined_fansclub_marker(tmp_path): + from douyin.reservation.douyin_reservation import write_reservations_state + + state_path = tmp_path / "reservations.json" + write_reservations_state( + state_path=str(state_path), + reservations=[ + { + "future_ts": time.time() + 600, + "prize_value_yuan": 159.0, + "content": "高价值福袋", + "pos_seen": 5, + "room_title": "交个朋友", + "room_title_raw": "交个朋友", + "task_count": 2, + "button_text": "加入购物粉丝团并关注主播", + "button_status": "需要加入粉丝团", + "prejoined_fansclub_before_reservation": True, + "prejoin_fansclub_button_text": "加入购物粉丝团并关注主播", + "prejoin_fansclub_ts": 123.0, + } + ], + room_pos=5, + current_room_title="交个朋友", + current_room_title_raw="交个朋友", + reason="test", + ) + + import json + + data = json.loads(state_path.read_text(encoding="utf-8")) + item = data["reservations"][0] + assert item["prejoined_fansclub_before_reservation"] is True + assert item["prejoin_fansclub_button_text"] == "加入购物粉丝团并关注主播" + assert item["prejoin_fansclub_ts"] == 123.0 + + +def test_duplicate_reservation_preserves_prejoined_fansclub_marker(): + from douyin.reservation.reservation_logic.add import add_reservation_core_logic + + ft = time.time() + 600 + reservations = [ + { + "future_ts": ft, + "prize_value_yuan": 159.0, + "content": "高价值福袋", + "room_title": "交个朋友", + } + ] + item = { + "future_ts": ft, + "prize_value_yuan": 159.0, + "content": "高价值福袋", + "room_title": "交个朋友", + "prejoined_fansclub_before_reservation": True, + "prejoin_fansclub_button_text": "加入购物粉丝团并关注主播", + "prejoin_fansclub_ts": 123.0, + } + + ok = add_reservation_core_logic( + reservations=reservations, + item=item, + max_reservations=10, + analyser=SimpleNamespace(_log_kv=MagicMock(return_value=None)), + write_state_callback=lambda: None, + ) + + assert ok is False + assert len(reservations) == 1 + assert reservations[0]["prejoined_fansclub_before_reservation"] is True + assert reservations[0]["prejoin_fansclub_button_text"] == "加入购物粉丝团并关注主播" + assert reservations[0]["prejoin_fansclub_ts"] == 123.0