refactor(fudai): 抽 _handle_in_fudai_detail 步骤2+3 第一次打开分支 (2568 → 2216)
新增 5 个 helper 到 handle_detail_helpers.py: - handle_first_open_branch 第一次打开主入口 - _validate_first_open_detail 数据验证失败处理 - _check_skip_and_handle 筛选检查 + 高价值预约 + 切房 - _try_high_value_reservation 高价值倒计时阈值判断(return/continue/still_skip) - _handle_skip_filter 普通筛选不通过:关详情 + 切房 - _fallback_swipe_up swipe_up_to_next_live 不存在时的兜底 净减 354 行嵌套样板代码。新增 8 个单元测试覆盖: - detail 无效 / 短 tuple - 数据验证失败 - 筛选通过保存数据 - 普通筛选不通过 + needswitch true/false - 高价值阈值内立即参与 - 高价值阈值外记预约 至此 _handle_in_fudai_detail 总长 1010 → 506 行(-50%), douyin_fudai.py 累计 4121 → 2216(-1905 行 / -46%)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
aeb51f73c7
commit
ca953cb4ba
3 files changed
+576
-354
No files matched your search
+2
-354
@@ -2016,360 +2016,8 @@ class fudai_analyse:
|
||||
self._log_kv("参与", "第二次打开:直接点击参与异常", error=str(e))
|
||||
# 异常时继续正常流程
|
||||
else:
|
||||
# 步骤2:读取福袋详情
|
||||
step2_start = time.time()
|
||||
# 第一次打开:完整读取详情(包括内容、倒计时、价值)
|
||||
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
|
||||
self,
|
||||
fudai_x=0,
|
||||
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
|
||||
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
|
||||
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
|
||||
)
|
||||
|
||||
# 在第一次打开时立即进行筛选检查,如果不符合要求则直接跳过
|
||||
# 注意:这里只检查筛选条件,不执行参与操作
|
||||
if detail and isinstance(detail, tuple) and len(detail) >= 6:
|
||||
# 先进行数据验证
|
||||
is_valid = True
|
||||
try:
|
||||
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(
|
||||
detail, self
|
||||
)
|
||||
if not is_valid:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时数据验证失败,跳过",
|
||||
validation_reason=reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 关闭详情页并返回
|
||||
try:
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 步骤2结束计时
|
||||
step2_elapsed = (time.time() - step2_start) * 1000
|
||||
self._log_kv(
|
||||
"详情处理",
|
||||
"步骤2[读取福袋详情]-完成",
|
||||
总耗时毫秒=step2_elapsed,
|
||||
)
|
||||
|
||||
# 清除任务数量标志
|
||||
_hd_helpers._clear_task_count_flag(self)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(数据验证失败)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 步骤3:筛选检查
|
||||
step3_start = time.time()
|
||||
# 执行筛选检查(不执行参与操作,只检查是否符合筛选条件)
|
||||
try:
|
||||
skip, skip_reason = douyin_fudai_core.should_skip_fudai_by_config(
|
||||
self, detail
|
||||
)
|
||||
if skip:
|
||||
# 检查是否是高价值福袋,如果是则记录预约或立即参与
|
||||
try:
|
||||
# 提取价值信息
|
||||
prize_value_yuan = 0.0
|
||||
countdown_tuple = None
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
# 使用解包避免索引告警
|
||||
_, _, _, _, prize_value_yuan, countdown_tuple = detail
|
||||
|
||||
# 检查是否启用高价值预约
|
||||
high_value_enabled = bool(
|
||||
getattr(self, "high_value_fudai_enabled", False)
|
||||
)
|
||||
high_value_threshold = float(
|
||||
getattr(self, "high_value_fudai_threshold_yuan", 500.0)
|
||||
or 500.0
|
||||
)
|
||||
|
||||
# 检查价值是否达到高价值阈值
|
||||
is_high_value = (
|
||||
high_value_enabled
|
||||
and prize_value_yuan > 0
|
||||
and prize_value_yuan >= high_value_threshold
|
||||
and isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 2
|
||||
and float(countdown_tuple[1] or 0.0) > 0.0
|
||||
)
|
||||
|
||||
# 如果被倒计时过滤筛掉(倒计时大于阈值),但价值达到高价值阈值
|
||||
# 需要检查剩余秒数是否在"预约参与阈值"内,如果在阈值内则立即参与,否则记录预约
|
||||
if is_high_value and skip_reason.startswith(
|
||||
"countdown_gt_"
|
||||
):
|
||||
# 计算剩余秒数
|
||||
remain_seconds = None
|
||||
if (
|
||||
isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 2
|
||||
):
|
||||
try:
|
||||
now = time.time()
|
||||
cd_start = float(countdown_tuple[0] or 0.0)
|
||||
cd_end = float(countdown_tuple[1] or 0.0)
|
||||
if cd_start > 0 and cd_end > cd_start:
|
||||
remain_seconds = float(cd_end - now)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 获取"预约参与阈值"配置
|
||||
join_ahead_seconds = int(
|
||||
getattr(
|
||||
self, "high_value_fudai_join_ahead_seconds", 100
|
||||
)
|
||||
or 120
|
||||
)
|
||||
|
||||
# 如果剩余秒数在"预约参与阈值"内,立即参与而不是预约
|
||||
if (
|
||||
remain_seconds is not None
|
||||
and remain_seconds <= join_ahead_seconds
|
||||
):
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:倒计时大于阈值,但剩余秒数在预约参与阈值内,立即参与",
|
||||
reason=skip_reason,
|
||||
value_yuan=prize_value_yuan,
|
||||
threshold=high_value_threshold,
|
||||
remain_seconds=int(remain_seconds),
|
||||
join_ahead_threshold=join_ahead_seconds,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 不关闭详情,继续走立即参与逻辑(保存详情数据,标记为已读取)
|
||||
skip = False
|
||||
else:
|
||||
# 剩余秒数大于"预约参与阈值",记录预约
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:倒计时大于阈值,但价值达到高价值阈值,记录预约",
|
||||
reason=skip_reason,
|
||||
value_yuan=prize_value_yuan,
|
||||
threshold=high_value_threshold,
|
||||
remain_seconds=(
|
||||
int(remain_seconds)
|
||||
if remain_seconds is not None
|
||||
else None
|
||||
),
|
||||
join_ahead_threshold=join_ahead_seconds,
|
||||
level="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 调用 basic_join_decision 来记录预约
|
||||
# 注意:basic_join_decision 内部已经关闭详情,返回False表示需要切换直播间
|
||||
try:
|
||||
result = douyin_fudai_core.basic_join_decision(
|
||||
self, detail, (skip, skip_reason)
|
||||
)
|
||||
if not result:
|
||||
# basic_join_decision 已经处理了记录与是否切换的逻辑,
|
||||
# 此处不再重复进行上滑切换以避免多次触发
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(记录预约)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:记录预约时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(记录预约)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
# 高价值检查异常,记录日志但继续流程
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:高价值检查异常",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果不是高价值福袋,或者不是倒计时原因,筛选不通过,直接跳过
|
||||
if skip:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时筛选不通过,直接跳过",
|
||||
skip_reason=skip_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 关闭详情页
|
||||
try:
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果允许切换直播间,筛选不通过直接切换直播间
|
||||
try:
|
||||
needswitch = bool(getattr(self, "needswitch", True))
|
||||
except Exception:
|
||||
needswitch = True
|
||||
if needswitch:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:筛选不通过后切换直播间",
|
||||
skip_reason=skip_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
# 优先使用封装好的上滑切房方法
|
||||
if hasattr(self, "swipe_up_to_next_live"):
|
||||
self.swipe_up_to_next_live()
|
||||
else:
|
||||
# 兜底:直接调用底层 swipe(基于 1080x2400 基准坐标)
|
||||
try:
|
||||
rx = int(
|
||||
getattr(
|
||||
self, "resolution_ratio_x", 1080
|
||||
)
|
||||
or 1080
|
||||
)
|
||||
ry = int(
|
||||
getattr(
|
||||
self, "resolution_ratio_y", 2400
|
||||
)
|
||||
or 2400
|
||||
)
|
||||
except Exception:
|
||||
rx, ry = 1080, 2400
|
||||
start_x = 540 * rx // 1080
|
||||
start_y = 1200 * ry // 2400
|
||||
end_x = 540 * rx // 1080
|
||||
end_y = 600 * ry // 2400
|
||||
# 直接使用 ADB 内部滑动命令
|
||||
try:
|
||||
if hasattr(self.operation, "_adb_path"):
|
||||
adb_cmd = self.operation._adb_path()
|
||||
else:
|
||||
adb_cmd = (
|
||||
os.environ.get(
|
||||
"ADB_EXECUTABLE", "adb"
|
||||
)
|
||||
or "adb"
|
||||
)
|
||||
if adb_cmd.startswith(
|
||||
'"'
|
||||
) and adb_cmd.endswith('"'):
|
||||
adb_cmd = adb_cmd[1:-1]
|
||||
cmd = [
|
||||
adb_cmd,
|
||||
"-s",
|
||||
str(self.device_id),
|
||||
"shell",
|
||||
"input",
|
||||
"swipe",
|
||||
str(int(start_x)),
|
||||
str(int(start_y)),
|
||||
str(int(end_x)),
|
||||
str(int(end_y)),
|
||||
"300",
|
||||
]
|
||||
subprocess.run(
|
||||
cmd,
|
||||
timeout=5.0,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=False,
|
||||
)
|
||||
except Exception:
|
||||
# 回退到 operation.swipe
|
||||
self.operation.swipe(
|
||||
self.device_id,
|
||||
start_x,
|
||||
start_y,
|
||||
end_x,
|
||||
end_y,
|
||||
300,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
_hd_helpers._clear_task_count_flag(self)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(筛选不通过)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
# 筛选检查异常,记录日志但继续流程
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:筛选检查异常",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 筛选通过,保存详情数据并标记为已读取
|
||||
try:
|
||||
self._last_fudai_detail_tuple = detail
|
||||
self._fudai_detail_first_read = True
|
||||
|
||||
# 设置当前福袋的内容标识(从 detail tuple 的第一个元素获取)
|
||||
if detail and isinstance(detail, tuple) and len(detail) >= 1:
|
||||
self._current_fudai_content = str(
|
||||
detail[0] if detail[0] else ""
|
||||
)
|
||||
|
||||
# 映射表已在并行OCR中记录,这里不再重复记录
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:福袋详情识别成功",
|
||||
fudai_content=(detail[0] if len(detail) > 0 else "")[:50],
|
||||
prize_value_yuan=float(detail[4] if len(detail) > 4 else 0.0),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 详情数据无效,不保存,不标记为已读取
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时详情数据无效,跳过",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 步骤2 + 步骤3:第一次打开分支已抽到 handle_first_open_branch
|
||||
if _hd_helpers.handle_first_open_branch(self, start_time):
|
||||
return
|
||||
|
||||
# 检查是否是优惠券弹窗、观看任务或识别错误:如果检测到,应该直接跳过,不继续处理
|
||||
|
||||
@@ -684,3 +684,398 @@ def _close_detail_after_attend(self: "fudai_analyse") -> None:
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# handle_first_open_branch —— 步骤2 + 步骤3 完整分支
|
||||
# ============================================================
|
||||
|
||||
def handle_first_open_branch(
|
||||
self: "fudai_analyse", start_time: float
|
||||
) -> bool:
|
||||
"""步骤2 + 步骤3:第一次打开福袋详情的完整分支。
|
||||
|
||||
流程:
|
||||
1. read_fudai_detail_with_retries 完整读取详情
|
||||
2. 数据验证 — 失败则关详情 return True
|
||||
3. should_skip_fudai_by_config 筛选检查
|
||||
- skip=True 且高价值且倒计时大于阈值:
|
||||
* 剩余秒数在预约阈值内 → skip=False,fall through 继续走立即参与
|
||||
* 剩余秒数大于预约阈值 → 调 basic_join_decision 记录预约 return True
|
||||
- skip=True 普通筛选不通过 → 关详情 + 切直播间 return True
|
||||
- skip=False 筛选通过 → 保存详情数据 + 标记已读取,fall through
|
||||
4. detail 无效(< 6 元素或 None)→ return True
|
||||
|
||||
返回 True 表示 caller 应当 `return`;False 表示继续走后续步骤
|
||||
(步骤3末尾的 last_status / 兜底 / step4)。
|
||||
"""
|
||||
import time
|
||||
|
||||
from douyin.fudai import douyin_fudai_detail
|
||||
|
||||
step2_start = time.time()
|
||||
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
|
||||
self,
|
||||
fudai_x=0,
|
||||
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
|
||||
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
|
||||
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
|
||||
)
|
||||
|
||||
# detail 无效 → 提前 return
|
||||
if not (detail and isinstance(detail, tuple) and len(detail) >= 6):
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时详情数据无效,跳过",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
|
||||
# 数据验证失败 → return
|
||||
if _validate_first_open_detail(self, detail, step2_start, start_time):
|
||||
return True
|
||||
|
||||
# 筛选检查
|
||||
branch_result = _check_skip_and_handle(
|
||||
self, detail, step2_start, start_time
|
||||
)
|
||||
if branch_result == "return":
|
||||
return True
|
||||
# branch_result == "continue" → 筛选通过或高价值阈值内立即参与,保存数据后继续
|
||||
|
||||
# 筛选通过,保存详情数据并标记为已读取
|
||||
try:
|
||||
self._last_fudai_detail_tuple = detail
|
||||
self._fudai_detail_first_read = True
|
||||
if detail and isinstance(detail, tuple) and len(detail) >= 1:
|
||||
self._current_fudai_content = str(
|
||||
detail[0] if detail[0] else ""
|
||||
)
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:福袋详情识别成功",
|
||||
fudai_content=(detail[0] if len(detail) > 0 else "")[:50],
|
||||
prize_value_yuan=float(detail[4] if len(detail) > 4 else 0.0),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def _validate_first_open_detail(
|
||||
self: "fudai_analyse", detail, step2_start: float, start_time: float
|
||||
) -> bool:
|
||||
"""数据验证。失败时关详情 + 清标志 + log,返回 True。"""
|
||||
import time
|
||||
|
||||
from douyin.fudai import douyin_fudai_detail
|
||||
|
||||
try:
|
||||
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(detail, self)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
if is_valid:
|
||||
return False
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时数据验证失败,跳过",
|
||||
validation_reason=reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
step2_elapsed = (time.time() - step2_start) * 1000
|
||||
self._log_kv(
|
||||
"详情处理",
|
||||
"步骤2[读取福袋详情]-完成",
|
||||
总耗时毫秒=step2_elapsed,
|
||||
)
|
||||
_clear_task_count_flag(self)
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(数据验证失败)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def _check_skip_and_handle(
|
||||
self: "fudai_analyse", detail, step2_start: float, start_time: float
|
||||
) -> str:
|
||||
"""筛选检查 + 高价值预约 + 筛选不通过切房。
|
||||
|
||||
返回:
|
||||
- "return" caller 应当 return(已记预约 / 切房)
|
||||
- "continue" 筛选通过或高价值阈值内立即参与,caller 继续走保存逻辑
|
||||
"""
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
|
||||
try:
|
||||
skip, skip_reason = douyin_fudai_core.should_skip_fudai_by_config(
|
||||
self, detail
|
||||
)
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:筛选检查异常",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return "continue"
|
||||
|
||||
if not skip:
|
||||
return "continue"
|
||||
|
||||
# 高价值倒计时筛选处理
|
||||
high_value_result = _try_high_value_reservation(
|
||||
self, detail, skip, skip_reason, start_time
|
||||
)
|
||||
if high_value_result == "return":
|
||||
return "return"
|
||||
if high_value_result == "continue":
|
||||
# 阈值内立即参与,回到保存路径
|
||||
return "continue"
|
||||
# "still_skip" → 走普通跳过
|
||||
return _handle_skip_filter(self, skip_reason, start_time)
|
||||
|
||||
|
||||
def _try_high_value_reservation(
|
||||
self: "fudai_analyse", detail, skip: bool, skip_reason: str, start_time: float
|
||||
) -> str:
|
||||
"""高价值倒计时筛选处理。
|
||||
|
||||
返回:
|
||||
- "return" 已记录预约,caller return
|
||||
- "continue" 阈值内立即参与,caller 走保存路径
|
||||
- "still_skip" 不是高价值或不是倒计时原因,走普通跳过
|
||||
"""
|
||||
import time
|
||||
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
|
||||
try:
|
||||
prize_value_yuan = 0.0
|
||||
countdown_tuple = None
|
||||
if isinstance(detail, tuple) and len(detail) >= 6:
|
||||
_, _, _, _, prize_value_yuan, countdown_tuple = detail
|
||||
|
||||
high_value_enabled = bool(
|
||||
getattr(self, "high_value_fudai_enabled", False)
|
||||
)
|
||||
high_value_threshold = float(
|
||||
getattr(self, "high_value_fudai_threshold_yuan", 500.0) or 500.0
|
||||
)
|
||||
|
||||
is_high_value = (
|
||||
high_value_enabled
|
||||
and prize_value_yuan > 0
|
||||
and prize_value_yuan >= high_value_threshold
|
||||
and isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 2
|
||||
and float(countdown_tuple[1] or 0.0) > 0.0
|
||||
)
|
||||
|
||||
if not (is_high_value and skip_reason.startswith("countdown_gt_")):
|
||||
return "still_skip"
|
||||
|
||||
# 计算剩余秒数
|
||||
remain_seconds = None
|
||||
if (
|
||||
isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 2
|
||||
):
|
||||
try:
|
||||
now = time.time()
|
||||
cd_start = float(countdown_tuple[0] or 0.0)
|
||||
cd_end = float(countdown_tuple[1] or 0.0)
|
||||
if cd_start > 0 and cd_end > cd_start:
|
||||
remain_seconds = float(cd_end - now)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
join_ahead_seconds = int(
|
||||
getattr(self, "high_value_fudai_join_ahead_seconds", 100) or 120
|
||||
)
|
||||
|
||||
# 阈值内立即参与
|
||||
if (
|
||||
remain_seconds is not None
|
||||
and remain_seconds <= join_ahead_seconds
|
||||
):
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:倒计时大于阈值,但剩余秒数在预约参与阈值内,立即参与",
|
||||
reason=skip_reason,
|
||||
value_yuan=prize_value_yuan,
|
||||
threshold=high_value_threshold,
|
||||
remain_seconds=int(remain_seconds),
|
||||
join_ahead_threshold=join_ahead_seconds,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return "continue"
|
||||
|
||||
# 阈值外:记录预约
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:倒计时大于阈值,但价值达到高价值阈值,记录预约",
|
||||
reason=skip_reason,
|
||||
value_yuan=prize_value_yuan,
|
||||
threshold=high_value_threshold,
|
||||
remain_seconds=(
|
||||
int(remain_seconds) if remain_seconds is not None else None
|
||||
),
|
||||
join_ahead_threshold=join_ahead_seconds,
|
||||
level="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
douyin_fudai_core.basic_join_decision(self, detail, (skip, skip_reason))
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:记录预约时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(记录预约)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return "return"
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:高价值检查异常",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return "still_skip"
|
||||
|
||||
|
||||
def _handle_skip_filter(
|
||||
self: "fudai_analyse", skip_reason: str, start_time: float
|
||||
) -> str:
|
||||
"""普通筛选不通过:关详情 + 按需切直播间。返回 "return"。"""
|
||||
import time
|
||||
|
||||
from douyin.fudai import douyin_fudai_detail
|
||||
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第一次打开时筛选不通过,直接跳过",
|
||||
skip_reason=skip_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
douyin_fudai_detail.close_fudai_detail(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
needswitch = bool(getattr(self, "needswitch", True))
|
||||
except Exception:
|
||||
needswitch = True
|
||||
|
||||
if needswitch:
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:筛选不通过后切换直播间",
|
||||
skip_reason=skip_reason,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if hasattr(self, "swipe_up_to_next_live"):
|
||||
self.swipe_up_to_next_live()
|
||||
else:
|
||||
_fallback_swipe_up(self)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_clear_task_count_flag(self)
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(筛选不通过)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
)
|
||||
return "return"
|
||||
|
||||
|
||||
def _fallback_swipe_up(self: "fudai_analyse") -> None:
|
||||
"""没有 swipe_up_to_next_live 时的兜底 swipe(基于 1080x2400 基准坐标)。"""
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080)
|
||||
ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400)
|
||||
except Exception:
|
||||
rx, ry = 1080, 2400
|
||||
start_x = 540 * rx // 1080
|
||||
start_y = 1200 * ry // 2400
|
||||
end_x = 540 * rx // 1080
|
||||
end_y = 600 * ry // 2400
|
||||
|
||||
try:
|
||||
if hasattr(self.operation, "_adb_path"):
|
||||
adb_cmd = self.operation._adb_path()
|
||||
else:
|
||||
adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb"
|
||||
if adb_cmd.startswith('"') and adb_cmd.endswith('"'):
|
||||
adb_cmd = adb_cmd[1:-1]
|
||||
cmd = [
|
||||
adb_cmd,
|
||||
"-s",
|
||||
str(self.device_id),
|
||||
"shell",
|
||||
"input",
|
||||
"swipe",
|
||||
str(int(start_x)),
|
||||
str(int(start_y)),
|
||||
str(int(end_x)),
|
||||
str(int(end_y)),
|
||||
"300",
|
||||
]
|
||||
subprocess.run(
|
||||
cmd,
|
||||
timeout=5.0,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=False,
|
||||
)
|
||||
except Exception:
|
||||
# 回退到 operation.swipe
|
||||
self.operation.swipe(
|
||||
self.device_id,
|
||||
start_x,
|
||||
start_y,
|
||||
end_x,
|
||||
end_y,
|
||||
300,
|
||||
)
|
||||
@@ -417,3 +417,182 @@ class TestCheckLastStatusBranch:
|
||||
s.swipe_up_to_next_live.assert_not_called()
|
||||
assert s._fudai_detail_consecutive_failures == 0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# handle_first_open_branch
|
||||
# ============================================================
|
||||
|
||||
class TestHandleFirstOpenBranch:
|
||||
def _mk_for_first_open(self):
|
||||
s = _mk_self()
|
||||
s.device_id = "MOCK"
|
||||
s.operation = MagicMock()
|
||||
s.operation.delay = MagicMock(return_value=None)
|
||||
s.operation.swipe = MagicMock(return_value=None)
|
||||
s._fudai_detail_task_count_from_stage = 0
|
||||
s._last_fudai_detail_tuple = None
|
||||
s._fudai_detail_first_read = False
|
||||
s._current_fudai_content = ""
|
||||
s.swipe_up_to_next_live = MagicMock(return_value=None)
|
||||
s.high_value_fudai_enabled = False
|
||||
return s
|
||||
|
||||
def test_invalid_detail_returns_true(self):
|
||||
"""read_fudai_detail_with_retries 返回 None / 短 tuple → return True 提前结束。"""
|
||||
s = self._mk_for_first_open()
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=None,
|
||||
):
|
||||
assert hd.handle_first_open_branch(s, 0.0) is True
|
||||
|
||||
def test_short_tuple_returns_true(self):
|
||||
s = self._mk_for_first_open()
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=("only_one_field",),
|
||||
):
|
||||
assert hd.handle_first_open_branch(s, 0.0) is True
|
||||
|
||||
def test_validation_failed_returns_true_closes_detail(self):
|
||||
"""validate_fudai_detail 返回 (False, reason) → 关详情 + return True。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
detail = ("X", 0, 0, 0, 0, (0, time.time() + 60, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(False, "bad_data"),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
|
||||
) as mock_close:
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is True
|
||||
mock_close.assert_called_once()
|
||||
|
||||
def test_filter_pass_saves_data_returns_false(self):
|
||||
"""筛选通过 → 保存 detail + 标记 first_read,return False 让 caller 继续。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
detail = ("有效福袋", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(True, ""),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
|
||||
return_value=(False, ""),
|
||||
):
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is False
|
||||
assert s._last_fudai_detail_tuple == detail
|
||||
assert s._fudai_detail_first_read is True
|
||||
assert s._current_fudai_content == "有效福袋"
|
||||
|
||||
def test_filter_skip_normal_closes_and_swipes(self):
|
||||
"""普通筛选不通过(非高价值)→ 关详情 + 切直播间 + return True。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
s.needswitch = True
|
||||
detail = ("X", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(True, ""),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
|
||||
return_value=(True, "value_lt_threshold"),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
|
||||
) as mock_close:
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is True
|
||||
mock_close.assert_called_once()
|
||||
s.swipe_up_to_next_live.assert_called_once()
|
||||
|
||||
def test_filter_skip_no_switch_when_needswitch_false(self):
|
||||
"""needswitch=False 筛选不通过时不切直播间。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
s.needswitch = False
|
||||
detail = ("X", 0, 0, 0, 100.0, (0, time.time() + 60, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(True, ""),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
|
||||
return_value=(True, "value_lt_threshold"),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
|
||||
):
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is True
|
||||
s.swipe_up_to_next_live.assert_not_called()
|
||||
|
||||
def test_high_value_within_threshold_continues(self):
|
||||
"""高价值倒计时阈值内 → skip=False,保存数据 return False。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
s.high_value_fudai_enabled = True
|
||||
s.high_value_fudai_threshold_yuan = 500.0
|
||||
s.high_value_fudai_join_ahead_seconds = 100
|
||||
# 倒计时只剩 60 秒(< 100),价值 1000(> 500) → 立即参与
|
||||
future_ts = time.time() + 60
|
||||
detail = ("高价值福袋", 0, 0, 0, 1000.0, (time.time(), future_ts, 60))
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(True, ""),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
|
||||
return_value=(True, "countdown_gt_30"),
|
||||
):
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is False
|
||||
assert s._fudai_detail_first_read is True
|
||||
|
||||
def test_high_value_outside_threshold_records_reservation(self):
|
||||
"""高价值倒计时阈值外 → 调 basic_join_decision 记预约 + return True。"""
|
||||
import time
|
||||
|
||||
s = self._mk_for_first_open()
|
||||
s.high_value_fudai_enabled = True
|
||||
s.high_value_fudai_threshold_yuan = 500.0
|
||||
s.high_value_fudai_join_ahead_seconds = 100
|
||||
# 倒计时还剩 600 秒(> 100) → 记预约
|
||||
future_ts = time.time() + 600
|
||||
detail = ("高价值福袋", 0, 0, 0, 1000.0, (time.time(), future_ts, 600))
|
||||
join_called = MagicMock()
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=detail,
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
|
||||
return_value=(True, ""),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
|
||||
return_value=(True, "countdown_gt_30"),
|
||||
), patch(
|
||||
"douyin.fudai.douyin_fudai_core.basic_join_decision",
|
||||
side_effect=join_called,
|
||||
):
|
||||
result = hd.handle_first_open_branch(s, 0.0)
|
||||
assert result is True
|
||||
join_called.assert_called_once()
|
||||
|
||||
Reference in new issue
Block a user