fix: broaden fansclub reservation prejoin match

This commit is contained in:
Your Name committed 2026-06-23 10:00:56 +08:00
1 parent ba9ac4cb0e
commit 920c51d460
2 files changed
+86 -2

No files matched your search

@@ -9,6 +9,7 @@ from __future__ import annotations
import re
import time
from datetime import datetime
from difflib import SequenceMatcher
from typing import Any, Tuple
from .helpers import _is_short_countdown_text, is_watch_task_fudai
@@ -54,13 +55,53 @@ def _has_valid_countdown(detail: Tuple) -> bool:
return False
_FANSCLUB_BUTTON_CANONICALS = (
"加入粉丝团",
"加入直播粉丝团",
"加入购物粉丝团",
"加入直播粉丝团并关注主播",
"加入购物粉丝团并关注主播",
)
def _normalize_button_text_for_match(text: str) -> str:
return re.sub(r"[\s·。.,,、::||\-—_]+", "", str(text or ""))
def _looks_like_fansclub_join_button(button_text: str, button_status: str = "") -> bool:
"""判断按钮是否是加入粉丝团任务,兼容直播/购物粉丝团和轻微 OCR 差异。"""
text_norm = _normalize_button_text_for_match(button_text)
status_norm = _normalize_button_text_for_match(button_status)
joined = text_norm + status_norm
if not joined:
return False
if "粉丝团" in joined or "需要加入粉丝团" in status_norm:
return True
if ("加入" in joined or "加人" in joined) and "关注主播" in joined and "粉丝" in joined:
return True
for canonical in _FANSCLUB_BUTTON_CANONICALS:
canonical_norm = _normalize_button_text_for_match(canonical)
score = SequenceMatcher(None, text_norm, canonical_norm).ratio()
if score >= 0.78 and (
"关注主播" in joined
or "粉丝" in joined
or "加入" in joined
or "加人" in joined
):
return True
return False
def _prejoin_fansclub_before_reservation(
analyser: Any,
button_text: str,
button_status: str,
remain_seconds: float | int | None,
) -> bool:
"""高价值预约前尽量先完成粉丝团任务,回退时减少临开奖动作。"""
text = str(button_text or "").strip()
status = str(button_status or "").strip()
try:
setattr(analyser, "_reservation_prejoined_fansclub", False)
setattr(analyser, "_reservation_prejoin_fansclub_button_text", text)
@@ -68,7 +109,7 @@ def _prejoin_fansclub_before_reservation(
except Exception:
pass
if not text or "粉丝团" not in text:
if not _looks_like_fansclub_join_button(text, status):
return False
if not bool(getattr(analyser, "auto_join_fansclub", False)):
@@ -77,6 +118,7 @@ def _prejoin_fansclub_before_reservation(
"预约",
"预约前粉丝团预处理跳过:未启用自动加入粉丝团",
button_text=text[:80],
button_status=status[:40],
reason="auto_join_fansclub_disabled",
)
except Exception:
@@ -88,6 +130,7 @@ def _prejoin_fansclub_before_reservation(
"预约",
"预约前粉丝团预处理:检测到粉丝团任务,先尝试加入",
button_text=text[:80],
button_status=status[:40],
remain_seconds=(
int(remain_seconds) if remain_seconds is not None else None
),
@@ -114,6 +157,7 @@ def _prejoin_fansclub_before_reservation(
"预约前粉丝团预处理完成",
result=ok,
button_text=text[:80],
button_status=status[:40],
)
except Exception:
pass
@@ -122,6 +166,7 @@ def _prejoin_fansclub_before_reservation(
"reservation_prejoin_fansclub",
"done" if ok else "failed",
button_text=text[:80],
button_status=status[:40],
)
return ok
except Exception as exc:
@@ -132,6 +177,7 @@ def _prejoin_fansclub_before_reservation(
error=str(exc),
error_type=type(exc).__name__,
button_text=text[:80],
button_status=status[:40],
level="WARNING",
)
except Exception:
@@ -844,6 +890,7 @@ def basic_join_decision(analyser: Any, detail: Tuple) -> bool:
_prejoin_fansclub_before_reservation(
analyser,
button_text,
str(getattr(analyser, "_fudai_button_status", "") or ""),
remain_seconds,
)
+38 -1
View File
@@ -3,7 +3,10 @@ from __future__ import annotations
import time
from unittest.mock import MagicMock, patch
from douyin.fudai.core.fudai_filter_pkg.join_decision import basic_join_decision
from douyin.fudai.core.fudai_filter_pkg.join_decision import (
_looks_like_fansclub_join_button,
basic_join_decision,
)
def _mk_analyser():
@@ -86,6 +89,40 @@ def test_high_value_reservation_prejoins_fansclub_before_recording():
analyser.swipe_up_to_next_live.assert_called_once()
def test_fansclub_button_match_accepts_live_and_ocr_variants():
assert _looks_like_fansclub_join_button("加入直播粉丝团并关注主播")
assert _looks_like_fansclub_join_button("加入购物粉丝团并关注主播")
assert _looks_like_fansclub_join_button("加人直播粉丝团并关注主播")
assert _looks_like_fansclub_join_button(
"加入直播粉丝并关注主播",
"需要加入粉丝团",
)
assert not _looks_like_fansclub_join_button("一键发表评论")
def test_high_value_reservation_prejoins_live_fansclub_variant():
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))
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",
return_value=True,
) as mock_prejoin, patch(
"douyin.reservation.reservation_logic.record.record_high_value_reservation",
return_value=False,
):
ok = basic_join_decision(analyser, detail)
assert ok is True
mock_prejoin.assert_called_once_with(analyser, "加入直播粉丝团并关注主播")
def test_high_value_countdown_within_join_window_attends():
analyser = _mk_analyser()
future_ts = time.time() + 30 # <= join_ahead_seconds