Files
douyi_guaji_main/douyin/handlers/screenshot_manager/paths.py
T
CodexandClaude Opus 4.7 bf7f30996e 拆分 douyin_screenshot_manager (1705 → 6 个子模块 + 79 行 shim)
把巨型文件 douyin/handlers/douyin_screenshot_manager.py 按职责拆成
douyin/handlers/screenshot_manager/ 子包:

- _common (79)     共享 logger / 全局状态 / 异步计数器
- paths (205)      OCR 目录、调试存档目录、各类路径
- save_files (173) save_latest_attempt / save_last_success / save_*_from_file / cleanup_ocr_dir
- async_save (360) _debug_save_loop + 工作线程启停(global 状态走 _common._state.X)
- cleanup (293)    debug_captures 目录清理与轮转
- save_image (731) save_debug_image 主入口(最大头)

原文件改为 79 行 re-export shim,30 个公共符号全部保留。
外部 29 处 save_debug_image / 6 处 save_latest_attempt / 等
import 路径不变,零调用方修改。

回归:
- 子包 7 文件 + shim compileall 通过
- pytest 353 passed / 1 skipped(与拆分前完全一致)
- 旧 import 路径与新子包路径返回同一函数对象
- AST 验证 30 个期望符号全部正确暴露

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 16:25:59 +08:00

206 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
"""screenshot_manager.paths - 路径计算与目录确保。
包含:OCR 目录、调试存档目录、最新尝试 / 上次成功的路径,
以及按图片名称分类到子目录的辅助函数。
"""
import os
from datetime import datetime
from douyin.core import path_config
def get_ocr_dir() -> str:
"""获取OCR目录路径"""
return os.path.join(path_config.get_pic_dir(), "ocr")
def get_category_for_picname(picname: str) -> str:
"""根据图片名称判断应该保存到哪个分类目录
参数:
picname: 图片名称(不含扩展名)
返回:
分类目录名称,如 "fudai", "live_room", "popups", "buttons", "stage_diag", "others"
"""
picname_lower = picname.lower()
# 福袋相关
if any(keyword in picname_lower for keyword in ["福袋", "fudai"]):
return "fudai"
# 直播间相关
if any(
keyword in picname_lower
for keyword in [
"live_room",
"直播间",
"直播列表",
"直播广场",
"直播状态",
"直播验证",
]
):
return "live_room"
# 弹窗相关
if any(
keyword in picname_lower
for keyword in [
"抽奖结果",
"无奖励",
"优惠券",
"购物红包",
"奖励通知",
"订单确认",
"lucky_draw",
"coupon",
"redpacket",
"reward",
"order",
]
):
return "popups"
# 按钮相关
if any(
keyword in picname_lower
for keyword in ["参与按钮", "粉丝团", "attend_button", "fans_club"]
):
return "buttons"
# 阶段诊断相关
if any(
keyword in picname_lower
for keyword in [
"推荐页",
"关注列表",
"直播列表标签",
"recommend",
"follow",
"live_list",
]
):
return "stage_diag"
# 其他
return "others"
def get_latest_attempt_path(operation_type: str = "general") -> str:
"""获取实时操作截图路径
参数:
operation_type: 操作类型,可选值:
- "pixel" 或 "pixel_detect": 像素检测
- "ocr": OCR识别
- "template" 或 "template_match": 模板匹配
- 其他: 通用操作
"""
# 规范化操作类型
if operation_type.startswith("pixel"):
type_key = "pixel"
elif operation_type.startswith("ocr") or operation_type.startswith("cut_pic"):
type_key = "ocr"
elif operation_type.startswith("template"):
type_key = "template"
else:
type_key = "general"
return os.path.join(get_ocr_dir(), f"latest_attempt_{type_key}.png")
def get_last_success_path(operation_type: str = "general") -> str:
"""获取成功识别存档截图路径
参数:
operation_type: 操作类型,可选值:
- "pixel" 或 "pixel_detect": 像素检测
- "ocr": OCR识别
- "template" 或 "template_match": 模板匹配
- 其他: 通用操作
"""
# 规范化操作类型
if operation_type.startswith("pixel"):
type_key = "pixel"
elif operation_type.startswith("ocr") or operation_type.startswith("cut_pic"):
type_key = "ocr"
elif operation_type.startswith("template"):
type_key = "template"
else:
type_key = "general"
return os.path.join(get_ocr_dir(), f"last_success_{type_key}.png")
def ensure_ocr_dir() -> None:
"""确保OCR目录存在"""
ocr_dir = get_ocr_dir()
os.makedirs(ocr_dir, exist_ok=True)
def get_debug_captures_dir() -> str:
"""获取调试图像存档目录路径"""
pic_dir = path_config.get_pic_dir()
debug_dir = os.path.join(pic_dir, "debug_captures")
# 按日期分类:YYYY-MM-DD
from datetime import datetime
date_str = datetime.now().strftime("%Y-%m-%d")
return os.path.join(debug_dir, date_str)
def ensure_debug_captures_dir() -> None:
"""确保调试图像存档目录存在"""
try:
debug_dir = get_debug_captures_dir()
# 确保父目录也存在
parent_dir = os.path.dirname(debug_dir)
if parent_dir and not os.path.exists(parent_dir):
try:
os.makedirs(parent_dir, exist_ok=True)
except PermissionError as e:
raise PermissionError(f"无权限创建调试图像目录: {parent_dir}") from e
except OSError as e:
raise OSError(f"创建调试图像目录失败: {parent_dir}") from e
try:
os.makedirs(debug_dir, exist_ok=True)
except PermissionError as e:
raise PermissionError(f"无权限创建调试图像目录: {debug_dir}") from e
except OSError as e:
raise OSError(f"创建调试图像目录失败: {debug_dir}") from e
except (PermissionError, OSError):
# 权限或系统错误,重新抛出
raise
except Exception as e:
# 其他未知错误
error_type = type(e).__name__
try:
_mgr_log(f"[截图管理] 创建调试图像目录失败({error_type}): {e}")
except Exception:
pass
raise
def get_debug_captures_base_dir() -> str:
"""获取调试图像存档根目录路径(不包含日期)"""
pic_dir = path_config.get_pic_dir()
return os.path.join(pic_dir, "debug_captures")