#!/usr/bin/env python3
"""Fetch YouTube captions for the videos in docs/voice/videos.tsv into docs/voice/transcripts/.

Usage:
  pip3 install --target /tmp/ytlib youtube-transcript-api
  python3 scripts/fetch-voice-transcripts.py docs/voice/videos.tsv docs/voice/transcripts /tmp/ytlib

Already downloaded transcripts are skipped. YouTube rate-limits bursts (IpBlocked):
the script waits 20 seconds between videos; rerun later to pick up the rest.
"""
import sys, time, csv, json, pathlib
sys.path.insert(0, sys.argv[3] if len(sys.argv) > 3 else 'vendor-py')
from youtube_transcript_api import YouTubeTranscriptApi
api = YouTubeTranscriptApi()
out = pathlib.Path(sys.argv[2])
rows = [l.rstrip('\n').split('\t') for l in open(sys.argv[1]) if l.strip()]
summary = []
for path, vid, title in rows:
    slug = path.rstrip('/').split('/')[-1]
    target = out / f"{slug}.md"
    if target.exists():
        summary.append((slug, vid, title, 'cached', len(target.read_text().split())))
        continue
    try:
        tl = api.list(vid)
        kinds = {t.language_code: ('generated' if t.is_generated else 'manual') for t in tl}
        lang = next((l for l in ['en', 'en-US', 'en-GB'] if l in kinds), None) or next(iter(kinds))
        fetched = api.fetch(vid, languages=[lang])
        lines = []
        for s in fetched:
            m, sec = divmod(int(s.start), 60)
            h, m = divmod(m, 60)
            stamp = f"{h}:{m:02d}:{sec:02d}" if h else f"{m}:{sec:02d}"
            lines.append(f"{stamp} {s.text.replace(chr(10), ' ').strip()}")
        body = "\n".join(lines)
        front = (f"---\ntitle: {json.dumps(title)}\nvideo_id: {vid}\nurl: https://www.youtube.com/watch?v={vid}\n"
                 f"workflow_page: https://workflows-of-ai.com{path}\nlanguage: {lang}\ncaptions: {kinds[lang]}\n---\n\n")
        target.write_text(front + body + "\n")
        summary.append((slug, vid, title, kinds[lang], len(body.split())))
    except Exception as e:
        summary.append((slug, vid, title, 'ERROR ' + type(e).__name__, 0))
    time.sleep(20)
for r in summary:
    print("\t".join(map(str, r)))
