#!/usr/bin/env python3
"""Uji sumber SERP alternatif yang mengindeks hasil Google (Startpage, Brave, Ecosia, Mojeek)."""
import 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",
      "Accept-Language": "id-ID,id;q=0.9"}
Q = "jasa seo surabaya"
enc = urllib.parse.quote_plus(Q)

SUMBER = [
    ("startpage", f"https://www.startpage.com/sp/search?query={enc}&language=indonesian"),
    ("brave", f"https://search.brave.com/search?q={enc}"),
    ("ecosia", f"https://www.ecosia.org/search?q={enc}"),
    ("mojeek", f"https://www.mojeek.com/search?q={enc}"),
    ("searx", f"https://searx.be/search?q={enc}&format=json"),
]
for nama, url in SUMBER:
    try:
        r = urllib.request.Request(url, headers=UA)
        with urllib.request.urlopen(r, timeout=50) as x:
            b = x.read().decode("utf-8", "replace")
        dom = re.findall(r'https?://(?:www\.)?([a-z0-9.-]+\.[a-z]{2,})/', b)
        dom = [d for d in dom if not any(k in d for k in ("startpage", "brave", "ecosia", "mojeek", "google", "gstatic",
                                                          "bing", "cloudflare", "w3.org", "schema.org", "searx"))]
        unik = list(dict.fromkeys(dom))[:10]
        print(f"  {nama:<10} HTTP {x.status} | {len(b):>7} char | domain terbaca: {unik[:6]}")
        open(f"/tmp/serp_{nama}2.html", "w").write(b)
    except Exception as e:
        print(f"  {nama:<10} GAGAL: {str(e)[:70]}")
