refactor(fudai): 抽 attend_choujiang 中 3 个简单失败分支用通用 helper

新增 handle_simple_fail 通用模板,覆盖结构完全相同的 3 个分支:
- watch_required(还需看播)
- cannot_participate(无法参与)
- watch_duration_insufficient(时长不足)

每个分支的逻辑都是:set fail_reason → log → metric → click(500,470) → return False。

fudai_attend.py: 1471 → 1446 行 (-25)
fudai_attend_branches.py: 184 → 217 行

验证:
- pytest 1013 passed
- compile OK

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 10:19:14 +08:00
1 parent 1c1f0abfc0
commit a3fe7b3ef0
2 files changed
+54 -46

No files matched your search

+21 -46
View File
@@ -166,54 +166,29 @@ def attend_choujiang(analyser) -> bool:
elif (
attend_button_text and "还需看播" in attend_button_text
): # 如果识别到需要看播
analyser.last_attend_fail_reason = "watch_required"
analyser._log_kv("参与", "需要看播时长:默认不参与", result="看播门槛")
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="fail",
reason="watch_required",
run_id=getattr(analyser, "_run_id", ""),
)
except Exception:
pass
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
from douyin.fudai.core.fudai_attend_branches import handle_simple_fail
return handle_simple_fail(
analyser, "需要看播时长:默认不参与", "看播门槛", "watch_required"
)
elif attend_button_text and "无法参与" in attend_button_text:
analyser._log_kv("参与", "无法参与(条件不满足)", result="无法参与")
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="fail",
reason="cannot_participate",
run_id=getattr(analyser, "_run_id", ""),
)
except Exception:
pass
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
from douyin.fudai.core.fudai_attend_branches import handle_simple_fail
return handle_simple_fail(
analyser,
"无法参与(条件不满足)",
"无法参与",
"cannot_participate",
)
elif attend_button_text and "时长不足" in attend_button_text:
analyser._log_kv("参与", "无法参与(看播时长不足)", result="时长不足")
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="fail",
reason="watch_duration_insufficient",
run_id=getattr(analyser, "_run_id", ""),
)
except Exception:
pass
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
from douyin.fudai.core.fudai_attend_branches import handle_simple_fail
return handle_simple_fail(
analyser,
"无法参与(看播时长不足)",
"时长不足",
"watch_duration_insufficient",
)
elif attend_button_text and "评论" in attend_button_text:
# 基于截图优化坐标:OCR区域(306,2030)-(780,2110),尺寸474x80
# 区域中心X = (306+780)/2 = 543,Y = (2030+2110)/2 = 2070
@@ -182,3 +182,36 @@ def handle_condition_not_met(analyser) -> bool:
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
def handle_simple_fail(
analyser, log_message: str, log_result: str, fail_reason: str
) -> bool:
"""通用"简单失败 + 关闭详情"模板,覆盖 watch_required / cannot_participate / watch_duration_insufficient 等。
流程:
1. 设置 last_attend_fail_reason = fail_reason
2. 记录"参与"日志(log_message + result=log_result)
3. 记录 metric: fudai_attend / fail / reason=fail_reason
4. click(500, 470) 关闭详情
5. 返回 False
"""
try:
analyser.last_attend_fail_reason = fail_reason
except Exception:
pass
analyser._log_kv("参与", log_message, result=log_result)
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="fail",
reason=fail_reason,
run_id=getattr(analyser, "_run_id", ""),
)
except Exception:
pass
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False