Files
douyin_guaji-main/douyin/handlers/screenshot_manager/paths.py
T
CodexandClaude Sonnet 4.5 ca00258bf3 fix(code-quality): 修复 NameError 风险和死代码清理
- detect_by_template.py: 添加缺失的 os 和 path_config 导入
- join_decision.py: 添加缺失的 is_watch_task_fudai 导入
- read_once.py: 添加缺失的 Optional 类型导入
- fudai_detail_validator.py: 使用 extract_2digits 做更健壮的数字解析
- screenshot_manager: 移除未使用的导入和死代码

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-05-23 20:22:52 +08:00

208 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
from ._common import _mgr_log
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")