Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz, Clipador de lives e Publicador para YouTube, TikTok e Instagram. Inclui a leitura das visualizações dos posts para o Dashboard e o botão "Publicar no <rede>" nos cartões das publicações. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
"""Configuração do Publicador, lida de publisher/.env (e do ambiente, que tem prioridade)."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from dataclasses import dataclass, replace
|
||
from pathlib import Path
|
||
|
||
PUBLISHER_DIR = Path(__file__).resolve().parent.parent
|
||
REPO_ROOT = PUBLISHER_DIR.parent
|
||
PORT_RANGE = (4500, 4599)
|
||
# Portas de depuração (CDP) dos navegadores do modo sessão: uma por janela aberta.
|
||
BROWSER_PORT_RANGE = (4540, 4549)
|
||
|
||
|
||
class ConfigError(RuntimeError):
|
||
"""Configuração inválida: o serviço não sobe."""
|
||
|
||
|
||
def read_env_file(path: Path) -> dict[str, str]:
|
||
if not path.is_file():
|
||
return {}
|
||
values: dict[str, str] = {}
|
||
for line in path.read_text(encoding="utf-8").splitlines():
|
||
if "=" not in line or line.lstrip().startswith("#"):
|
||
continue
|
||
name, value = line.split("=", 1)
|
||
values[name.strip()] = value.strip()
|
||
return values
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class Settings:
|
||
service_key: str
|
||
port: int
|
||
data_dir: Path
|
||
# Raiz do projeto: `file`/`cover` dos jobs precisam estar dentro dela.
|
||
repo_root: Path
|
||
browser_path: str | None
|
||
ig_api_version: str
|
||
|
||
@property
|
||
def base_url(self) -> str:
|
||
return f"http://127.0.0.1:{self.port}"
|
||
|
||
def redirect_uri(self, platform: str) -> str:
|
||
return f"{self.base_url}/oauth/callback/{platform}"
|
||
|
||
@property
|
||
def db_path(self) -> Path:
|
||
return self.data_dir / "publisher.db"
|
||
|
||
@property
|
||
def vault_path(self) -> Path:
|
||
return self.data_dir / "vault.bin"
|
||
|
||
@property
|
||
def browser_dir(self) -> Path:
|
||
return self.data_dir / "browser"
|
||
|
||
@property
|
||
def logs_dir(self) -> Path:
|
||
return self.data_dir / "logs"
|
||
|
||
@staticmethod
|
||
def load(env_file: Path | None = None, **overrides: object) -> "Settings":
|
||
env = {**read_env_file(env_file or PUBLISHER_DIR / ".env"), **os.environ}
|
||
raw_port = env.get("PUBLISHER_PORT", "") or "4530"
|
||
try:
|
||
port = int(raw_port)
|
||
except ValueError as exc:
|
||
raise ConfigError("PUBLISHER_PORT precisa ser número") from exc
|
||
if not PORT_RANGE[0] <= port <= PORT_RANGE[1]:
|
||
raise ConfigError(f"PUBLISHER_PORT={port} fora da faixa {PORT_RANGE[0]}–{PORT_RANGE[1]} do CenaForge")
|
||
|
||
data_dir = REPO_ROOT / "data" / "publisher"
|
||
raw_data = env.get("PUBLISHER_DATA_DIR", "").strip()
|
||
if raw_data:
|
||
path = Path(raw_data)
|
||
path = (path if path.is_absolute() else REPO_ROOT / path).resolve()
|
||
if not path.is_relative_to(REPO_ROOT.resolve()):
|
||
raise ConfigError(f"PUBLISHER_DATA_DIR precisa ficar dentro do repositório ({REPO_ROOT})")
|
||
data_dir = path
|
||
|
||
settings = Settings(
|
||
service_key=env.get("PUBLISHER_SERVICE_KEY", ""),
|
||
port=port,
|
||
data_dir=data_dir,
|
||
repo_root=REPO_ROOT,
|
||
browser_path=env.get("PUBLISHER_BROWSER_PATH", "").strip() or None,
|
||
ig_api_version=env.get("PUBLISHER_IG_API_VERSION", "").strip() or "v24.0",
|
||
)
|
||
if overrides:
|
||
settings = replace(settings, **overrides) # type: ignore[arg-type]
|
||
return settings
|