#!/usr/bin/env python3
"""Buka reel Instagram dengan browser sungguhan (playwright) dan ambil isi halamannya."""
import re, sys, time

from playwright.sync_api import sync_playwright

URL = "https://www.instagram.com/reel/DeaOaDAzcMQ/"
with sync_playwright() as p:
    b = p.chromium.launch(args=["--no-sandbox", "--disable-dev-shm-usage"])
    ctx = b.new_context(locale="id-ID", user_agent=(
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
        "(KHTML, like Gecko) Chrome/124.0 Safari/537.36"))
    hal = ctx.new_page()
    try:
        hal.goto(URL, wait_until="domcontentloaded", timeout=90000)
    except Exception as e:
        print("goto:", str(e)[:80])
    hal.wait_for_timeout(8000)
    print("judul halaman:", hal.title()[:120])
    for meta in ("og:title", "og:description", "description"):
        try:
            v = hal.get_attribute(f'meta[property="{meta}"]', "content") or \
                hal.get_attribute(f'meta[name="{meta}"]', "content")
            if v:
                print(f"[{meta}] {v[:400]}")
        except Exception:
            pass
    teks = hal.inner_text("body")[:1500].replace("\n", " | ")
    print("teks badan:", teks[:1200])
    hal.screenshot(path="/tmp/ig_reel.png", full_page=False)
    print("tangkapan layar: /tmp/ig_reel.png")
    b.close()
