import os import re import unicodedata from datetime import datetime def slugify(text: str) -> str: for src, dst in [("ä","a"),("ö","o"),("ü","u"),("Ä","a"),("Ö","o"),("Ü","u"),("ß","ss")]: text = text.replace(src, dst) text = unicodedata.normalize("NFKD", text) text = "".join(c for c in text if unicodedata.category(c) != "Mn") text = text.lower() text = re.sub(r"[^a-z0-9]+", "-", text) return text.strip("-") def save_transcript( title: str, content: str, output_dir: str, dt: datetime | None = None, ) -> str: if dt is None: dt = datetime.now() slug = slugify(title)[:60] filename = f"{dt.strftime('%Y-%m-%d-%H%M')}-{slug}.md" os.makedirs(output_dir, exist_ok=True) path = os.path.join(output_dir, filename) with open(path, "w", encoding="utf-8") as f: f.write(f"---\ndate: {dt.isoformat(timespec='seconds')}\ntags: [transkript]\n---\n\n") f.write(f"# {title}\n\n") f.write(content) if not content.endswith("\n"): f.write("\n") return path def read_transcript(output_dir: str, filename: str) -> str | None: """Return file content if filename is a plain .md file inside output_dir.""" if os.path.basename(filename) != filename or not filename.endswith(".md"): return None path = os.path.join(output_dir, filename) if not os.path.exists(path): return None with open(path, encoding="utf-8") as f: return f.read() def list_transcripts(output_dir: str, limit: int = 20) -> list[dict]: if not os.path.exists(output_dir): return [] files = sorted( [f for f in os.listdir(output_dir) if f.endswith(".md")], reverse=True, )[:limit] result = [] for f in files: full = os.path.join(output_dir, f) stat = os.stat(full) result.append({"filename": f, "path": full, "size": stat.st_size, "mtime": stat.st_mtime}) return result