拆分 douyin_fudai_core (2205 → 2124 + 3 个子模块)
把 douyin/fudai/douyin_fudai_core.py 中工具/兼容部分搬到
douyin/fudai/douyin_fudai_core_pkg/ 子包:
- ocr_executor (46) _FudaiOcrExecutorState 类 + _fudai_ocr_executor_state
全局实例 + _get_fudai_ocr_executor 池复用函数
- thin_wrappers (65) 11 个 [兼容] 薄壳 forwarding 函数
(attend_choujiang / check_lucky_draw_result /
is_watch_task_fudai / should_skip_fudai_by_config /
basic_join_decision 等)
巨型 get_fudai_contain (2062 行) 留在原文件。原文件从子包 import 后
re-export,外部全部不需修改:
- should_skip_fudai_by_config 24 处外部引用
- attend_choujiang 15 处 / is_watch_task_fudai 13 处 / basic_join_decision 13 处
- 其他 1-10 处
回归:
- 4 个新/改文件 compileall 通过
- pytest 355 passed / 1 skipped(其中 2 个新 _is_short_countdown_text 测试也通过)
- attend_choujiang shim ⇄ pkg 同一对象
- should_skip_fudai_by_config(高价值) 返回 (False, 'ok')
- _fudai_ocr_executor_state.workers = 0 全局实例正常
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
a337b4b93d
commit
57d99f3459
4 files changed
+171
-98
No files matched your search
@@ -26,6 +26,23 @@ from douyin.fudai.core.fudai_debug_utils import (
|
||||
_save_full_screenshot_with_ocr_region,
|
||||
)
|
||||
|
||||
|
||||
# 从子包 re-export(保持兼容):薄壳 forwarding + OCR 线程池
|
||||
from douyin.fudai.douyin_fudai_core_pkg import (
|
||||
_FudaiOcrExecutorState,
|
||||
_fudai_ocr_executor_state,
|
||||
_get_fudai_ocr_executor,
|
||||
attend_choujiang,
|
||||
basic_join_decision,
|
||||
check_have_reward_notice_confirm,
|
||||
check_in_order_confirm_page,
|
||||
check_lucky_draw_result,
|
||||
get_reward,
|
||||
is_watch_task_fudai,
|
||||
should_skip_fudai_by_config,
|
||||
_is_short_countdown_text,
|
||||
)
|
||||
|
||||
try:
|
||||
from douyin.utils.ocr_preprocess import preprocess_for_ocr # type: ignore
|
||||
except Exception: # pyright: ignore
|
||||
@@ -41,33 +58,6 @@ except Exception: # pyright: ignore
|
||||
# 模块级线程池状态,用于复用 OCR 并行执行,避免频繁创建/销毁
|
||||
|
||||
|
||||
class _FudaiOcrExecutorState:
|
||||
def __init__(self) -> None:
|
||||
self.executor: Optional[ThreadPoolExecutor] = None
|
||||
self.workers: int = 0
|
||||
self.lock = threading.Lock()
|
||||
|
||||
|
||||
_fudai_ocr_executor_state = _FudaiOcrExecutorState()
|
||||
|
||||
|
||||
def _get_fudai_ocr_executor(optimal_workers: int) -> ThreadPoolExecutor:
|
||||
"""获取复用的 OCR 线程池;如线程数变化则重建。"""
|
||||
state = _fudai_ocr_executor_state
|
||||
with state.lock:
|
||||
if state.executor and state.workers == optimal_workers:
|
||||
return state.executor
|
||||
# 若线程数变化,安全关闭旧池
|
||||
if state.executor:
|
||||
try:
|
||||
state.executor.shutdown(wait=False, cancel_futures=True)
|
||||
except RuntimeError:
|
||||
pass
|
||||
state.executor = None
|
||||
executor = ThreadPoolExecutor(max_workers=optimal_workers)
|
||||
state.executor = executor
|
||||
state.workers = optimal_workers
|
||||
return executor
|
||||
|
||||
|
||||
def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, float]:
|
||||
@@ -2132,74 +2122,3 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo
|
||||
# 此处保留模块级名称以兼容历史 from douyin_fudai_core import check_contain。
|
||||
|
||||
|
||||
def attend_choujiang(analyser) -> bool: # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_attend。"""
|
||||
from douyin.fudai.core.fudai_attend import attend_choujiang as _impl
|
||||
|
||||
return _impl(analyser)
|
||||
|
||||
|
||||
def check_lucky_draw_result(analyser): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_lucky_draw_result as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
|
||||
|
||||
def check_have_reward_notice_confirm(analyser) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_have_reward_notice_confirm as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
|
||||
|
||||
def check_in_order_confirm_page(analyser) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_in_order_confirm_page as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
|
||||
|
||||
def get_reward(analyser, reward_y: int = 0) -> None: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import get_reward as _impl
|
||||
|
||||
return _impl(analyser, reward_y=reward_y)
|
||||
|
||||
|
||||
def is_watch_task_fudai(content: str) -> bool: # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import is_watch_task_fudai as _impl
|
||||
|
||||
return _impl(content)
|
||||
|
||||
|
||||
def _is_short_countdown_text(time_text: str) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import (
|
||||
_is_short_countdown_text as _impl,
|
||||
)
|
||||
|
||||
return _impl(time_text)
|
||||
|
||||
|
||||
def should_skip_fudai_by_config(analyser, detail): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import (
|
||||
should_skip_fudai_by_config as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser, detail)
|
||||
|
||||
|
||||
def basic_join_decision(analyser, detail): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import basic_join_decision as _impl
|
||||
|
||||
return _impl(analyser, detail)
|
||||
@@ -0,0 +1,43 @@
|
||||
"""douyin_fudai_core_pkg - 福袋核心薄壳子包。
|
||||
|
||||
原 douyin/fudai/douyin_fudai_core.py 的工具/兼容部分搬到本子包:
|
||||
|
||||
- ocr_executor _FudaiOcrExecutorState / _get_fudai_ocr_executor / _fudai_ocr_executor_state
|
||||
- thin_wrappers 11 个 [兼容] 薄壳 forwarding 函数
|
||||
|
||||
巨型 get_fudai_contain (2062 行) 留在原文件 douyin_fudai_core.py 不变。
|
||||
外部 ``from douyin.fudai.douyin_fudai_core import attend_choujiang`` 等
|
||||
完全兼容(原文件 from 子包 import 后自动暴露)。
|
||||
"""
|
||||
|
||||
from .ocr_executor import (
|
||||
_FudaiOcrExecutorState,
|
||||
_fudai_ocr_executor_state,
|
||||
_get_fudai_ocr_executor,
|
||||
)
|
||||
from .thin_wrappers import (
|
||||
attend_choujiang,
|
||||
basic_join_decision,
|
||||
check_have_reward_notice_confirm,
|
||||
check_in_order_confirm_page,
|
||||
check_lucky_draw_result,
|
||||
get_reward,
|
||||
is_watch_task_fudai,
|
||||
should_skip_fudai_by_config,
|
||||
_is_short_countdown_text,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"_FudaiOcrExecutorState",
|
||||
"_fudai_ocr_executor_state",
|
||||
"_get_fudai_ocr_executor",
|
||||
"attend_choujiang",
|
||||
"check_lucky_draw_result",
|
||||
"check_have_reward_notice_confirm",
|
||||
"check_in_order_confirm_page",
|
||||
"get_reward",
|
||||
"is_watch_task_fudai",
|
||||
"_is_short_countdown_text",
|
||||
"should_skip_fudai_by_config",
|
||||
"basic_join_decision",
|
||||
]
|
||||
@@ -0,0 +1,46 @@
|
||||
"""douyin_fudai_core_pkg.ocr_executor - OCR 线程池复用。
|
||||
|
||||
避免每次 OCR 都创建/销毁 ThreadPoolExecutor 的开销。
|
||||
线程数变化时安全销毁旧池并新建。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class _FudaiOcrExecutorState:
|
||||
def __init__(self) -> None:
|
||||
self.executor: Optional[ThreadPoolExecutor] = None
|
||||
self.workers: int = 0
|
||||
self.lock = threading.Lock()
|
||||
|
||||
|
||||
_fudai_ocr_executor_state = _FudaiOcrExecutorState()
|
||||
|
||||
|
||||
|
||||
|
||||
_fudai_ocr_executor_state = _FudaiOcrExecutorState()
|
||||
|
||||
|
||||
def _get_fudai_ocr_executor(optimal_workers: int) -> ThreadPoolExecutor:
|
||||
"""获取复用的 OCR 线程池;如线程数变化则重建。"""
|
||||
state = _fudai_ocr_executor_state
|
||||
with state.lock:
|
||||
if state.executor and state.workers == optimal_workers:
|
||||
return state.executor
|
||||
# 若线程数变化,安全关闭旧池
|
||||
if state.executor:
|
||||
try:
|
||||
state.executor.shutdown(wait=False, cancel_futures=True)
|
||||
except RuntimeError:
|
||||
pass
|
||||
state.executor = None
|
||||
executor = ThreadPoolExecutor(max_workers=optimal_workers)
|
||||
state.executor = executor
|
||||
state.workers = optimal_workers
|
||||
return executor
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
"""douyin_fudai_core_pkg.thin_wrappers - 11 个兼容性薄壳。
|
||||
|
||||
原 douyin/fudai/douyin_fudai_core.py 末尾 11 个 forwarding 函数全是
|
||||
``[兼容] 已迁移到 douyin.fudai.core.X`` 风格。本子模块集中存放,
|
||||
保持外部 ``from douyin.fudai.douyin_fudai_core import attend_choujiang``
|
||||
等仍然可用。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def attend_choujiang(analyser) -> bool: # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_attend。"""
|
||||
from douyin.fudai.core.fudai_attend import attend_choujiang as _impl
|
||||
|
||||
return _impl(analyser)
|
||||
def check_lucky_draw_result(analyser): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_lucky_draw_result as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
def check_have_reward_notice_confirm(analyser) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_have_reward_notice_confirm as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
def check_in_order_confirm_page(analyser) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import (
|
||||
check_in_order_confirm_page as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser)
|
||||
def get_reward(analyser, reward_y: int = 0) -> None: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_result_checker。"""
|
||||
from douyin.fudai.core.fudai_result_checker import get_reward as _impl
|
||||
|
||||
return _impl(analyser, reward_y=reward_y)
|
||||
def is_watch_task_fudai(content: str) -> bool: # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import is_watch_task_fudai as _impl
|
||||
|
||||
return _impl(content)
|
||||
def _is_short_countdown_text(time_text: str) -> bool: # noqa: F811
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import (
|
||||
_is_short_countdown_text as _impl,
|
||||
)
|
||||
|
||||
return _impl(time_text)
|
||||
def should_skip_fudai_by_config(analyser, detail): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import (
|
||||
should_skip_fudai_by_config as _impl,
|
||||
)
|
||||
|
||||
return _impl(analyser, detail)
|
||||
def basic_join_decision(analyser, detail): # noqa: F811 — 兼容性 re-export
|
||||
"""[兼容] 已迁移到 douyin.fudai.core.fudai_filter。"""
|
||||
from douyin.fudai.core.fudai_filter import basic_join_decision as _impl
|
||||
|
||||
return _impl(analyser, detail)
|
||||
Reference in new issue
Block a user