#!/usr/bin/env python3
"""Baca dokumentasi API v3 Semrush untuk mendapatkan format permintaan yang benar."""
import html as H, json, 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"}

for u in ("https://developer.semrush.com/api/v3/", "https://developer.semrush.com/api/v3/analytics/",
          "https://developer.semrush.com/api/v3/analytics/domain-reports/", "https://www.semrush.com/api-documentation/"):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60)
        h = r.read().decode("utf-8", "replace")
    except Exception as e:
        print(f"\n=== {u} | gagal {str(e)[:60]}")
        continue
    print(f"\n=== {u} | {len(h):,} byte")
    teks = H.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", h)))
    # cari contoh URL API & nama parameter
    for pola in (r"https://api\.semrush\.com[^\s\"'<>]{0,160}",
                 r"(?:type|report|target|database|key|export_columns)=\w+",
                 r"[^.]{0,120}(?:curl|GET |Authorization|api_key)[^.]{0,160}\."):
        temuan = sorted(set(re.findall(pola, teks, re.I)))[:6]
        for t in temuan:
            print("   •", re.sub(r"\s+", " ", t)[:200])
json.dump({}, open("/dev/null", "w"))
