"""video 模块独立 .env 加载器 — 与 djapi/env_loader.py 功能一致但非 Django 依赖"""
import os
from pathlib import Path


def _load_dotenv():
    """从项目根目录 .env 加载环境变量（不覆盖已有）"""
    # video/env.py → video/ → api/ → djapi/ (项目根)
    base_dir = Path(__file__).resolve().parent.parent.parent
    dotenv_path = base_dir / '.env'

    if not dotenv_path.exists():
        return

    with open(dotenv_path) as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith('#') or '=' not in line:
                continue
            key, _, value = line.partition('=')
            key = key.strip()
            value = value.strip().strip('"').strip("'")
            if key and key not in os.environ:
                os.environ[key] = value


_load_dotenv()
