#!/usr/bin/env python3
"""Pustaka referensi: tarik halaman dokumentasi Meta + perbaiki 2 URL Google yang 404."""
import asyncio, json, re

META = [
    ("Meta Business Help Center", "https://www.facebook.com/business/help"),
    ("Meta Ads Manager: panduan", "https://www.facebook.com/business/tools/ads-manager"),
    ("Meta Pixel", "https://www.facebook.com/business/tools/meta-pixel"),
    ("Conversions API", "https://developers.facebook.com/docs/marketing-api/conversions-api/"),
    ("Marketing API", "https://developers.facebook.com/docs/marketing-apis/"),
    ("Instagram untuk bisnis", "https://business.instagram.com/"),
    ("WhatsApp Business", "https://business.whatsapp.com/"),
    ("Threads untuk bisnis", "https://about.fb.com/news/2023/07/introducing-threads/"),
]
GOOGLE_PERBAIKAN = [
    ("Hasil lokal di Google", "https://developers.google.com/search/docs/appearance/local-business"),
    ("GTM: tag", "https://support.google.com/tagmanager/answer/3281060"),
    ("GTM: pemicu", "https://support.google.com/tagmanager/answer/7679316"),
    ("Penelusuran lokal (local SEO)", "https://developers.google.com/search/docs/specialty/local-business"),
]


async def jalan():
    from playwright.async_api import async_playwright
    hasil = {}
    async with async_playwright() as pw:
        br = await pw.chromium.launch()
        ctx = await br.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
                                   locale="id-ID")
        hal = await ctx.new_page()
        for nama, url in META + [("G:" + n, u) for n, u in GOOGLE_PERBAIKAN]:
            try:
                await hal.goto(url, wait_until="domcontentloaded", timeout=60000)
                await hal.wait_for_timeout(3500)
                judul = await hal.title()
                teks = await hal.inner_text("body")
                teks = re.sub(r"\s+", " ", teks)[:1200]
                heads = []
                try:
                    heads = [h.strip()[:80] for h in await hal.eval_on_selector_all("h1,h2", "els => els.slice(0,10).map(e => e.innerText)")]
                except Exception:
                    pass
                hasil[nama] = {"url": url, "judul": judul[:140], "h": heads, "cuplikan": teks[:500]}
                print(f"   ✅ {nama}: {judul[:80]}")
            except Exception as e:
                hasil[nama] = {"url": url, "error": str(e)[:90]}
                print(f"   ⚠️ {nama}: {str(e)[:70]}")
        await br.close()
    json.dump(hasil, open("/home/mantapman/.hermes/profiles/atun/workspace/drafts/literatur_meta.json", "w"),
              indent=1, ensure_ascii=False)
    print("\n→ drafts/literatur_meta.json")


asyncio.run(jalan())
