← back to AgentAbrams

media/md_to_podcast.py

66 lines

#!/usr/bin/env python3
"""
md_to_podcast.py — Transform any markdown file into a podcast narration script.

Credits the developer and explains what it is, how it works, and how to build it.

Usage:
    python3 media/md_to_podcast.py <source.md> <developer_name>
"""

import re
import sys
from pathlib import Path


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

    # Clean markdown for speech
    body = text
    body = re.sub(r"^#+ .*\n", "", body, flags=re.MULTILINE)
    body = re.sub(r"\*+", "", body)
    body = re.sub(r"`[^`]+`", "", body)
    body = re.sub(r"\[([^\]]+)\]\([^\)]+\)", r"\1", body)
    body = re.sub(r"^- ", "  ", body, flags=re.MULTILINE)
    body = re.sub(r"\n{3,}", "\n\n", body)
    body = body.strip()

    # Truncate for ~3 minute episode
    if len(body) > 2000:
        body = body[:2000] + "..."

    script = f"""Welcome to the Agent Abrams podcast.

I'm {developer}, and today we're looking at: {title}.

Let me walk you through what this is, how it works, and how you can build it yourself.

{body}

This content excludes proprietary processes and confidential information.
Everything shared here is designed to be reproducible and useful to any builder.

If you want to try this yourself, check out the AgentAbrams Public repository on GitHub.

Thanks for listening. Build in public. Build responsibly.

This has been Agent Abrams."""

    return script


def main():
    if len(sys.argv) < 3:
        print("Usage: md_to_podcast.py <source.md> <developer_name>", 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(transform(sys.argv[1], sys.argv[2]))


if __name__ == "__main__":
    main()