#!/usr/bin/env python3
"""Re-measure YouTube thumbnail files for any video ID.

No third-party packages: uses urllib and a small header parser, so it runs on a
plain Python 3 install. Prints the same fields the dataset was built from.

    python measure_thumbnails.py dQw4w9WgXcQ
    python measure_thumbnails.py dQw4w9WgXcQ --names maxresdefault.jpg,hqdefault.jpg
    python measure_thumbnails.py dQw4w9WgXcQ --csv > out.csv

The placeholder response is called out explicitly: a thumbnail name that does
not exist returns HTTP 404 with a small JPEG body, which is easy to mistake for
a real image if you only look at the bytes.
"""
import argparse
import hashlib
import json
import struct
import sys
import urllib.error
import urllib.request

HOST = "https://img.youtube.com/vi/{vid}/{name}"

DEFAULT_NAMES = [
    "maxresdefault.jpg", "hq720.jpg", "sddefault.jpg",
    "hqdefault.jpg", "mqdefault.jpg", "default.jpg",
    "oar1.jpg", "oar2.jpg", "oar3.jpg", "oardefault.jpg",
    "0.jpg", "1.jpg", "2.jpg", "3.jpg",
]


def jpeg_size(data):
    """Return (width, height) from JPEG bytes, or None."""
    if data[:2] != b"\xff\xd8":
        return None
    i, n = 2, len(data)
    while i < n - 9:
        if data[i] != 0xFF:
            i += 1
            continue
        marker = data[i + 1]
        if marker in (0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7,
                      0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF):
            height, width = struct.unpack(">HH", data[i + 5:i + 9])
            return width, height
        if marker in (0xD8, 0xD9) or 0xD0 <= marker <= 0xD7:
            i += 2
            continue
        seg = struct.unpack(">H", data[i + 2:i + 4])[0]
        i += 2 + seg
    return None


def webp_size(data):
    """Return (width, height) from WebP bytes, or None."""
    if data[:4] != b"RIFF" or data[8:12] != b"WEBP":
        return None
    fmt = data[12:16]
    if fmt == b"VP8 ":
        w = struct.unpack("<H", data[26:28])[0] & 0x3FFF
        h = struct.unpack("<H", data[28:30])[0] & 0x3FFF
        return w, h
    if fmt == b"VP8L":
        bits = struct.unpack("<I", data[21:25])[0]
        return (bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1
    if fmt == b"VP8X":
        w = int.from_bytes(data[24:27], "little") + 1
        h = int.from_bytes(data[27:30], "little") + 1
        return w, h
    return None


def measure(vid, name):
    url = HOST.format(vid=vid, name=name)
    req = urllib.request.Request(url, headers={"User-Agent": "thumbnail-measure/1.0"})
    try:
        with urllib.request.urlopen(req, timeout=20) as resp:
            body = resp.read()
            status = resp.status
            headers = dict(resp.headers)
    except urllib.error.HTTPError as exc:
        body = exc.read()
        status = exc.code
        headers = dict(exc.headers)
    except Exception as exc:                       # noqa: BLE001
        return {"file": name, "error": str(exc)}

    ctype = headers.get("Content-Type", "")
    size = None
    if "jpeg" in ctype or "jpg" in ctype:
        size = jpeg_size(body)
    elif "webp" in ctype:
        size = webp_size(body)

    return {
        "file": name,
        "http_status": status,
        "bytes": len(body),
        "width": size[0] if size else None,
        "height": size[1] if size else None,
        "content_type": ctype,
        "cache_control": headers.get("Cache-Control"),
        "accept_ranges": headers.get("Accept-Ranges"),
        "cors": headers.get("Access-Control-Allow-Origin"),
        "etag": headers.get("ETag"),
        "last_modified": headers.get("Last-Modified"),
        "content_disposition": headers.get("Content-Disposition"),
        "sha1": hashlib.sha1(body).hexdigest()[:16],
    }


def main():
    ap = argparse.ArgumentParser(description=__doc__)
    ap.add_argument("video_id")
    ap.add_argument("--names", help="comma-separated filenames (default: common set)")
    ap.add_argument("--csv", action="store_true", help="print CSV instead of JSON")
    args = ap.parse_args()

    names = args.names.split(",") if args.names else DEFAULT_NAMES
    rows = [measure(args.video_id, n) for n in names]

    if args.csv:
        cols = ["file", "http_status", "bytes", "width", "height",
                "content_type", "cache_control", "accept_ranges", "cors", "sha1"]
        print(",".join(cols))
        for r in rows:
            print(",".join(str(r.get(c, "")) for c in cols))
    else:
        print(json.dumps({"video_id": args.video_id, "rows": rows}, indent=1))

    missing = [r for r in rows if r.get("http_status") == 404]
    if missing:
        print("\n# %d of %d names returned 404 (placeholder body, not a real "
              "thumbnail): %s" % (len(missing), len(rows),
                                  ", ".join(r["file"] for r in missing)),
              file=sys.stderr)


if __name__ == "__main__":
    main()
