#!/usr/bin/env python3
"""Cek dokumentasi Ahrefs API: format token & syarat langganan (agar laporan ke Bos tepat)."""
import html as H, re, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36"}
KANDIDAT = [
    ("docs api intro", "https://docs.ahrefs.com/docs/api/reference/introduction"),
    ("docs api authentication", "https://docs.ahrefs.com/docs/api/reference/authentication"),
    ("halaman API Ahrefs", "https://ahrefs.com/api"),
    ("bantuan API", "https://help.ahrefs.com/en/articles/5959377-how-to-get-an-api-key"),
]
for nama, u in KANDIDAT:
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60)
        h = r.read().decode("utf-8", "replace")
        teks = H.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", h)))
        print(f"\n=== {nama} | status {r.status} | {len(h):,} byte ===")
        for pola in (r"[^.]{0,140}(?:Bearer|api key|API key|token)[^.]{0,160}\.",
                     r"[^.]{0,140}(?:subscription|plan|Enterprise|add-on)[^.]{0,140}\."):
            for m in re.findall(pola, teks, re.I)[:3]:
                print("   •", re.sub(r"\s+", " ", m).strip()[:260])
    except urllib.error.HTTPError as e:
        print(f"\n=== {nama} | HTTP {e.code} ===")
    except Exception as e:
        print(f"\n=== {nama} | gagal {str(e)[:70]} ===")
