#!/usr/bin/env python3
"""Locuta una carta entera con edge-tts (online, gratis). Para comparar con XTTS.
Uso: tts_carta_edge.py [voz] [nombre_salida]
voz por defecto: es-ES-XimenaNeural
"""
import html
import json
import os
import re
import subprocess
import sys
from pathlib import Path
EDGE = os.path.expanduser("~/.hermes/hermes-agent/venv/bin/edge-tts")
OUT = Path(__file__).resolve().parent.parent / "wordpress/wp-content/uploads/tts-samples"
CONTAINER = "wordpress-web"
def get_post_text(pid):
subprocess.run(["docker", "exec", CONTAINER, "php", "/tmp/fea_post_io.php", "get", str(pid)],
check=True, capture_output=True)
subprocess.run(["docker", "cp", f"{CONTAINER}:/tmp/fea_es.json", "/tmp/fea_es.json"], check=True)
d = json.load(open("/tmp/fea_es.json"))
raw = d["content"]
raw = re.sub(r"(?i)
|
|", "\n", raw)
raw = re.sub(r"<[^>]+>", "", raw)
raw = re.sub(r"\[[^\]]+\]", "", raw)
raw = html.unescape(raw)
paras = [re.sub(r"\s+", " ", p).strip() for p in raw.split("\n")]
return d["title"], [p for p in paras if len(p) > 1]
def main():
pid = int(sys.argv[1])
voice = sys.argv[2] if len(sys.argv) > 2 else "es-ES-XimenaNeural"
name = sys.argv[3] if len(sys.argv) > 3 else f"carta-edge-{pid}"
title, paras = get_post_text(pid)
text = "\n\n".join(paras)
txt_path = "/tmp/carta_text.txt"
open(txt_path, "w").write(text)
print(f"Post #{pid}: «{title}» ({len(text)} car) → {voice}")
OUT.mkdir(parents=True, exist_ok=True)
mp3 = OUT / f"{name}.mp3"
subprocess.run([EDGE, "--voice", voice, "--file", txt_path,
"--write-media", str(mp3)], check=True)
print(f"OK -> {mp3}")
if __name__ == "__main__":
main()