refactor(fudai_core): 抽 6 个 fallback OCR 闭包到 OcrTaskRunnerFallback (1350 → 1194)
主并行 OCR 失败时走的备用路径里有 6 个 ocr_*_fallback 闭包,结构与 OcrTaskRunner 类似但更简单(不计耗时、countdown 不做数字提取、日志 tag 改为"备用OCR..."),共 156 行。 复用 ocr_tasks.py 模块新增 OcrTaskRunnerFallback 类,与 OcrTaskRunner 共享 _safe_preprocess / _BUTTON_KEYWORDS / _read_disable_flag。 主流程 6 个闭包定义换成 14 行 runner 实例化 + 方法绑定。 新增 10 个单元测试覆盖 Fallback 的所有方法(disable 跳过 / 异常 / countdown 不提取数字 / button 关键词短路 + retry)。 1304 passed (+10)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f38b373636
commit
2f0eb90d98
3 files changed
+214
-157
No files matched your search
@@ -546,163 +546,20 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
# 使用线程池并行执行OCR(包括任务列表OCR,与其他OCR一起并行执行)
|
||||
try:
|
||||
|
||||
def ocr_task_list_fallback():
|
||||
try:
|
||||
# 同样尊重开关
|
||||
try:
|
||||
disable_task_recognition = bool(
|
||||
getattr(analyser, "fudai_disable_task_recognition", False)
|
||||
)
|
||||
except Exception:
|
||||
disable_task_recognition = False
|
||||
if not disable_task_recognition:
|
||||
try:
|
||||
import json
|
||||
|
||||
cfg_path = path_config.get_config_path()
|
||||
if cfg_path and os.path.exists(cfg_path):
|
||||
with open(cfg_path, "r", encoding="utf-8") as f:
|
||||
cfg = json.load(f)
|
||||
disable_task_recognition = bool(
|
||||
cfg.get("fudai_disable_task_recognition", False)
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if disable_task_recognition:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋",
|
||||
"跳过备用福袋任务列表OCR识别(配置启用)",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
name_tl2 = preprocess_for_ocr(analyser, "福袋任务列表")
|
||||
return analyser.operation.analyse_pic_word(name_tl2, 0)
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别任务列表失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def ocr_content_fallback():
|
||||
if disable_content_recognition:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"福袋",
|
||||
"跳过备用福袋内容OCR识别(配置启用)",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
try:
|
||||
return analyser.operation.analyse_pic_word("福袋内容", 0)
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别福袋内容失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def ocr_countdown_mm_fallback():
|
||||
try:
|
||||
return analyser.operation.analyse_pic_digits("福袋倒计时_分钟", 0)
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别倒计时分钟失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def ocr_countdown_ss_fallback():
|
||||
try:
|
||||
return analyser.operation.analyse_pic_digits("福袋倒计时_秒", 0)
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别倒计时秒失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def ocr_value_fallback():
|
||||
try:
|
||||
return analyser.operation.analyse_pic_word("福袋价值", 0)
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别福袋价值失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def ocr_button_fallback():
|
||||
try:
|
||||
name_btn = preprocess_for_ocr(analyser, "参与按钮")
|
||||
button_text = str(
|
||||
analyser.operation.analyse_pic_word(name_btn, 0) or ""
|
||||
)
|
||||
if not button_text or not any(
|
||||
k in button_text
|
||||
for k in (
|
||||
"参与成功",
|
||||
"还需看播",
|
||||
"评论",
|
||||
"参与抽奖",
|
||||
"开始观看",
|
||||
"参与",
|
||||
"加入粉丝团",
|
||||
"粉丝团",
|
||||
"等待开奖",
|
||||
"未达成",
|
||||
)
|
||||
):
|
||||
button_text = str(
|
||||
analyser.operation.analyse_pic_word(name_btn, 0) or ""
|
||||
)
|
||||
return button_text
|
||||
except Exception as e_inner2:
|
||||
try:
|
||||
analyser._log_kv(
|
||||
"错误",
|
||||
"备用OCR识别参与按钮失败",
|
||||
error=str(e_inner2),
|
||||
error_type=type(e_inner2).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
# 6 个备用并行 OCR 子任务封装到 OcrTaskRunnerFallback
|
||||
from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import (
|
||||
OcrTaskRunnerFallback,
|
||||
)
|
||||
_fb_runner = OcrTaskRunnerFallback(
|
||||
analyser,
|
||||
disable_content_recognition=disable_content_recognition,
|
||||
)
|
||||
ocr_task_list_fallback = _fb_runner.ocr_task_list
|
||||
ocr_content_fallback = _fb_runner.ocr_content
|
||||
ocr_countdown_mm_fallback = _fb_runner.ocr_countdown_mm
|
||||
ocr_countdown_ss_fallback = _fb_runner.ocr_countdown_ss
|
||||
ocr_value_fallback = _fb_runner.ocr_value
|
||||
ocr_button_fallback = _fb_runner.ocr_button
|
||||
|
||||
# 使用动态线程池大小,并复用线程池
|
||||
from operations.screenshot_service import get_optimal_workers
|
||||
|
||||
@@ -224,3 +224,117 @@ class OcrTaskRunner:
|
||||
return "", ""
|
||||
finally:
|
||||
self._record_duration("ocr_live_room_title", start)
|
||||
|
||||
|
||||
class OcrTaskRunnerFallback:
|
||||
"""主并行 OCR 失败时的备用任务集。
|
||||
|
||||
与 OcrTaskRunner 区别:
|
||||
- 不记录 perf_durations(备用路径调用频率低,无性能追踪需求)
|
||||
- countdown_mm / countdown_ss 直接返回原始 OCR 结果(不做数字提取)
|
||||
- 日志里把 tag 改为"备用OCR..."
|
||||
|
||||
Args:
|
||||
analyser: fudai_analyse 实例
|
||||
disable_content_recognition: 主流程已读到的内容 OCR 开关
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
analyser,
|
||||
*,
|
||||
disable_content_recognition: bool = False,
|
||||
):
|
||||
self.analyser = analyser
|
||||
self.disable_content_recognition = bool(disable_content_recognition)
|
||||
|
||||
def _log_warn(self, msg: str, exc: BaseException) -> None:
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"错误",
|
||||
msg,
|
||||
error=str(exc),
|
||||
error_type=type(exc).__name__,
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def ocr_task_list(self) -> Optional[str]:
|
||||
try:
|
||||
from douyin.fudai.douyin_fudai_core_pkg.heavy_ocr import (
|
||||
_read_disable_flag,
|
||||
)
|
||||
disable = _read_disable_flag(
|
||||
self.analyser,
|
||||
"fudai_disable_task_recognition",
|
||||
"fudai_disable_task_recognition",
|
||||
)
|
||||
if disable:
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"福袋", "跳过备用福袋任务列表OCR识别(配置启用)"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
name_tl = _safe_preprocess(self.analyser, "福袋任务列表")
|
||||
return self.analyser.operation.analyse_pic_word(name_tl, 0)
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别任务列表失败", e)
|
||||
return None
|
||||
|
||||
def ocr_content(self) -> Optional[str]:
|
||||
if self.disable_content_recognition:
|
||||
try:
|
||||
self.analyser._log_kv(
|
||||
"福袋", "跳过备用福袋内容OCR识别(配置启用)"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
try:
|
||||
return self.analyser.operation.analyse_pic_word("福袋内容", 0)
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别福袋内容失败", e)
|
||||
return None
|
||||
|
||||
def ocr_countdown_mm(self):
|
||||
try:
|
||||
return self.analyser.operation.analyse_pic_digits("福袋倒计时_分钟", 0)
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别倒计时分钟失败", e)
|
||||
return None
|
||||
|
||||
def ocr_countdown_ss(self):
|
||||
try:
|
||||
return self.analyser.operation.analyse_pic_digits("福袋倒计时_秒", 0)
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别倒计时秒失败", e)
|
||||
return None
|
||||
|
||||
def ocr_value(self):
|
||||
try:
|
||||
return self.analyser.operation.analyse_pic_word("福袋价值", 0)
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别福袋价值失败", e)
|
||||
return None
|
||||
|
||||
def ocr_button(self) -> Optional[str]:
|
||||
try:
|
||||
name_btn = _safe_preprocess(self.analyser, "参与按钮")
|
||||
button_text = str(
|
||||
self.analyser.operation.analyse_pic_word(name_btn, 0) or ""
|
||||
)
|
||||
if not button_text or not any(
|
||||
k in button_text for k in _BUTTON_KEYWORDS
|
||||
):
|
||||
button_text = str(
|
||||
self.analyser.operation.analyse_pic_word(name_btn, 0) or ""
|
||||
)
|
||||
return button_text
|
||||
except Exception as e:
|
||||
self._log_warn("备用OCR识别参与按钮失败", e)
|
||||
return None
|
||||
|
||||
@@ -17,6 +17,7 @@ import pytest
|
||||
|
||||
from douyin.fudai.douyin_fudai_core_pkg.ocr_tasks import (
|
||||
OcrTaskRunner,
|
||||
OcrTaskRunnerFallback,
|
||||
_BUTTON_KEYWORDS,
|
||||
_safe_preprocess,
|
||||
)
|
||||
@@ -295,3 +296,88 @@ class TestPerfDurations:
|
||||
):
|
||||
assert k in durations, f"missing duration for {k}"
|
||||
assert durations[k] >= 0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# OcrTaskRunnerFallback
|
||||
# ============================================================
|
||||
|
||||
class TestOcrTaskRunnerFallback:
|
||||
def test_task_list_disabled_skips(self):
|
||||
a = _mk_analyser()
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||||
return_value=True,
|
||||
):
|
||||
assert runner.ocr_task_list() is None
|
||||
|
||||
def test_task_list_success(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(return_value="任务文本")
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
with patch(
|
||||
"douyin.fudai.douyin_fudai_core_pkg.heavy_ocr._read_disable_flag",
|
||||
return_value=False,
|
||||
):
|
||||
assert runner.ocr_task_list() == "任务文本"
|
||||
|
||||
def test_content_disabled_skips(self):
|
||||
a = _mk_analyser()
|
||||
runner = OcrTaskRunnerFallback(a, disable_content_recognition=True)
|
||||
a.operation.analyse_pic_word = MagicMock()
|
||||
assert runner.ocr_content() is None
|
||||
a.operation.analyse_pic_word.assert_not_called()
|
||||
|
||||
def test_content_enabled_runs(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(return_value="content")
|
||||
runner = OcrTaskRunnerFallback(a, disable_content_recognition=False)
|
||||
assert runner.ocr_content() == "content"
|
||||
|
||||
def test_countdown_returns_raw_no_extraction(self):
|
||||
"""fallback 不做数字提取,直接返回原始 OCR 结果。"""
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_digits = MagicMock(return_value="abc12def")
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
# fallback 版本直接返回原始结果,不提取数字
|
||||
assert runner.ocr_countdown_mm() == "abc12def"
|
||||
assert runner.ocr_countdown_ss() == "abc12def"
|
||||
|
||||
def test_countdown_throws_returns_none(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_digits = MagicMock(side_effect=RuntimeError())
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
assert runner.ocr_countdown_mm() is None
|
||||
assert runner.ocr_countdown_ss() is None
|
||||
|
||||
def test_value_passes_through(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(return_value="¥888")
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
assert runner.ocr_value() == "¥888"
|
||||
|
||||
def test_button_keyword_short_circuit(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(return_value="参与抽奖")
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
assert runner.ocr_button() == "参与抽奖"
|
||||
# 含关键词,仅 1 次 OCR
|
||||
assert a.operation.analyse_pic_word.call_count == 1
|
||||
|
||||
def test_button_no_keyword_retries(self):
|
||||
"""无关键词时再试一次(保留旧 fallback 行为)。"""
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(
|
||||
side_effect=["未知文本", "重试结果"]
|
||||
)
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
# 第一次返回非关键词 → 进 retry → 返回第二次结果
|
||||
assert runner.ocr_button() == "重试结果"
|
||||
assert a.operation.analyse_pic_word.call_count == 2
|
||||
|
||||
def test_button_throws(self):
|
||||
a = _mk_analyser()
|
||||
a.operation.analyse_pic_word = MagicMock(side_effect=RuntimeError())
|
||||
runner = OcrTaskRunnerFallback(a)
|
||||
assert runner.ocr_button() is None
|
||||
Reference in new issue
Block a user