#!/usr/bin/env python3
"""Summarize YouTube videos with Claude: one video, a list, a playlist or a channel.

Transcripts come from YouTubeTranscriptAPI (https://youtubetranscriptapi.com);
summaries come from the Claude API. Every key point in a summary ends with the
[m:ss] moment it comes from, turned into a link that opens the video there.

  pip install anthropic
  export YT_API_KEY=...            # free key, 100 credits
  export ANTHROPIC_API_KEY=...

  python summarize_videos.py dQw4w9WgXcQ
  python summarize_videos.py --file videos.txt --style notes
  python summarize_videos.py --playlist PLxxxxxxxx --model claude-haiku-5-5
  python summarize_videos.py --channel @TED --estimate      # transcripts only, no LLM calls

Costs:
  transcript                    1 YouTubeTranscriptAPI credit per video (no captions = 404, free)
  playlist or channel listing   20 credits (up to 100 videos)
  summary                       Claude API tokens, priced in PRICES below (list prices, Nov 2026)

Output in ./summaries/:
  <videoId>.md            the summary, with a title, a link and timestamp links
  transcripts/<id>.json   cached transcripts, so re-runs never pay for them twice
  no_captions.txt         videos without captions (nothing to summarize)
  report.json             credits, tokens, estimated LLM cost, timestamp checks
Re-running skips videos that already have a summary.

Videos longer than --long-after minutes are summarized section by section
(--section-minutes each), then combined. That keeps timestamps accurate on
3-hour podcasts and stops the middle of a long video from getting skimmed.
"""

from __future__ import annotations

import argparse
import json
import os
import re
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path

BASE = os.environ.get("YT_API_BASE", "https://youtubetranscriptapi.com/api/v2")
TIMEOUT = 60
MAX_ATTEMPTS = 4
LISTING_COST = 20
ID_RE = re.compile(r"(?:v=|youtu\.be/|shorts/|embed/|live/)([A-Za-z0-9_-]{11})|^([A-Za-z0-9_-]{11})$")

# USD per million tokens (input, output), Claude API list prices checked November 2026:
# https://platform.claude.com/docs/en/about-claude/pricing
# Claude Haiku 5.5 costs more when one request's prompt is over 100,000 tokens.
PRICES = {
    "claude-haiku-5-5": ((0.10, 0.50), (0.50, 2.50)),
    "claude-sonnet-5-5": ((2.00, 10.00), (2.00, 10.00)),
    "claude-opus-5-5": ((4.00, 20.00), (4.00, 20.00)),
}
LONG_PROMPT = 100_000
# Models that take server-side refusal fallbacks (beta header below).
FALLBACK_MODELS = {"claude-opus-5-5", "claude-sonnet-5-5", "claude-fable-5-2"}
FALLBACK_BETA = "server-side-fallback-2026-07-01"

STYLES = {
    "brief": "A one-paragraph overview (who is speaking, what it is about), then 5-8 key points as bullets, "
             "then one line starting 'Worth watching if'.",
    "notes": "Study notes: a short overview, then headed sections that follow the video's order, with "
             "definitions, steps and examples as bullets, then 3 review questions.",
    "chapters": "A chapter list: one line per topic change, each starting with its timestamp, followed by "
                "a one-sentence description.",
}

SYSTEM = """You summarize YouTube videos from their transcripts.

Rules:
- Use only what the transcript says. Do not add facts, names or numbers from elsewhere.
- End every key point with the timestamp where it is said, written as [m:ss] or [h:mm:ss], copied from the transcript.
- Captions are automatic and can misspell names and terms. Keep the wording, and flag a passage as unclear instead of guessing.
- If the transcript is too short or garbled to summarize, say so in one line.
- Write in plain Markdown. No preamble."""

SECTION_PROMPT = """This is part {n} of {total} of a video transcript ({start} to {end}).
Write 4-8 bullet points covering what is said in this part, each ending with its [m:ss] or [h:mm:ss] timestamp.

<transcript>
{text}
</transcript>"""

FINAL_PROMPT = """Video: {title}
{reply}Write this summary: {style}

<transcript>
{text}
</transcript>"""

COMBINE_PROMPT = """Video: {title}
These are notes on each part of the video, in order, with timestamps. Combine them into one summary.
{reply}Write this summary: {style}
Keep the timestamps from the notes.

<notes>
{text}
</notes>"""


class ApiError(Exception):
    def __init__(self, status: int, code: str, detail):
        super().__init__(f"{status} {code}: {detail}")
        self.status, self.code, self.detail = status, code, detail


def call(path: str, params: dict, key: str) -> dict:
    """GET one YouTubeTranscriptAPI endpoint. Retries 408/429/503 and network errors, honoring Retry-After."""
    url = f"{BASE}{path}?{urllib.parse.urlencode(params)}"
    req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}", "Accept": "application/json"})
    for attempt in range(1, MAX_ATTEMPTS + 1):
        try:
            with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
                return json.loads(resp.read().decode("utf-8"))
        except urllib.error.HTTPError as e:
            try:
                body = json.loads(e.read().decode("utf-8") or "{}")
            except ValueError:
                body = {}
            if e.code in (408, 429, 503) and attempt < MAX_ATTEMPTS:
                time.sleep(_retry_after(e.headers.get("Retry-After"), attempt))
                continue
            raise ApiError(e.code, body.get("code") or body.get("error") or "error", body.get("detail")) from None
        except (urllib.error.URLError, TimeoutError):
            if attempt < MAX_ATTEMPTS:
                time.sleep(2 ** attempt)
                continue
            raise
    raise RuntimeError("unreachable")


def _retry_after(value: str | None, attempt: int) -> float:
    try:
        return max(1.0, min(float(value), 120.0))
    except (TypeError, ValueError):
        return float(2 ** attempt)


def video_id(text: str) -> str | None:
    m = ID_RE.search(text.strip())
    return (m.group(1) or m.group(2)) if m else None


def stamp(seconds: float) -> str:
    s = int(seconds)
    h, m, s = s // 3600, (s % 3600) // 60, s % 60
    return f"{h}:{m:02d}:{s:02d}" if h else f"{m}:{s:02d}"


def to_seconds(text: str) -> int:
    secs = 0
    for part in text.split(":"):
        secs = secs * 60 + int(part)
    return secs


def segments_of(data: dict) -> list[dict]:
    t = data.get("transcript")
    if isinstance(t, list):
        return [s for s in t if (s.get("text") or "").strip()]
    return []


def render(segments: list[dict], every: float = 30.0) -> str:
    """Transcript as lines, each starting with the [m:ss] of its first caption, about every 30 seconds."""
    lines, current, line_start = [], [], None
    for seg in segments:
        start = float(seg.get("start") or 0)
        if line_start is None:
            line_start = start
        if current and start - line_start >= every:
            lines.append(f"[{stamp(line_start)}] {' '.join(current)}")
            current, line_start = [], start
        current.append(" ".join(seg["text"].split()))
    if current:
        lines.append(f"[{stamp(line_start)}] {' '.join(current)}")
    return "\n".join(lines)


def duration_of(segments: list[dict]) -> float:
    if not segments:
        return 0.0
    last = segments[-1]
    return float(last.get("start") or 0) + float(last.get("duration") or 0)


def sections(segments: list[dict], minutes: float) -> list[list[dict]]:
    out, cur, cur_start = [], [], 0.0
    for seg in segments:
        start = float(seg.get("start") or 0)
        if cur and start - cur_start >= minutes * 60:
            out.append(cur)
            cur, cur_start = [], start
        cur.append(seg)
    if cur:
        out.append(cur)
    return out


def est_tokens(text: str) -> int:
    """Rough estimate: about 4 characters per token for English. The API reports the real count."""
    return max(1, len(text) // 4)


def price(model: str, input_tokens: int, output_tokens: int) -> float | None:
    if model not in PRICES:
        return None
    short, long_ = PRICES[model]
    p_in, p_out = long_ if input_tokens > LONG_PROMPT else short
    return (input_tokens * p_in + output_tokens * p_out) / 1_000_000


TS_RE = re.compile(r"\[((?:\d{1,2}:)?\d{1,2}:\d{2})\](?!\()")


def link_timestamps(text: str, vid: str, limit: float) -> tuple[str, int, list[str]]:
    """Turn [m:ss] into links that open the video there. Returns (text, cited, out_of_range)."""
    cited, bad = 0, []

    def repl(m: re.Match) -> str:
        nonlocal cited
        cited += 1
        secs = to_seconds(m.group(1))
        if limit and secs > limit + 5:
            bad.append(m.group(1))
            return f"[{m.group(1)}?]"
        return f"[{m.group(1)}](https://www.youtube.com/watch?v={vid}&t={secs}s)"

    return TS_RE.sub(repl, text), cited, bad


class Summarizer:
    def __init__(self, client, model: str, effort: str):
        self.client, self.model, self.effort = client, model, effort
        self.input_tokens = self.output_tokens = 0
        self.cost = 0.0
        self.calls = 0

    def ask(self, prompt: str) -> str:
        kwargs = dict(
            model=self.model,
            max_tokens=16000,
            system=SYSTEM,
            messages=[{"role": "user", "content": prompt}],
            output_config={"effort": self.effort},
        )
        if self.model in FALLBACK_MODELS:
            kwargs.update(betas=[FALLBACK_BETA], fallbacks="default")
        resp = self.client.beta.messages.create(**kwargs)
        self.calls += 1
        used_in, used_out = resp.usage.input_tokens, resp.usage.output_tokens
        self.input_tokens += used_in
        self.output_tokens += used_out
        self.cost += price(getattr(resp, "model", None) or self.model, used_in, used_out) or 0.0
        if resp.stop_reason == "refusal":
            raise RuntimeError("the model declined to summarize this transcript")
        text = "".join(b.text for b in resp.content if getattr(b, "type", "") == "text").strip()
        if resp.stop_reason == "max_tokens":
            text += "\n\n_(Summary cut off at the output limit.)_"
        return text


def summarize_video(s: Summarizer, data: dict, title: str, style: str, reply: str,
                    long_after: float, section_minutes: float) -> str:
    segs = segments_of(data)
    total = duration_of(segs)
    reply_line = f"Reply in {reply}.\n" if reply else ""
    if total <= long_after * 60:
        return s.ask(FINAL_PROMPT.format(title=title, reply=reply_line, style=STYLES[style], text=render(segs)))
    parts = sections(segs, section_minutes)
    notes = []
    for n, part in enumerate(parts, 1):
        start, end = stamp(float(part[0].get("start") or 0)), stamp(duration_of(part))
        print(f"    part {n}/{len(parts)} ({start}-{end})")
        notes.append(f"## Part {n} ({start}-{end})\n" + s.ask(SECTION_PROMPT.format(
            n=n, total=len(parts), start=start, end=end, text=render(part))))
    return s.ask(COMBINE_PROMPT.format(title=title, reply=reply_line, style=STYLES[style], text="\n\n".join(notes)))


def main(argv: list[str] | None = None, client=None) -> int:
    p = argparse.ArgumentParser(description="Summarize YouTube videos with Claude, with timestamp links.")
    p.add_argument("videos", nargs="*", help="video IDs or URLs")
    p.add_argument("--file", help="text file with one video ID or URL per line")
    p.add_argument("--playlist", help="playlist ID or URL (20 credits, up to 100 videos)")
    p.add_argument("--channel", help="channel @handle, URL or UC ID: its 100 newest uploads (20 credits)")
    p.add_argument("--model", default="claude-opus-5-5", help="Claude model (default claude-opus-5-5; "
                   "claude-haiku-5-5 is the cheap bulk option)")
    p.add_argument("--effort", default="low", choices=["low", "medium", "high"], help="model effort (default low)")
    p.add_argument("--style", default="brief", choices=sorted(STYLES), help="summary style (default brief)")
    p.add_argument("--language", default="en", help="preferred caption language (default en)")
    p.add_argument("--reply-language", default="", help="write summaries in this language, e.g. German")
    p.add_argument("--long-after", type=float, default=60, help="summarize by sections past this many minutes (default 60)")
    p.add_argument("--section-minutes", type=float, default=20, help="section length in minutes (default 20)")
    p.add_argument("--estimate", action="store_true", help="fetch transcripts and estimate LLM cost; no LLM calls")
    p.add_argument("--out", default="summaries", help="output folder")
    a = p.parse_args(argv)

    key = os.environ.get("YT_API_KEY")
    if not key:
        print("Set YT_API_KEY first (free key, 100 credits: https://youtubetranscriptapi.com/signup)", file=sys.stderr)
        return 2
    if not a.estimate and client is None:
        if not os.environ.get("ANTHROPIC_API_KEY"):
            print("Set ANTHROPIC_API_KEY first, or run with --estimate.", file=sys.stderr)
            return 2
        import anthropic  # pip install anthropic
        client = anthropic.Anthropic(max_retries=4)

    videos: list[tuple[str, str]] = []
    raw = list(a.videos)
    if a.file:
        raw += [ln for ln in Path(a.file).read_text(encoding="utf-8").splitlines() if ln.strip() and not ln.startswith("#")]
    for item in raw:
        vid = video_id(item)
        if vid:
            videos.append((vid, ""))
        else:
            print(f"Skipping, not a video ID or URL: {item}", file=sys.stderr)

    credits = 0
    try:
        balance = call("/account/whoami", {}, key)["credits"]["spendable"]
        for kind, value in (("playlist", a.playlist), ("channel", a.channel)):
            if not value:
                continue
            listing = call("/youtube/playlist/videos" if kind == "playlist" else "/youtube/channel/videos", {kind: value}, key)
            credits += LISTING_COST
            for r in listing.get("results", []):
                if r.get("videoId"):
                    videos.append((r["videoId"], r.get("title") or ""))
            if listing.get("has_more"):
                print(f"Note: the {kind} has more than the {len(listing.get('results', []))} videos listed here.")
    except ApiError as e:
        print(f"Request failed (not charged): {e}", file=sys.stderr)
        return 1

    seen, unique = set(), []
    for v in videos:
        if v[0] not in seen:
            seen.add(v[0])
            unique.append(v)
    if not unique:
        print("No videos to process.", file=sys.stderr)
        return 2
    print(f"{len(unique)} videos. Balance {balance} credits; transcripts cost at most {len(unique)}.")

    out = Path(a.out)
    cache = out / "transcripts"
    cache.mkdir(parents=True, exist_ok=True)
    s = Summarizer(client, a.model, a.effort)
    done, skipped, no_captions, failed, flagged = [], [], [], [], {}
    est_in, est_cost = 0, {}
    for n, (vid, title) in enumerate(unique, 1):
        target = out / f"{vid}.md"
        if target.exists() and not a.estimate:
            skipped.append(vid)
            continue
        cached = cache / f"{vid}.json"
        if cached.exists():
            data = json.loads(cached.read_text(encoding="utf-8"))
        else:
            try:
                data = call("/youtube/transcript", {"video_url": vid, "include_timestamp": "true",
                                                    "send_metadata": "true", "language": a.language}, key)
            except ApiError as e:
                if e.status == 404:
                    no_captions.append(vid)
                    print(f"[{n}/{len(unique)}] {vid}: no captions (free)")
                    continue
                if e.status == 402:
                    print(f"Out of credits after {len(done)} videos; stopping.", file=sys.stderr)
                    break
                failed.append({"videoId": vid, "error": f"{e.status} {e.code}"})
                print(f"[{n}/{len(unique)}] {vid}: {e}", file=sys.stderr)
                continue
            credits += 1
            cached.write_text(json.dumps(data), encoding="utf-8")
        title = title or (data.get("metadata") or {}).get("title") or vid
        segs = segments_of(data)
        if not segs:
            no_captions.append(vid)
            continue
        if a.estimate:
            tokens = est_tokens(render(segs)) + 300  # transcript plus instructions
            est_in += tokens
            for m in PRICES:  # priced per request, as the API does; ~600 output tokens per summary
                est_cost[m] = est_cost.get(m, 0.0) + (price(m, tokens, 600) or 0.0)
            print(f"[{n}/{len(unique)}] {vid}: {stamp(duration_of(segs))}, ~{tokens:,} tokens")
            continue
        print(f"[{n}/{len(unique)}] {vid}: summarizing {title[:60]}")
        try:
            summary = summarize_video(s, data, title, a.style, a.reply_language, a.long_after, a.section_minutes)
        except Exception as e:  # keep going on one bad video; the SDK already retried transient errors
            failed.append({"videoId": vid, "error": str(e)[:200]})
            print(f"    failed: {e}", file=sys.stderr)
            continue
        body, cited, bad = link_timestamps(summary, vid, duration_of(segs))
        if bad:
            flagged[vid] = bad
            print(f"    check: {len(bad)} timestamp(s) past the end of the video: {', '.join(bad)}")
        header = (f"# {title}\n\nhttps://www.youtube.com/watch?v={vid}  \n"
                  f"Length {stamp(duration_of(segs))} - captions: {data.get('language', '')} - model: {a.model}\n\n")
        target.write_text(header + body + "\n", encoding="utf-8")
        done.append({"videoId": vid, "title": title, "timestamps_cited": cited})

    (out / "no_captions.txt").write_text("".join(f"{v}\n" for v in no_captions), encoding="utf-8")
    report = {"videos": len(unique), "summarized": len(done), "skipped_existing": len(skipped),
              "no_captions": len(no_captions), "failed": failed, "transcript_credits": credits,
              "model": a.model, "llm_calls": s.calls, "input_tokens": s.input_tokens,
              "output_tokens": s.output_tokens, "llm_cost_usd": round(s.cost, 4),
              "timestamps_out_of_range": flagged}
    if a.estimate:
        per = {m: round(c, 4) for m, c in est_cost.items()}
        report["estimate"] = {"input_tokens": est_in, "llm_cost_usd_by_model": per}
        print(f"\nEstimated input: ~{est_in:,} tokens. LLM cost by model: " +
              ", ".join(f"{m} ${c:.2f}" for m, c in per.items()) + f". Transcript credits used: {credits}.")
    else:
        print(f"\nSummarized {len(done)}, skipped {len(skipped)}, no captions {len(no_captions)}, failed {len(failed)}. "
              f"Transcript credits: {credits}. Claude: {s.input_tokens:,} in / {s.output_tokens:,} out, "
              f"about ${s.cost:.2f}.")
    (out / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
    return 0


if __name__ == "__main__":
    sys.exit(main())
