refactor(fudai): 提取 attend_choujiang 中 4 个独立分支到 fudai_attend_branches
将 attend_choujiang 内 4 个 button_text 命中的早返回分支抽出: - handle_no_parallel_ocr:并行OCR结果不可用 - handle_condition_not_met:不满足参与条件 - handle_attend_already_success:已参与/参与成功 - handle_activity_ended:活动已结束/福袋超时 每个 helper 处理一个分支并返回 True/False,与原代码 return 值一致。 fudai_attend.py: 1629 → 1471 行 (-158) 新增 fudai_attend_branches.py: 184 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5d2a0ac3ff
commit
1c1f0abfc0
2 files changed
+197
-171
No files matched your search
@@ -136,154 +136,23 @@ def attend_choujiang(analyser) -> bool:
|
||||
|
||||
# 如果并行OCR结果不可用,直接放弃参与(决策已由福袋详情中的并行OCR进行)
|
||||
if not attend_button_text:
|
||||
analyser.last_attend_fail_reason = "no_parallel_ocr_result"
|
||||
analyser._log_kv("参与", "并行OCR结果不可用,放弃参与", result="不参与")
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="no_parallel_ocr_result",
|
||||
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_no_parallel_ocr
|
||||
|
||||
return handle_no_parallel_ocr(analyser)
|
||||
|
||||
# 基于并行OCR结果进行决策
|
||||
if attend_button_text and "不满足参与条件" in attend_button_text:
|
||||
analyser.last_attend_fail_reason = "condition_not_met"
|
||||
analyser._log_kv("参与", "不满足参与条件,直接放弃", result="不满足条件")
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="condition_not_met",
|
||||
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_condition_not_met
|
||||
|
||||
return handle_condition_not_met(analyser)
|
||||
elif (
|
||||
attend_button_text and "参与成功" in attend_button_text
|
||||
): # 如果识别到已经参与抽奖
|
||||
analyser._log_kv("参与", "已参与,等待开奖", result="已参与")
|
||||
from douyin.fudai.core.fudai_attend_branches import (
|
||||
handle_attend_already_success,
|
||||
)
|
||||
|
||||
# future_ts 可能尚未在此分支前赋值,先做兜底初始化(供 metric 使用)
|
||||
future_ts = 0.0
|
||||
try:
|
||||
future_ts = float(
|
||||
getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0
|
||||
)
|
||||
except Exception:
|
||||
future_ts = 0.0
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="success",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
future_ts=int(future_ts) if future_ts else None,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 记录最后一次成功参与的时间
|
||||
try:
|
||||
analyser._last_successful_attend_ts = time.time()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 记录已参与的福袋(通过 future_ts 标识),避免重复参与
|
||||
future_ts = 0.0
|
||||
try:
|
||||
# 从 detail 中获取 future_ts(如果可用)
|
||||
future_ts = getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0
|
||||
# 如果没有从 detail 中获取到 future_ts,尝试直接解析当前详情页的倒计时文本
|
||||
if not future_ts:
|
||||
try:
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
|
||||
content, time_text, participants, copies, prize_value_yuan = (
|
||||
douyin_fudai_core.get_fudai_contain(analyser, renwu=1)
|
||||
)
|
||||
# 尝试用 operation.check_countdown 解析 time_text
|
||||
try:
|
||||
res = analyser.operation.check_countdown(time_text)
|
||||
if isinstance(res, tuple) and len(res) == 2:
|
||||
_, parsed_future = res
|
||||
future_ts = float(parsed_future or 0.0)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
if future_ts > 0:
|
||||
participated_fudai_ts = getattr(
|
||||
analyser, "_participated_fudai_future_ts", set()
|
||||
)
|
||||
if not isinstance(participated_fudai_ts, set):
|
||||
participated_fudai_ts = set()
|
||||
participated_fudai_ts.add(future_ts)
|
||||
analyser._participated_fudai_future_ts = participated_fudai_ts
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"记录已参与福袋",
|
||||
future_ts=future_ts,
|
||||
total_participated=len(participated_fudai_ts),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 设置开奖等待状态(如果 future_ts 可用)
|
||||
if future_ts > 0:
|
||||
try:
|
||||
current_time = time.time()
|
||||
remain_seconds = max(0, 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
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"参与成功,设置等待开奖标志",
|
||||
remain_seconds=int(remain_seconds),
|
||||
future_ts=int(future_ts),
|
||||
lottery_result_expected_ts=int(
|
||||
analyser._lottery_result_expected_ts
|
||||
),
|
||||
)
|
||||
else:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"参与成功,但倒计时已过期,不设置等待标志",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=int(remain_seconds),
|
||||
)
|
||||
except Exception as e:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"设置开奖等待状态时出错",
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
)
|
||||
|
||||
# 设置标志:已经关闭了详情,避免后续再次点击返回键
|
||||
try:
|
||||
analyser._detail_closed_by_attend = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
analyser.click(500, 470) # 点击刚才打开福袋的旁边位置
|
||||
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
|
||||
return True
|
||||
return handle_attend_already_success(analyser, attend_button_text)
|
||||
elif attend_button_text and (
|
||||
("活动已结束" in attend_button_text)
|
||||
or ("活动己结束" in attend_button_text)
|
||||
@@ -291,36 +160,9 @@ def attend_choujiang(analyser) -> bool:
|
||||
or ("活动超时" in attend_button_text)
|
||||
or ("已超时" in attend_button_text)
|
||||
):
|
||||
# 活动结束/福袋超时:按一次返回键退出详情
|
||||
analyser.last_attend_fail_reason = "activity_ended"
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"活动已结束/福袋超时:按返回键退出详情",
|
||||
result="activity_ended",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="activity_ended",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.operation.click_back(analyser.device_id)
|
||||
analyser.operation.delay(0.5)
|
||||
except Exception:
|
||||
try:
|
||||
analyser.click(500, 470)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
from douyin.fudai.core.fudai_attend_branches import handle_activity_ended
|
||||
|
||||
return handle_activity_ended(analyser)
|
||||
elif (
|
||||
attend_button_text and "还需看播" in attend_button_text
|
||||
): # 如果识别到需要看播
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
"""attend_choujiang 分支辅助函数
|
||||
|
||||
从 douyin/fudai/core/fudai_attend.py:attend_choujiang 中提取的相对独立的早返回分支。
|
||||
每个 helper 处理一个 button_text 命中情况,返回 True/False(与原代码 return 值一致)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
|
||||
def handle_attend_already_success(analyser, attend_button_text: str) -> bool:
|
||||
"""处理"参与成功/已参与"分支:记录、设置等待开奖标志、关闭详情。返回 True。"""
|
||||
analyser._log_kv("参与", "已参与,等待开奖", result="已参与")
|
||||
|
||||
future_ts = 0.0
|
||||
try:
|
||||
future_ts = float(getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0)
|
||||
except Exception:
|
||||
future_ts = 0.0
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="success",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
future_ts=int(future_ts) if future_ts else None,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._last_successful_attend_ts = time.time()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
future_ts = 0.0
|
||||
try:
|
||||
future_ts = getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0
|
||||
if not future_ts:
|
||||
try:
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
|
||||
content, time_text, participants, copies, prize_value_yuan = (
|
||||
douyin_fudai_core.get_fudai_contain(analyser, renwu=1)
|
||||
)
|
||||
try:
|
||||
res = analyser.operation.check_countdown(time_text)
|
||||
if isinstance(res, tuple) and len(res) == 2:
|
||||
_, parsed_future = res
|
||||
future_ts = float(parsed_future or 0.0)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
if future_ts > 0:
|
||||
participated_fudai_ts = getattr(
|
||||
analyser, "_participated_fudai_future_ts", set()
|
||||
)
|
||||
if not isinstance(participated_fudai_ts, set):
|
||||
participated_fudai_ts = set()
|
||||
participated_fudai_ts.add(future_ts)
|
||||
analyser._participated_fudai_future_ts = participated_fudai_ts
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"记录已参与福袋",
|
||||
future_ts=future_ts,
|
||||
total_participated=len(participated_fudai_ts),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if future_ts > 0:
|
||||
try:
|
||||
current_time = time.time()
|
||||
remain_seconds = max(0, 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
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"参与成功,设置等待开奖标志",
|
||||
remain_seconds=int(remain_seconds),
|
||||
future_ts=int(future_ts),
|
||||
lottery_result_expected_ts=int(
|
||||
analyser._lottery_result_expected_ts
|
||||
),
|
||||
)
|
||||
else:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"参与成功,但倒计时已过期,不设置等待标志",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=int(remain_seconds),
|
||||
)
|
||||
except Exception as e:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"设置开奖等待状态时出错",
|
||||
error=str(e),
|
||||
error_type=type(e).__name__,
|
||||
)
|
||||
|
||||
try:
|
||||
analyser._detail_closed_by_attend = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
analyser.click(500, 470)
|
||||
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
|
||||
return True
|
||||
|
||||
|
||||
def handle_activity_ended(analyser) -> bool:
|
||||
"""处理"活动已结束/福袋超时"分支:按返回键退出详情。返回 False。"""
|
||||
analyser.last_attend_fail_reason = "activity_ended"
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"活动已结束/福袋超时:按返回键退出详情",
|
||||
result="activity_ended",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="activity_ended",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser.operation.click_back(analyser.device_id)
|
||||
analyser.operation.delay(0.5)
|
||||
except Exception:
|
||||
try:
|
||||
analyser.click(500, 470)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def handle_no_parallel_ocr(analyser) -> bool:
|
||||
"""处理"并行OCR结果不可用"分支:放弃参与并关闭详情。返回 False。"""
|
||||
analyser.last_attend_fail_reason = "no_parallel_ocr_result"
|
||||
analyser._log_kv("参与", "并行OCR结果不可用,放弃参与", result="不参与")
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="no_parallel_ocr_result",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.click(500, 470)
|
||||
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
|
||||
return False
|
||||
|
||||
|
||||
def handle_condition_not_met(analyser) -> bool:
|
||||
"""处理"不满足参与条件"分支:放弃参与并关闭详情。返回 False。"""
|
||||
analyser.last_attend_fail_reason = "condition_not_met"
|
||||
analyser._log_kv("参与", "不满足参与条件,直接放弃", result="不满足条件")
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"统计",
|
||||
"metric",
|
||||
metric="fudai_attend",
|
||||
metric_type="fail",
|
||||
reason="condition_not_met",
|
||||
run_id=getattr(analyser, "_run_id", ""),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser.click(500, 470)
|
||||
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
|
||||
return False
|
||||
Reference in new issue
Block a user