#!/usr/bin/env python3
"""Bedah HTML reel Instagram yang sudah diunduh: cari judul, deskripsi, dan nama pengguna."""
import json, re

t = open("/tmp/ig_reel.html", encoding="utf-8", errors="replace").read()
print("panjang:", len(t))

for kunci in ("og:title", "og:description", "twitter:title", "twitter:description", "description"):
    for m in re.finditer(r'<meta[^>]*(?:property|name)="' + re.escape(kunci) + r'"[^>]*>', t):
        print(f"[{kunci}] {m.group(0)[:400]}")

m = re.search(r'"username"\s*:\s*"([^"]+)"', t)
print("username:", m.group(1) if m else "-")
for m in re.finditer(r'"caption"\s*:\s*"(.{0,400}?)"', t):
    print("[caption]", m.group(1)[:400])
for m in re.finditer(r'"accessibility_caption"\s*:\s*"(.{0,300}?)"', t):
    print("[aksesibilitas]", m.group(1)[:300])
m = re.search(r'"video_duration"\s*:\s*([\d.]+)', t)
print("durasi video:", m.group(1) if m else "-")
m = re.search(r'"like_count"\s*:\s*(\d+)', t)
print("suka:", m.group(1) if m else "-")
m = re.search(r'"comment_count"\s*:\s*(\d+)', t)
print("komentar:", m.group(1) if m else "-")
m = re.search(r'"video_view_count"\s*:\s*(\d+)', t)
print("tayangan:", m.group(1) if m else "-")
m = re.search(r'"taken_at"\s*:\s*(\d+)', t)
print("waktu unggah (epoch):", m.group(1) if m else "-")

# bagian teks yang bisa dibaca manusia
teks = re.sub(r"<script.*?</script>|<style.*?</style>", " ", t, flags=re.S)
teks = re.sub("<[^>]+>", " ", teks)
teks = re.sub(r"\s+", " ", teks).strip()
print("\nteks halaman:", teks[:600])
