"""
配置管理模块
使用 pydantic-settings 管理环境变量和配置
"""
import os
import tempfile
from pathlib import Path
from typing import Optional
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""应用配置"""
# 服务器配置
host: str = "0.0.0.0"
port: int = 18060
# 浏览器配置
headless: bool = True
browser_bin_path: Optional[str] = None
# 反检测配置
enable_stealth: bool = True
min_action_delay: int = 1000 # 最小操作延迟(毫秒)
max_action_delay: int = 3000 # 最大操作延迟(毫秒)
daily_publish_limit: int = 10 # 每日发布限制
daily_comment_limit: int = 50 # 每日评论限制
daily_like_limit: int = 100 # 每日点赞限制
daily_favorite_limit: int = 50 # 每日收藏限制
# Cookie 配置
cookies_path: str = "./data/cookies.json"
# 日志配置
log_level: str = "INFO"
# 图片目录
images_dir: str = "xiaohongshu_images"
# 用户名
username: str = "xiaohongshu-mcp"
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
)
def get_images_path(self) -> Path:
"""获取图片存储路径"""
return Path(tempfile.gettempdir()) / self.images_dir
def ensure_images_dir(self) -> Path:
"""确保图片目录存在"""
images_path = self.get_images_path()
images_path.mkdir(parents=True, exist_ok=True)
return images_path
def ensure_cookies_dir(self) -> None:
"""确保 cookies 文件的目录存在"""
cookies_path = Path(self.cookies_path)
cookies_path.parent.mkdir(parents=True, exist_ok=True)
# 全局配置实例
settings = Settings()