refactor(stage): 抽 diagnose_stage_from_current_screen 中"步骤1+1.5 抽奖结果/购物红包"块到独立模块
将"优先级1(抽奖结果弹窗 + 按钮 OCR 确认)"和"优先级1.5(购物红包弹窗 OCR)" 两个相邻的 priority 检测块(约 104 行)抽到 douyin_stage_priority.py: detect_lottery_or_redpacket_popup。 返回值约定: - (stage, reason) 二元组:检测到弹窗时 - None:未检测到,调用方继续后续流程 依赖通过参数注入:analyser、pixel_state、max_step_time、diag_start、 max_diag_time、log_fn/log_summary_fn/log_separator_fn、preprocess_for_ocr。 douyin_stage.py: 845 → 757 行 (-88) 新增 douyin_stage_priority.py: 137 行 douyin_stage.py 跌破 800 行(1385 → 757,本会话累计削减 628 行 = -45%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ca071250be
commit
cec4a2d692
2 files changed
+152
-103
No files matched your search
+15
-103
@@ -379,110 +379,22 @@ def diagnose_stage_from_current_screen(analyser, trigger_reason="unknown"): # t
|
||||
# 根据像素检测结果,按优先级返回阶段
|
||||
|
||||
# 优先级1:抽奖结果弹窗(像素检测 + 按钮OCR确认)
|
||||
if pixel_state.get("has_lucky_draw_result", False):
|
||||
step1_start = time.time()
|
||||
_log_separator("步骤1:检测抽奖结果弹窗(简化)")
|
||||
try:
|
||||
from douyin.utils.douyin_helpers import HelperFunctions
|
||||
# 优先级1+1.5:抽奖结果弹窗 + 购物红包弹窗 OCR 检测
|
||||
from douyin.stage.douyin_stage_priority import detect_lottery_or_redpacket_popup
|
||||
|
||||
detected = HelperFunctions.check_lottery_result_by_ocr(analyser)
|
||||
if detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(抽奖结果弹窗)",
|
||||
阶段="FUDai_RESULT",
|
||||
原因="像素检测+按钮OCR确认",
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
_log_separator("最终结果:FUDai_RESULT(抽奖结果弹窗)")
|
||||
return "FUDai_RESULT", "像素检测+按钮OCR确认"
|
||||
else:
|
||||
_log("步骤1:像素检测到弹窗但未识别出按钮,继续后续检测")
|
||||
except Exception as e:
|
||||
_log(
|
||||
"步骤1[检测抽奖结果弹窗]-简化OCR异常",
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
# 步骤1结束计时
|
||||
step1_elapsed = (time.time() - step1_start) * 1000
|
||||
_log("步骤1[检测抽奖结果弹窗]-完成", 总耗时毫秒=step1_elapsed)
|
||||
|
||||
# 优先级1.5:购物红包弹窗检测(保持现有检测方式)
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
_log_separator("步骤1.5:检测购物红包弹窗")
|
||||
step_redpacket_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
_log(
|
||||
"步骤1.5[检测购物红包弹窗]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="像素检测到购物红包背景",
|
||||
)
|
||||
|
||||
redpacket_detected = False
|
||||
try:
|
||||
if time.time() - step_redpacket_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
_log("步骤1.5[检测购物红包弹窗]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_redpacket_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp = preprocess_for_ocr(analyser, "診斷購物紅包")
|
||||
redpacket_text = str(
|
||||
analyser.operation.analyse_pic_word(name_rp, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
_log(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=redpacket_text[:50] if redpacket_text else "",
|
||||
)
|
||||
|
||||
# 检查是否包含购物红包关键词
|
||||
redpacket_keywords = ("购物红包", "邀你抢", "抢红包")
|
||||
if redpacket_text and any(
|
||||
kw in redpacket_text for kw in redpacket_keywords
|
||||
):
|
||||
redpacket_detected = True
|
||||
else:
|
||||
# OCR未识别到关键词,不判定为购物红包弹窗(以OCR为准)
|
||||
_log(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR未识别到关键词",
|
||||
说明="OCR未匹配,不判定为购物红包弹窗",
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
_log(
|
||||
"步骤1.5[检测购物红包弹窗]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
# OCR异常时,不判定为购物红包弹窗(以OCR为准)
|
||||
_log(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR异常,不判定为购物红包弹窗",
|
||||
说明="需要OCR确认才能判定",
|
||||
)
|
||||
|
||||
if redpacket_detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
_log_summary(
|
||||
"阶段诊断完成(购物红包弹窗)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="检测到购物红包弹窗",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(购物红包弹窗)"
|
||||
|
||||
# 步骤1.5结束计时
|
||||
step15_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
_log("步骤1.5[检测购物红包弹窗]-完成", 总耗时毫秒=step15_elapsed)
|
||||
lottery_or_redpacket_result = detect_lottery_or_redpacket_popup(
|
||||
analyser,
|
||||
pixel_state,
|
||||
max_step_time,
|
||||
diag_start,
|
||||
max_diag_time,
|
||||
_log,
|
||||
_log_summary,
|
||||
_log_separator,
|
||||
preprocess_for_ocr,
|
||||
)
|
||||
if lottery_or_redpacket_result is not None:
|
||||
return lottery_or_redpacket_result
|
||||
|
||||
# ========== 步骤1:像素检测福袋详情页(从并行检测结果获取)==========
|
||||
step1_fudai_start = time.time()
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
"""阶段诊断辅助:步骤 1(抽奖结果弹窗)+ 步骤 1.5(购物红包弹窗)
|
||||
|
||||
从 douyin/stage/douyin_stage.py:diagnose_stage_from_current_screen 中提取的
|
||||
两个相邻的 priority 检测块。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
from douyin.fudai.core.coords import (
|
||||
SHOPPING_REDPACKET_AREA,
|
||||
)
|
||||
|
||||
|
||||
def detect_lottery_or_redpacket_popup(
|
||||
analyser,
|
||||
pixel_state: dict,
|
||||
max_step_time: float,
|
||||
diag_start: float,
|
||||
max_diag_time: float,
|
||||
log_fn,
|
||||
log_summary_fn,
|
||||
log_separator_fn,
|
||||
preprocess_for_ocr,
|
||||
):
|
||||
"""步骤 1(抽奖结果弹窗 OCR 确认)+ 步骤 1.5(购物红包弹窗 OCR 检测)。
|
||||
|
||||
返回:
|
||||
(stage, reason) 二元组:检测到弹窗时
|
||||
None:未检测到,调用方继续后续流程
|
||||
"""
|
||||
if pixel_state.get("has_lucky_draw_result", False):
|
||||
step1_start = time.time()
|
||||
log_separator_fn("步骤1:检测抽奖结果弹窗(简化)")
|
||||
try:
|
||||
from douyin.utils.douyin_helpers import HelperFunctions
|
||||
|
||||
detected = HelperFunctions.check_lottery_result_by_ocr(analyser)
|
||||
if detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(抽奖结果弹窗)",
|
||||
阶段="FUDai_RESULT",
|
||||
原因="像素检测+按钮OCR确认",
|
||||
总耗时毫秒=total_elapsed,
|
||||
)
|
||||
log_separator_fn("最终结果:FUDai_RESULT(抽奖结果弹窗)")
|
||||
return "FUDai_RESULT", "像素检测+按钮OCR确认"
|
||||
else:
|
||||
log_fn("步骤1:像素检测到弹窗但未识别出按钮,继续后续检测")
|
||||
except Exception as e:
|
||||
log_fn(
|
||||
"步骤1[检测抽奖结果弹窗]-简化OCR异常",
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
|
||||
# 步骤1结束计时
|
||||
step1_elapsed = (time.time() - step1_start) * 1000
|
||||
log_fn("步骤1[检测抽奖结果弹窗]-完成", 总耗时毫秒=step1_elapsed)
|
||||
|
||||
# 优先级1.5:购物红包弹窗检测(保持现有检测方式)
|
||||
if pixel_state.get("has_redpacket_bg", False):
|
||||
log_separator_fn("步骤1.5:检测购物红包弹窗")
|
||||
step_redpacket_start = time.time()
|
||||
remaining_time_before = max_diag_time - (time.time() - diag_start)
|
||||
log_fn(
|
||||
"步骤1.5[检测购物红包弹窗]-开始",
|
||||
剩余时间秒=remaining_time_before,
|
||||
说明="像素检测到购物红包背景",
|
||||
)
|
||||
|
||||
redpacket_detected = False
|
||||
try:
|
||||
if time.time() - step_redpacket_start < max_step_time:
|
||||
cut_start = time.time()
|
||||
analyser.cut_pic(*SHOPPING_REDPACKET_AREA, "", "診斷購物紅包")
|
||||
cut_elapsed = (time.time() - cut_start) * 1000
|
||||
log_fn("步骤1.5[检测购物红包弹窗]-截图完成", 耗时毫秒=cut_elapsed)
|
||||
|
||||
if time.time() - step_redpacket_start < max_step_time:
|
||||
ocr_start = time.time()
|
||||
name_rp = preprocess_for_ocr(analyser, "診斷購物紅包")
|
||||
redpacket_text = str(
|
||||
analyser.operation.analyse_pic_word(name_rp, 0) or ""
|
||||
)
|
||||
ocr_elapsed = (time.time() - ocr_start) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR模式0",
|
||||
耗时毫秒=ocr_elapsed,
|
||||
文本长度=len(redpacket_text),
|
||||
文本预览=redpacket_text[:50] if redpacket_text else "",
|
||||
)
|
||||
|
||||
# 检查是否包含购物红包关键词
|
||||
redpacket_keywords = ("购物红包", "邀你抢", "抢红包")
|
||||
if redpacket_text and any(
|
||||
kw in redpacket_text for kw in redpacket_keywords
|
||||
):
|
||||
redpacket_detected = True
|
||||
else:
|
||||
# OCR未识别到关键词,不判定为购物红包弹窗(以OCR为准)
|
||||
log_fn(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR未识别到关键词",
|
||||
说明="OCR未匹配,不判定为购物红包弹窗",
|
||||
)
|
||||
except Exception as e:
|
||||
redpacket_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
log_fn(
|
||||
"步骤1.5[检测购物红包弹窗]-异常",
|
||||
耗时毫秒=redpacket_elapsed,
|
||||
异常类型=type(e).__name__,
|
||||
异常信息=str(e),
|
||||
)
|
||||
# OCR异常时,不判定为购物红包弹窗(以OCR为准)
|
||||
log_fn(
|
||||
"步骤1.5[检测购物红包弹窗]-OCR异常,不判定为购物红包弹窗",
|
||||
说明="需要OCR确认才能判定",
|
||||
)
|
||||
|
||||
if redpacket_detected:
|
||||
total_elapsed = (time.time() - diag_start) * 1000
|
||||
redpacket_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
log_summary_fn(
|
||||
"阶段诊断完成(购物红包弹窗)",
|
||||
阶段="SHOPPING_REDPACKET_POPUP",
|
||||
原因="检测到购物红包弹窗",
|
||||
总耗时毫秒=total_elapsed,
|
||||
红包检测耗时毫秒=redpacket_elapsed,
|
||||
)
|
||||
return "SHOPPING_REDPACKET_POPUP", "像素检测+OCR确认(购物红包弹窗)"
|
||||
|
||||
# 步骤1.5结束计时
|
||||
step15_elapsed = (time.time() - step_redpacket_start) * 1000
|
||||
log_fn("步骤1.5[检测购物红包弹窗]-完成", 总耗时毫秒=step15_elapsed)
|
||||
|
||||
return None
|
||||
Reference in new issue
Block a user