Files
douyin_guaji-main/douyin/fudai/core/fudai_attend.py
T
CodexandClaude Sonnet 4.5 c4544489ac refactor(coords): 提取点击坐标常量,消除硬编码
在 coords.py 添加 DETAIL_OUTSIDE_CLICK、PARTICIPATE_BTN_CLICK、
FANSCLUB_JOIN_CLICK、FANSCLUB_JOIN_FALLBACK_CLICK 常量,
替换 fudai_attend/fansclub_handler/join_decision 中的硬编码坐标。

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-05-23 21:20:17 +08:00

582 lines
25 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""福袋参与模块
包含参与抽奖、处理各种按钮状态的逻辑。
"""
from __future__ import annotations
import os
import time
from typing import Any
from douyin.core import path_config
from .coords import DETAIL_OUTSIDE_CLICK, PARTICIPATE_BTN_CLICK
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属性缺失或不完整,尝试从其它缓存字段复用识别结果(例如 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
# 如果并行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(*DETAIL_OUTSIDE_CLICK)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
# 基于并行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(*DETAIL_OUTSIDE_CLICK)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
elif (
attend_button_text and "参与成功" in attend_button_text
): # 如果识别到已经参与抽奖
analyser._log_kv("参与", "已参与,等待开奖", result="已参与")
# 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(*DETAIL_OUTSIDE_CLICK) # 点击刚才打开福袋的旁边位置
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return True
elif attend_button_text and (
("活动已结束" in attend_button_text)
or ("活动己结束" in attend_button_text)
or ("福袋超时" in attend_button_text)
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(*DETAIL_OUTSIDE_CLICK)
except Exception:
pass
return False
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(*DETAIL_OUTSIDE_CLICK)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
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(*DETAIL_OUTSIDE_CLICK)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
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(*DETAIL_OUTSIDE_CLICK)
analyser._log_kv("参与", "关闭福袋详情", action="关闭详情")
return False
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(*PARTICIPATE_BTN_CLICK)
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
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(*PARTICIPATE_BTN_CLICK)
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
# 设置标志:点击参与抽奖后直接查找福袋,跳过阶段检测
# 注意:点击参与按钮后会自动关闭详情页,返回直播间
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
elif attend_button_text and (
"加入粉丝团" in attend_button_text or "粉丝团" in attend_button_text
):
from douyin.fudai.core.fudai_attend_pkg.fansclub_handler import (
handle_fansclub_branch,
)
return handle_fansclub_branch(analyser, attend_button_text)
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(*PARTICIPATE_BTN_CLICK)
analyser._log_kv(
"参与",
"点击开始观看(基于截图优化坐标)",
action="点击观看",
base_x=543,
base_y=2070,
region="(306,2030)-(780,2110)",
)
return True
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(*PARTICIPATE_BTN_CLICK)
analyser._log_kv(
"参与",
"点击参与(基于截图优化坐标)",
action="点击参与",
base_x=543,
base_y=2070,
region="(306,2030)-(780,2110)",
)
return True
# 未命中任何已知按钮文案时,避免死循环:限次重试并稍作等待
click_times += 1
if click_times < 2:
try:
analyser.operation.delay(0.2)
except Exception:
pass
analyser._log_kv(
"参与",
"参与抽奖失败:并行OCR结果无效",
last_text=attend_button_text or "",
result="参与失败",
)
try:
analyser._log_kv(
"统计",
"metric",
metric="fudai_attend",
metric_type="fail",
reason="parallel_ocr_invalid",
run_id=getattr(analyser, "_run_id", ""),
)
except Exception:
pass
return False