refactor(fudai): 抽 attend_choujiang 中"评论"和"参与抽奖"分支到 handle_attend_click

将 attend_choujiang 中两个结构高度重叠的"点击参与按钮"分支抽到通用
handle_attend_click(analyser, mode) helper:

- 评论模式:仅点击 + 记 metric,return True
- 参与抽奖模式(mode="普通"):额外
  - 设置 _skip_stage_diagnosis_and_find_fudai 标志
  - 写 debug log(开始设置等待开奖标志)
  - 设置 _waiting_for_lottery_result / _lottery_result_expected_ts
  - 写 debug log(成功设置等待开奖标志)

fudai_attend.py: 1446 → 1309 行 (-137)
fudai_attend_branches.py: 217 → 349 行

验证:
- 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:47:24 +08:00
1 parent 4e89fadd2e
commit 6f4dbb2d8b
2 files changed
+137 -142

No files matched your search

+5 -142
View File
@@ -190,150 +190,13 @@ def attend_choujiang(analyser) -> bool:
"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
analyser.click(543, 2070)
analyser._log_kv(
"参与",
"点击参与抽奖(基于截图优化坐标)",
action="点击参与",
mode="评论",
base_x=543,
base_y=2070,
region="(306,2030)-(780,2110)",
)
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="attempt",
run_id=getattr(analyser, "_run_id", ""),
mode="评论",
)
except Exception:
pass
return True
from douyin.fudai.core.fudai_attend_branches import handle_attend_click
return handle_attend_click(analyser, "评论")
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
analyser.click(543, 2070)
analyser._log_kv(
"参与",
"点击参与抽奖(基于截图优化坐标)",
action="点击参与",
mode="普通",
base_x=543,
base_y=2070,
region="(306,2030)-(780,2110)",
)
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="attempt",
run_id=getattr(analyser, "_run_id", ""),
mode="普通",
)
except Exception:
pass
from douyin.fudai.core.fudai_attend_branches import handle_attend_click
# 设置标志:点击参与抽奖后直接查找福袋,跳过阶段检测
# 注意:点击参与按钮后会自动关闭详情页,返回直播间
try:
analyser._skip_stage_diagnosis_and_find_fudai = True
except Exception:
pass
# 设置等待开奖标志,避免参与后立即切换直播间
# #region agent log - set countdown
import json
from douyin.core.path_config import get_debug_log_path
current_ts_debug = int(time.time() * 1000)
with open(get_debug_log_path(), "a", encoding="utf-8") as f:
f.write(
json.dumps(
{
"id": f"log_{current_ts_debug}_set_countdown_start",
"timestamp": current_ts_debug,
"location": "fudai_attend.py:186",
"message": "开始设置等待开奖标志",
"data": {
"future_ts": getattr(
analyser, "_current_fudai_future_ts", 0.0
)
},
"sessionId": "debug-session",
"runId": "countdown_debug_run1",
"hypothesisId": "countdown_hyp2",
}
)
+ "\n"
)
# #endregion
try:
# 从 detail 中获取 future_ts(如果可用)
future_ts = getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0
if future_ts > 0:
current_time = time.time()
remain_seconds = int(future_ts - current_time)
if remain_seconds > 0:
# 设置等待开奖标志和预期开奖时间
analyser._waiting_for_lottery_result = True
analyser._lottery_result_expected_ts = future_ts
analyser._lottery_wait_start_ts = current_time
# #region agent log - countdown set
with open(get_debug_log_path(), "a", encoding="utf-8") as f:
f.write(
json.dumps(
{
"id": f"log_{current_ts_debug}_countdown_set",
"timestamp": current_ts_debug,
"location": "fudai_attend.py:195",
"message": "成功设置等待开奖标志",
"data": {
"waiting_for_lottery_result": analyser._waiting_for_lottery_result,
"lottery_result_expected_ts": analyser._lottery_result_expected_ts,
"future_ts": future_ts,
"current_time": current_time,
"remain_seconds": remain_seconds,
},
"sessionId": "debug-session",
"runId": "countdown_debug_run1",
"hypothesisId": "countdown_hyp2",
}
)
+ "\n"
)
# #endregion
analyser._log_kv(
"参与",
"点击参与抽奖后,设置等待开奖标志",
future_ts=int(future_ts),
remain_seconds=remain_seconds,
expected_result_ts=int(
analyser._lottery_result_expected_ts
),
)
except Exception as e:
try:
analyser._log_kv(
"参与",
"点击参与抽奖后,设置等待开奖标志时出错",
error=str(e),
)
except Exception:
pass
analyser._log_kv(
"参与",
"点击参与按钮完成,详情页将自动关闭,下次循环直接查找福袋,跳过阶段检测",
)
return True
return handle_attend_click(analyser, "普通")
elif attend_button_text and (
"加入粉丝团" in attend_button_text or "粉丝团" in attend_button_text
):
+132
View File
@@ -215,3 +215,135 @@ def handle_simple_fail(
analyser.click(500, 470)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
def handle_attend_click(analyser, mode: str) -> bool:
"""处理"评论"或"参与抽奖"分支:点击参与按钮、记录 metric。返回 True。
"参与抽奖"模式下还会:
- 设置 _skip_stage_diagnosis_and_find_fudai 标志
- 写 debug log(开始设置等待开奖标志)
- 设置 _waiting_for_lottery_result / _lottery_result_expected_ts
- 写 debug log(成功设置等待开奖标志)
"""
# 基于截图优化坐标:OCR区域(306,2030)-(780,2110),尺寸474x80
# 区域中心 X = 543, Y = 2070
analyser.click(543, 2070)
analyser._log_kv(
"参与",
"点击参与抽奖(基于截图优化坐标)",
action="点击参与",
mode=mode,
base_x=543,
base_y=2070,
region="(306,2030)-(780,2110)",
)
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="attempt",
run_id=getattr(analyser, "_run_id", ""),
mode=mode,
)
except Exception:
pass
if mode != "普通":
# 评论分支:直接返回,不设置等待开奖标志
return True
# 参与抽奖分支:设置标志 + 等待开奖
try:
analyser._skip_stage_diagnosis_and_find_fudai = True
except Exception:
pass
import json as _json
import time as _time
from douyin.core.path_config import get_debug_log_path
current_ts_debug = int(_time.time() * 1000)
try:
with open(get_debug_log_path(), "a", encoding="utf-8") as f:
f.write(
_json.dumps(
{
"id": f"log_{current_ts_debug}_set_countdown_start",
"timestamp": current_ts_debug,
"location": "fudai_attend_branches.py:handle_attend_click",
"message": "开始设置等待开奖标志",
"data": {
"future_ts": getattr(
analyser, "_current_fudai_future_ts", 0.0
)
},
"sessionId": "debug-session",
"runId": "countdown_debug_run1",
"hypothesisId": "countdown_hyp2",
}
)
+ "\n"
)
except Exception:
pass
try:
future_ts = getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0
if future_ts > 0:
current_time = _time.time()
remain_seconds = int(future_ts - current_time)
if remain_seconds > 0:
analyser._waiting_for_lottery_result = True
analyser._lottery_result_expected_ts = future_ts
analyser._lottery_wait_start_ts = current_time
try:
with open(get_debug_log_path(), "a", encoding="utf-8") as f:
f.write(
_json.dumps(
{
"id": f"log_{current_ts_debug}_countdown_set",
"timestamp": current_ts_debug,
"location": "fudai_attend_branches.py:handle_attend_click",
"message": "成功设置等待开奖标志",
"data": {
"waiting_for_lottery_result": analyser._waiting_for_lottery_result,
"lottery_result_expected_ts": analyser._lottery_result_expected_ts,
"future_ts": future_ts,
"current_time": current_time,
"remain_seconds": remain_seconds,
},
"sessionId": "debug-session",
"runId": "countdown_debug_run1",
"hypothesisId": "countdown_hyp2",
}
)
+ "\n"
)
except Exception:
pass
analyser._log_kv(
"参与",
"点击参与抽奖后,设置等待开奖标志",
future_ts=int(future_ts),
remain_seconds=remain_seconds,
expected_result_ts=int(analyser._lottery_result_expected_ts),
)
except Exception as e:
try:
analyser._log_kv(
"参与",
"点击参与抽奖后,设置等待开奖标志时出错",
error=str(e),
)
except Exception:
pass
analyser._log_kv(
"参与",
"点击参与按钮完成,详情页将自动关闭,下次循环直接查找福袋,跳过阶段检测",
)
return True