#!/usr/bin/env python3
"""Kandidat keyword + uji cara membaca SERP (Google lewat proxy, Bing, DDG lite)."""
import json, re, urllib.parse, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124 Safari/537.36"}
data = json.load(open("drafts/ss_keyword_nilai.json"))

print("=== KANDIDAT: impresi ≥ 15, posisi ≤ 30, niat komersial (paling dekat ke halaman 1) ===")
kand = [h for h in data if h["imp"] >= 15 and h["pos"] <= 30]
kand.sort(key=lambda x: x["nilai"], reverse=True)
for h in kand[:30]:
    print(f"  nilai {h['nilai']:>3} | imp {h['imp']:>4} | pos {h['pos']:>5} | {h['query'][:56]:<58} | {h['halaman'][:34]}")
print(f"  total kandidat: {len(kand)}")

print("\n=== UJI AKSES SERP ===")
contoh = kand[0]["query"] if kand else "jasa seo surabaya"
enc = urllib.parse.quote_plus(contoh)
uji = [
    ("google-jina", f"https://r.jina.ai/https://www.google.com/search?q={enc}&hl=id&num=20"),
    ("bing", f"https://www.bing.com/search?q={enc}&setlang=id&count=20"),
    ("ddg-lite", f"https://lite.duckduckgo.com/lite/?q={enc}"),
]
for nama, url in uji:
    try:
        r = urllib.request.Request(url, headers={**UA, "Accept-Language": "id-ID,id;q=0.9"})
        with urllib.request.urlopen(r, timeout=60) as x:
            b = x.read().decode("utf-8", "replace")
        print(f"  {nama:<12} HTTP {x.status} | {len(b):>7} char | kemunculan 'klinikmetamorf'/'http': {b.count('http')}")
        open(f"/tmp/serp_{nama}.html", "w").write(b)
    except Exception as e:
        print(f"  {nama:<12} GAGAL: {str(e)[:70]}")
