refactor(fudai): 抽 _handle_in_fudai_detail 步骤1"第二次打开直接参与"块到 handle_second_open_direct_attend

将 116 行的"第二次打开使用保存详情直接参与"块抽到 helper:
- 从 _last_fudai_detail_tuple 读取保存的详情
- 设置 _current_fudai_future_ts / _current_fudai_content
- 调用 attend_choujiang 直接参与
- 更新连续失败计数器(成功重置/失败 +1)
- 清除标志 + 写步骤1完成日志

返回值约定:
- True:已完成第二次打开的直接参与处理(调用方应直接 return)
- False:保存的详情数据无效或参与异常,调用方继续兜底逻辑

douyin_fudai.py: 1921 → 1821 行 (-100)
fudai_detail_helpers.py: 1930 → 2047 行

douyin_fudai.py 跌破 1900 行(4121 → 1821,本会话累计削减 2300 行 = -56%)

验证:
- pytest 1013 passed
- compile OK

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 12:07:55 +08:00
1 parent 887f8bad3e
commit d109e94c68
2 files changed
+123 -106

No files matched your search

+6 -106
View File
@@ -1267,114 +1267,14 @@ class fudai_analyse:
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
# 使用第一次保存的详情数据:直接参与(不进行验证)
from douyin.core.fudai_modules.fudai_detail_helpers import (
handle_second_open_direct_attend,
)
# 直接调用参与函数,不进行验证
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
# 清除任务数量标志
from douyin.core.fudai_modules.fudai_detail_helpers import (
clear_task_count_flag,
)
clear_task_count_flag(self)
# 步骤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,
)
if handle_second_open_direct_attend(self, start_time, step1_start):
return
except Exception as e:
self._log_kv("参与", "第二次打开:直接点击参与异常", error=str(e))
# 异常时继续正常流程
# 异常时继续正常流程
else:
# 步骤2:读取福袋详情
step2_start = time.time()
@@ -42,6 +42,123 @@ def log_screenshot_failed(
pass
def handle_second_open_direct_attend(
analyser, start_time: float, step1_start: float
) -> bool:
"""_handle_in_fudai_detail 步骤1:第二次打开时使用保存的详情数据直接参与。
流程:
1. 从 _last_fudai_detail_tuple 读取保存的详情
2. 设置 _current_fudai_future_ts 和 _current_fudai_content
3. 调用 attend_choujiang 直接参与
4. 更新连续失败计数器(成功重置/失败 +1)
5. 清除标志 + 写步骤1完成日志
返回:
True:已完成第二次打开的直接参与处理(调用方应直接 return)
False:保存的详情数据无效或参与异常,调用方继续兜底逻辑
"""
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
):
return False
# 设置当前福袋的倒计时信息
try:
countdown_tuple = detail[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[0] if len(detail) > 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
clear_task_count_flag(analyser)
# 步骤1结束计时
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:
analyser._log_kv("参与", "第二次打开:直接点击参与异常", error=str(e))
return False
def handle_filter_skip(analyser, skip_reason: str, start_time: float) -> None:
"""_handle_in_fudai_detail 筛选不通过分支:关闭详情 + 按 needswitch 切换直播间。