refactor(fudai): 抽 attend_choujiang 中"按钮状态推断 + 并行 OCR 缓存复用"块到 resolve_attend_button_text
将 122 行的"按钮状态解析"逻辑抽到 fudai_attend_branches.resolve_attend_button_text。 逻辑: - 优先级1:已有的 _fudai_button_text/_fudai_button_status(并行 OCR 设置) - 优先级2:缺失则从 button_text_raw / 映射表中查找候选 + 推断状态 - 优先级3:检查 OCR 时间戳——5 秒内有效;超时但同房间则复用 返回 attend_button_text(不可用则空字符串)。 fudai_attend.py: 246 → 134 行 (-112) fudai_attend_branches.py: 366 → 496 行 attend_choujiang 函数从 232 → 120 行 fudai_attend.py 跌破 150 行(1629 → 134,本会话累计削减 1495 行 = -92%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e5f7434034
commit
1801e50a4c
2 files changed
+133
-115
No files matched your search
@@ -17,122 +17,10 @@ def attend_choujiang(analyser) -> bool:
|
||||
analyser.last_attend_fail_reason = ""
|
||||
click_times = 0
|
||||
while click_times < 2:
|
||||
# 优化:优先使用并行OCR中已识别的按钮状态(避免重复识别)
|
||||
attend_button_text = ""
|
||||
button_status = getattr(analyser, "_fudai_button_status", None)
|
||||
button_text = getattr(analyser, "_fudai_button_text", "")
|
||||
try:
|
||||
current_room_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
except Exception:
|
||||
current_room_pos = 0
|
||||
try:
|
||||
cached_room_pos = int(getattr(analyser, "_fudai_button_room_pos", 0) or 0)
|
||||
except Exception:
|
||||
cached_room_pos = 0
|
||||
# 解析参与按钮文本(从并行 OCR / 缓存 / 映射表中获取)
|
||||
from douyin.fudai.core.fudai_attend_branches import resolve_attend_button_text
|
||||
|
||||
# 如果并行OCR属性缺失或不完整,尝试从其它缓存字段复用识别结果(例如 button_text_raw 或映射表)
|
||||
if not button_text or not button_status:
|
||||
try:
|
||||
candidate = getattr(analyser, "button_text_raw", None)
|
||||
if not candidate:
|
||||
try:
|
||||
room_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
room_map = getattr(analyser, "_room_pos_to_title_map", {}) or {}
|
||||
if room_map and room_pos in room_map:
|
||||
info = room_map.get(room_pos) or {}
|
||||
if isinstance(info, dict):
|
||||
fudai_detail = info.get("fudai_detail") or {}
|
||||
candidate = (
|
||||
fudai_detail.get("button_text")
|
||||
or fudai_detail.get("button_text_raw")
|
||||
or None
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if candidate:
|
||||
bt = str(candidate or "").strip()
|
||||
if bt:
|
||||
# 推断按钮状态(与并行OCR的判断逻辑保持一致)
|
||||
if "参与成功" in bt or "等待开奖" in bt:
|
||||
bs = "已参与"
|
||||
elif "加入粉丝团" in bt or "粉丝团" in bt:
|
||||
bs = "需要加入粉丝团"
|
||||
elif "评论" in bt or "发表评论" in bt or "一键发表评论" in bt:
|
||||
bs = "需要评论"
|
||||
elif "还需看播" in bt or "开始观看" in bt:
|
||||
bs = "需要观看"
|
||||
elif "未达成" in bt:
|
||||
bs = "未达成要求"
|
||||
elif "参与抽奖" in bt or "参与" in bt:
|
||||
bs = "可以参与"
|
||||
else:
|
||||
bs = "未知状态"
|
||||
|
||||
try:
|
||||
analyser._fudai_button_status = bs
|
||||
analyser._fudai_button_text = bt
|
||||
analyser._fudai_button_ocr_time = time.time()
|
||||
try:
|
||||
analyser._fudai_button_room_pos = int(
|
||||
getattr(analyser, "room_pos", 0) or 0
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"复用缓存的并行OCR文本作为按钮状态",
|
||||
button_text=(bt[:100] if bt else ""),
|
||||
button_status=bs,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
button_text = bt
|
||||
button_status = bs
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 使用并行OCR识别的按钮状态(如果可用且在有效期内)
|
||||
try:
|
||||
button_ocr_time = getattr(analyser, "_fudai_button_ocr_time", 0)
|
||||
current_time = time.time()
|
||||
if button_text and button_status and current_time - button_ocr_time < 5.0:
|
||||
attend_button_text = button_text
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"使用并行OCR识别的按钮状态",
|
||||
button_text=(
|
||||
attend_button_text[:100] if attend_button_text else ""
|
||||
),
|
||||
button_status=button_status,
|
||||
)
|
||||
elif button_text and button_status:
|
||||
same_room = (
|
||||
not current_room_pos
|
||||
or not cached_room_pos
|
||||
or current_room_pos == cached_room_pos
|
||||
)
|
||||
if same_room:
|
||||
attend_button_text = button_text
|
||||
try:
|
||||
age_ms = int(
|
||||
max(0.0, current_time - float(button_ocr_time)) * 1000
|
||||
)
|
||||
except Exception:
|
||||
age_ms = None
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"使用并行OCR缓存的按钮状态(超时复用)",
|
||||
button_text=(
|
||||
attend_button_text[:100] if attend_button_text else ""
|
||||
),
|
||||
button_status=button_status,
|
||||
age_ms=age_ms,
|
||||
room_pos=current_room_pos,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
attend_button_text = resolve_attend_button_text(analyser)
|
||||
|
||||
# 如果并行OCR结果不可用,直接放弃参与(决策已由福袋详情中的并行OCR进行)
|
||||
if not attend_button_text:
|
||||
|
||||
@@ -8,6 +8,136 @@ from __future__ import annotations
|
||||
import time
|
||||
|
||||
|
||||
def resolve_attend_button_text(analyser):
|
||||
"""从 analyser 缓存解析"参与按钮文本"。
|
||||
|
||||
优先级:
|
||||
1. 已有的 _fudai_button_text + _fudai_button_status(并行 OCR 设置)
|
||||
2. 缺失则从 button_text_raw / 映射表中查找候选并推断状态
|
||||
3. 检查 OCR 时间戳:5 秒内有效 → 直接使用;超时但同房间 → 复用
|
||||
|
||||
返回 attend_button_text(若不可用则返回空字符串)。
|
||||
"""
|
||||
button_status = getattr(analyser, "_fudai_button_status", None)
|
||||
button_text = getattr(analyser, "_fudai_button_text", "")
|
||||
|
||||
try:
|
||||
current_room_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
except Exception:
|
||||
current_room_pos = 0
|
||||
try:
|
||||
cached_room_pos = int(getattr(analyser, "_fudai_button_room_pos", 0) or 0)
|
||||
except Exception:
|
||||
cached_room_pos = 0
|
||||
|
||||
# 如果并行 OCR 属性缺失或不完整,尝试从其它缓存字段复用识别结果
|
||||
if not button_text or not button_status:
|
||||
try:
|
||||
candidate = getattr(analyser, "button_text_raw", None)
|
||||
if not candidate:
|
||||
try:
|
||||
room_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
room_map = getattr(analyser, "_room_pos_to_title_map", {}) or {}
|
||||
if room_map and room_pos in room_map:
|
||||
info = room_map.get(room_pos) or {}
|
||||
if isinstance(info, dict):
|
||||
fudai_detail = info.get("fudai_detail") or {}
|
||||
candidate = (
|
||||
fudai_detail.get("button_text")
|
||||
or fudai_detail.get("button_text_raw")
|
||||
or None
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if candidate:
|
||||
bt = str(candidate or "").strip()
|
||||
if bt:
|
||||
# 推断按钮状态(与并行 OCR 的判断逻辑保持一致)
|
||||
if "参与成功" in bt or "等待开奖" in bt:
|
||||
bs = "已参与"
|
||||
elif "加入粉丝团" in bt or "粉丝团" in bt:
|
||||
bs = "需要加入粉丝团"
|
||||
elif "评论" in bt or "发表评论" in bt or "一键发表评论" in bt:
|
||||
bs = "需要评论"
|
||||
elif "还需看播" in bt or "开始观看" in bt:
|
||||
bs = "需要观看"
|
||||
elif "未达成" in bt:
|
||||
bs = "未达成要求"
|
||||
elif "参与抽奖" in bt or "参与" in bt:
|
||||
bs = "可以参与"
|
||||
else:
|
||||
bs = "未知状态"
|
||||
|
||||
try:
|
||||
analyser._fudai_button_status = bs
|
||||
analyser._fudai_button_text = bt
|
||||
analyser._fudai_button_ocr_time = time.time()
|
||||
try:
|
||||
analyser._fudai_button_room_pos = int(
|
||||
getattr(analyser, "room_pos", 0) or 0
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"复用缓存的并行OCR文本作为按钮状态",
|
||||
button_text=(bt[:100] if bt else ""),
|
||||
button_status=bs,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
button_text = bt
|
||||
button_status = bs
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 使用并行 OCR 识别的按钮状态(如果可用且在有效期内)
|
||||
attend_button_text = ""
|
||||
try:
|
||||
button_ocr_time = getattr(analyser, "_fudai_button_ocr_time", 0)
|
||||
current_time = time.time()
|
||||
if button_text and button_status and current_time - button_ocr_time < 5.0:
|
||||
attend_button_text = button_text
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"使用并行OCR识别的按钮状态",
|
||||
button_text=(
|
||||
attend_button_text[:100] if attend_button_text else ""
|
||||
),
|
||||
button_status=button_status,
|
||||
)
|
||||
elif button_text and button_status:
|
||||
same_room = (
|
||||
not current_room_pos
|
||||
or not cached_room_pos
|
||||
or current_room_pos == cached_room_pos
|
||||
)
|
||||
if same_room:
|
||||
attend_button_text = button_text
|
||||
try:
|
||||
age_ms = int(
|
||||
max(0.0, current_time - float(button_ocr_time)) * 1000
|
||||
)
|
||||
except Exception:
|
||||
age_ms = None
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"使用并行OCR缓存的按钮状态(超时复用)",
|
||||
button_text=(
|
||||
attend_button_text[:100] if attend_button_text else ""
|
||||
),
|
||||
button_status=button_status,
|
||||
age_ms=age_ms,
|
||||
room_pos=current_room_pos,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return attend_button_text
|
||||
|
||||
|
||||
def handle_simple_click(analyser, action_label: str, log_message: str) -> bool:
|
||||
"""通用"点击 543,2070 + 简单日志"分支模板,覆盖"开始观看"/"参与"等无额外副作用的点击。
|
||||
|
||||
|
||||
Reference in new issue
Block a user