"""Fetch public YouTube oEmbed metadata; this does not watch video contents."""
import json
from pathlib import Path
from urllib.request import Request, urlopen
from urllib.parse import urlencode

VIDEOS = [
    ("EjsB0WgIfUM", "Poda I", "https://hanlab.mit.edu/courses/2024-fall-65940"),
    ("upaZrpXkELc", "Poda II", "https://hanlab.mit.edu/courses/2024-fall-65940"),
    ("ymAzUz3qlIA", "Cuantización I", "https://hanlab.mit.edu/courses/2024-fall-65940"),
    ("wrcgWm_nUeE", "Cuantización II", "https://hanlab.mit.edu/courses/2024-fall-65940"),
    ("Ubj3QXv4rjw", "Destilación", "https://hanlab.mit.edu/courses/2024-fall-65940"),
    ("rNgUoH7Wbv8", "SFT para agentes", "https://huggingface.co/blog/sergiopaniego/rl-environments-2026"),
    ("GZT_oNQnLV4", "Destilación de agentes", "https://huggingface.co/sergiopaniego/pi-mono-youtube-livestream-2-scripts"),
    ("ztdTed5egrM", "RL para agentes", "https://huggingface.co/blog/sergiopaniego/rl-environments-2026"),
    ("nJV3yUuz6DU", "Entornos y recompensas", "https://huggingface.co/blog/sergiopaniego/rl-environments-2026"),
]
TRANSCRIPTS = {
    "GZT_oNQnLV4": {"retrieved_segments": 1768, "reviewed_seconds": [[1100,1300],[1560,1810]]},
    "Ubj3QXv4rjw": {"retrieved_segments": 1186, "reviewed_seconds": [[0,150],[300,470]]},
}
rows = []
for video_id, topic, companion in VIDEOS:
    url = "https://www.youtube.com/watch?v=" + video_id
    row = {"id": video_id, "topic": topic, "url": url, "companion_or_author_index": companion,
           "visual_frames_reviewed": False, "full_video_watched": False,
           "transcript": TRANSCRIPTS.get(video_id, {"status": "not_retrieved"})}
    try:
        request = Request("https://www.youtube.com/oembed?" + urlencode({"url":url,"format":"json"}), headers={"User-Agent":"OrnithResearch/1.0"})
        with urlopen(request, timeout=25) as response:
            metadata = json.load(response)
        row.update({"metadata_status":"verified", "title": metadata["title"], "author": metadata["author_name"]})
    except Exception as exc:
        row.update({"metadata_status":"unavailable", "error":type(exc).__name__ + ": " + str(exc)})
    rows.append(row)
report = {"checked_date":"2026-09-23", "unique_video_ids":len(rows), "metadata_verified":sum(r["metadata_status"]=="verified" for r in rows), "transcripts_retrieved":len(TRANSCRIPTS), "sources":rows}
Path(__file__).with_name("sources-videos.json").write_text(json.dumps(report, ensure_ascii=False, indent=2)+"\n")
print(json.dumps(report, ensure_ascii=False, indent=2))
