refactor(fudai): 拆出"第二次打开福袋详情"分支(2318→2185,-133 行)
把 _handle_in_fudai_detail 中"is_second_open"分支(156 行)抽到 fudai_detail_blocks.handle_second_open_attend: - 若 _fudai_detail_first_read=True 且有保存的详情数据:直接调用 attend_choujiang - 参与成功重置计数器;失败累加;attend_result 写入完成日志 - 异常时返回 False 让主流程落到第一次打开路径 新增 5 个单元测试覆盖:非二次打开 / 无详情 fallthrough / 有效详情参与成功 / 参与失败计数 / attend 异常 fallthrough。1152 → 1157 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
03e509ac25
commit
25c7a0440d
3 files changed
+235
-138
No files matched your search
+5
-138
@@ -1569,30 +1569,15 @@ class fudai_analyse:
|
||||
|
||||
# 步骤1:检查是否是第二次打开福袋详情
|
||||
step1_start = time.time()
|
||||
is_second_open = getattr(self, "_fudai_detail_first_read", False)
|
||||
# 如果是第二次打开,直接点击参与,不需要验证,使用第一次的倒计时
|
||||
if fudai_detail_blocks.handle_second_open_attend(self, start_time, step1_start):
|
||||
return
|
||||
|
||||
is_second_open = getattr(self, "_fudai_detail_first_read", False)
|
||||
if is_second_open:
|
||||
# 第二次打开:直接点击参与,不进行验证,使用第一次的倒计时
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第二次打开,直接点击参与,跳过验证和内容识别",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 获取第一次读取的详情数据
|
||||
# 第二次打开但 handle_second_open_attend 返回 False(无详情数据需要重读,
|
||||
# 或参与函数抛错),落入第一次打开路径继续处理
|
||||
detail = getattr(self, "_last_fudai_detail_tuple", None)
|
||||
if detail is None or not isinstance(detail, tuple) or len(detail) < 6:
|
||||
# 如果没有保存的详情数据,需要重新读取(兜底逻辑)
|
||||
try:
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:未找到第一次读取的详情数据,重新读取",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
|
||||
self,
|
||||
fudai_x=0,
|
||||
@@ -1600,130 +1585,12 @@ class fudai_analyse:
|
||||
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:
|
||||
self._last_fudai_detail_tuple = detail
|
||||
# 设置当前福袋的 content(使用 cast 帮助静态分析)
|
||||
detail_t: Tuple[Any, ...] = tuple(detail)
|
||||
self._current_fudai_content = (
|
||||
detail_t[0] if len(detail_t) > 0 else ""
|
||||
)
|
||||
else:
|
||||
# 使用第一次保存的详情数据,设置当前福袋的倒计时信息
|
||||
try:
|
||||
# 从保存的 tuple 中安全读取倒计时信息,使用 cast 明确类型以帮助静态分析
|
||||
detail = getattr(self, "_last_fudai_detail_tuple", None)
|
||||
if (
|
||||
detail is not None
|
||||
and isinstance(detail, tuple)
|
||||
and len(detail) >= 6
|
||||
):
|
||||
# 使用 cast 明确 detail 的类型,避免静态分析报 unsubscriptable
|
||||
detail_t: Tuple[Any, ...] = tuple(detail)
|
||||
# detail 已校验为 tuple 且长度足够,可直接索引读取倒计时信息
|
||||
countdown_tuple = detail_t[5]
|
||||
if (
|
||||
isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 3 # 现在需要至少3个元素
|
||||
):
|
||||
future_ts = float(countdown_tuple[1] or 0.0)
|
||||
original_seconds = countdown_tuple[2] # 原始秒数
|
||||
if future_ts > 0:
|
||||
# 设置当前福袋的 future_ts 和 content,供 attend_choujiang 使用
|
||||
self._current_fudai_future_ts = future_ts
|
||||
self._current_fudai_content = (
|
||||
detail_t[0] if len(detail_t) > 0 else ""
|
||||
)
|
||||
# 优先使用原始秒数计算剩余时间,避免时间差误差
|
||||
remain_seconds = (
|
||||
max(0, int(original_seconds or 0))
|
||||
if original_seconds is not None
|
||||
else int(future_ts - time.time())
|
||||
)
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"第二次打开:使用第一次的倒计时信息",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=remain_seconds,
|
||||
original_seconds=(
|
||||
int(original_seconds or 0)
|
||||
if original_seconds is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
except Exception as e:
|
||||
try:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"第二次打开:设置倒计时信息时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 直接调用参与函数,不进行验证
|
||||
try:
|
||||
attend_result = douyin_fudai_core.attend_choujiang(self)
|
||||
|
||||
if attend_result:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"第二次打开:直接点击参与成功",
|
||||
result="参与成功",
|
||||
)
|
||||
# 参与成功,重置连续失败计数器
|
||||
self._consecutive_attend_failures = 0
|
||||
else:
|
||||
self._log_kv(
|
||||
"参与",
|
||||
"第二次打开:直接点击参与失败",
|
||||
result="参与失败",
|
||||
)
|
||||
# 参与失败,增加连续失败计数器
|
||||
self._consecutive_attend_failures = (
|
||||
getattr(self, "_consecutive_attend_failures", 0) + 1
|
||||
)
|
||||
|
||||
# 清除第一次读取标志,下次打开新福袋时重新识别
|
||||
try:
|
||||
self._fudai_detail_first_read = False
|
||||
self._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 清除任务数量标志
|
||||
try:
|
||||
self._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
self._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 步骤1结束计时
|
||||
step1_elapsed = (time.time() - step1_start) * 1000
|
||||
self._log_kv(
|
||||
"详情处理",
|
||||
"步骤1[检查是否第二次打开]-完成",
|
||||
总耗时毫秒=step1_elapsed,
|
||||
)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
self._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(第二次打开直接参与)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
attend_result=attend_result,
|
||||
)
|
||||
return
|
||||
except Exception as e:
|
||||
self._log_kv("参与", "第二次打开:直接点击参与异常", error=str(e))
|
||||
# 异常时继续正常流程
|
||||
else:
|
||||
# 步骤2:读取福袋详情
|
||||
step2_start = time.time()
|
||||
|
||||
@@ -10,6 +10,157 @@ import time
|
||||
from typing import Any
|
||||
|
||||
|
||||
def handle_second_open_attend(analyser: Any, start_time: float, step1_start: float) -> bool:
|
||||
"""处理"第二次打开福袋详情"分支:直接点击参与,跳过验证和内容识别。
|
||||
|
||||
返回 True 表示触发了第二次打开分支且主流程应 return。
|
||||
返回 False 表示未触发(不是第二次打开,主流程继续走第一次打开路径)。
|
||||
"""
|
||||
is_second_open = getattr(analyser, "_fudai_detail_first_read", False)
|
||||
if not is_second_open:
|
||||
return False
|
||||
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:第二次打开,直接点击参与,跳过验证和内容识别",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
from douyin.fudai import douyin_fudai_detail
|
||||
from douyin.fudai import douyin_fudai_core
|
||||
|
||||
# 获取第一次读取的详情数据
|
||||
detail = getattr(analyser, "_last_fudai_detail_tuple", None)
|
||||
if detail is None or not isinstance(detail, tuple) or len(detail) < 6:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail:未找到第一次读取的详情数据,重新读取",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
|
||||
analyser,
|
||||
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:
|
||||
analyser._last_fudai_detail_tuple = detail
|
||||
detail_t = tuple(detail)
|
||||
analyser._current_fudai_content = (
|
||||
detail_t[0] if len(detail_t) > 0 else ""
|
||||
)
|
||||
# 兜底重读后没有走点击参与流程;返回 False 让主流程继续走第一次打开路径
|
||||
return False
|
||||
|
||||
# 使用第一次保存的详情数据,设置当前福袋的倒计时信息
|
||||
try:
|
||||
detail_t = tuple(detail)
|
||||
countdown_tuple = detail_t[5]
|
||||
if (
|
||||
isinstance(countdown_tuple, (tuple, list))
|
||||
and len(countdown_tuple) >= 3
|
||||
):
|
||||
future_ts = float(countdown_tuple[1] or 0.0)
|
||||
original_seconds = countdown_tuple[2]
|
||||
if future_ts > 0:
|
||||
analyser._current_fudai_future_ts = future_ts
|
||||
analyser._current_fudai_content = (
|
||||
detail_t[0] if len(detail_t) > 0 else ""
|
||||
)
|
||||
remain_seconds = (
|
||||
max(0, int(original_seconds or 0))
|
||||
if original_seconds is not None
|
||||
else int(future_ts - time.time())
|
||||
)
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"第二次打开:使用第一次的倒计时信息",
|
||||
future_ts=int(future_ts),
|
||||
remain_seconds=remain_seconds,
|
||||
original_seconds=(
|
||||
int(original_seconds or 0)
|
||||
if original_seconds is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
except Exception as e:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"第二次打开:设置倒计时信息时出错",
|
||||
error=str(e),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 直接调用参与函数,不进行验证
|
||||
try:
|
||||
attend_result = douyin_fudai_core.attend_choujiang(analyser)
|
||||
if attend_result:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"第二次打开:直接点击参与成功",
|
||||
result="参与成功",
|
||||
)
|
||||
analyser._consecutive_attend_failures = 0
|
||||
else:
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"第二次打开:直接点击参与失败",
|
||||
result="参与失败",
|
||||
)
|
||||
analyser._consecutive_attend_failures = (
|
||||
getattr(analyser, "_consecutive_attend_failures", 0) + 1
|
||||
)
|
||||
|
||||
try:
|
||||
analyser._fudai_detail_first_read = False
|
||||
analyser._last_fudai_detail_tuple = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
analyser._fudai_detail_task_count_from_stage = None
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋详情",
|
||||
"_handle_in_fudai_detail:清除任务数量标志",
|
||||
log_type="DEBUG",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
step1_elapsed = (time.time() - step1_start) * 1000
|
||||
analyser._log_kv(
|
||||
"详情处理",
|
||||
"步骤1[检查是否第二次打开]-完成",
|
||||
总耗时毫秒=step1_elapsed,
|
||||
)
|
||||
|
||||
elapsed = time.time() - start_time
|
||||
analyser._log_kv(
|
||||
"流程",
|
||||
"_handle_in_fudai_detail完成(第二次打开直接参与)",
|
||||
elapsed_seconds=round(elapsed, 2),
|
||||
attend_result=attend_result,
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
try:
|
||||
analyser._log_kv("参与", "第二次打开:直接点击参与异常", error=str(e))
|
||||
except Exception:
|
||||
pass
|
||||
# 异常时让主流程继续走第一次打开路径
|
||||
return False
|
||||
|
||||
|
||||
def handle_too_many_consecutive_failures(
|
||||
analyser: Any, max_consecutive_failures: int = 5
|
||||
) -> bool:
|
||||
|
||||
@@ -396,3 +396,82 @@ def test_skip_search_invalid_ft_returns_false():
|
||||
a.queue_full_waiting_at_earliest = True
|
||||
a.reservations = [{"future_ts": 0}] # 无效
|
||||
assert fudai_detail_blocks.should_skip_search_in_lock_period(a, now) is False
|
||||
|
||||
|
||||
# ---- handle_second_open_attend ---- #
|
||||
|
||||
def test_second_open_not_first_read_returns_false():
|
||||
a = MagicMock()
|
||||
a._fudai_detail_first_read = False
|
||||
assert fudai_detail_blocks.handle_second_open_attend(a, _time.time(), _time.time()) is False
|
||||
|
||||
|
||||
def test_second_open_no_saved_detail_returns_false_for_fallthrough():
|
||||
"""没有保存的详情数据 → 函数尝试重新读取后返回 False,让主流程走第一次打开路径"""
|
||||
a = MagicMock()
|
||||
a._fudai_detail_first_read = True
|
||||
a._last_fudai_detail_tuple = None
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
|
||||
return_value=None,
|
||||
):
|
||||
result = fudai_detail_blocks.handle_second_open_attend(a, _time.time(), _time.time())
|
||||
assert result is False
|
||||
|
||||
|
||||
def test_second_open_with_valid_detail_calls_attend():
|
||||
"""有有效详情 → 调用 attend_choujiang 后 return True"""
|
||||
a = MagicMock()
|
||||
a._fudai_detail_first_read = True
|
||||
a._last_fudai_detail_tuple = (
|
||||
"content",
|
||||
"x",
|
||||
"x",
|
||||
"x",
|
||||
5.0,
|
||||
("start_ts", _time.time() + 100, 100), # countdown_tuple: 至少 3 个元素
|
||||
)
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang", return_value=True
|
||||
) as fake_attend:
|
||||
result = fudai_detail_blocks.handle_second_open_attend(
|
||||
a, _time.time(), _time.time()
|
||||
)
|
||||
assert result is True
|
||||
fake_attend.assert_called_once_with(a)
|
||||
assert a._consecutive_attend_failures == 0
|
||||
assert a._fudai_detail_first_read is False
|
||||
|
||||
|
||||
def test_second_open_attend_failure_increments_counter():
|
||||
a = MagicMock()
|
||||
a._fudai_detail_first_read = True
|
||||
a._last_fudai_detail_tuple = (
|
||||
"content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100),
|
||||
)
|
||||
a._consecutive_attend_failures = 2
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang", return_value=False
|
||||
):
|
||||
result = fudai_detail_blocks.handle_second_open_attend(
|
||||
a, _time.time(), _time.time()
|
||||
)
|
||||
assert result is True
|
||||
assert a._consecutive_attend_failures == 3
|
||||
|
||||
|
||||
def test_second_open_attend_exception_returns_false_for_fallthrough():
|
||||
"""attend_choujiang 抛错 → 返回 False 让主流程继续"""
|
||||
a = MagicMock()
|
||||
a._fudai_detail_first_read = True
|
||||
a._last_fudai_detail_tuple = (
|
||||
"content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100),
|
||||
)
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core.attend_choujiang",
|
||||
side_effect=RuntimeError("boom"),
|
||||
):
|
||||
result = fudai_detail_blocks.handle_second_open_attend(
|
||||
a, _time.time(), _time.time()
|
||||
)
|
||||
assert result is False
|
||||
Reference in new issue
Block a user