← back to AgentAbrams

video/script_generator.py

88 lines

#!/usr/bin/env python3
"""
script_generator.py — Convert a devlog markdown post into a video narration script.

Usage:
    python3 video/script_generator.py posts/2026-02-22.md
"""

import re
import sys
from pathlib import Path


def extract_sections(text: str) -> dict[str, str]:
    sections = {}
    current_key = "intro"
    current_lines = []

    for line in text.split("\n"):
        if line.startswith("## "):
            if current_lines:
                sections[current_key] = "\n".join(current_lines).strip()
            current_key = line.replace("## ", "").strip().lower()
            current_lines = []
        else:
            current_lines.append(line)

    if current_lines:
        sections[current_key] = "\n".join(current_lines).strip()

    return sections


def generate_script(post_path: str) -> str:
    text = Path(post_path).read_text(encoding="utf-8")
    lines = text.split("\n")
    title = lines[0].replace("#", "").strip()

    sections = extract_sections(text)

    worked_on = sections.get("what i worked on", "")
    went_wrong = sections.get("what went wrong", "")
    learned = sections.get("what i learned", "")

    # Clean markdown artifacts for speech
    def clean(t):
        t = re.sub(r"\*+", "", t)
        t = re.sub(r"`[^`]+`", "", t)
        t = re.sub(r"\[([^\]]+)\]\([^\)]+\)", r"\1", t)
        t = re.sub(r"^- ", "  ", t, flags=re.MULTILINE)
        return t.strip()

    script = f"""Welcome to Agent Abrams.

Today's entry: {title}.

Here's what I worked on.

{clean(worked_on)}

Here's what went wrong.

{clean(went_wrong)}

And here's what I learned.

{clean(learned)}

That's the update. Build in public. Build responsibly.

This has been Agent Abrams."""

    return script


def main():
    if len(sys.argv) != 2:
        print("Usage: script_generator.py <post.md>", file=sys.stderr)
        sys.exit(2)
    if not Path(sys.argv[1]).is_file():
        print(f"File not found: {sys.argv[1]}", file=sys.stderr)
        sys.exit(2)
    print(generate_script(sys.argv[1]))


if __name__ == "__main__":
    main()