#!/usr/bin/env python3
"""Batch verifikasi URL dari luar via check-host.net (banyak node), lapor kalau ada node 200."""
import json, sys, time, urllib.parse, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) Chrome/124 Safari/537.36", "Accept": "application/json"}


def get(url, timeout=90):
    req = urllib.request.Request(url, headers=UA)
    with urllib.request.urlopen(req, timeout=timeout) as r:
        return json.loads(r.read().decode("utf-8", "replace"))


def verdict(u, nodes=6, wait=40):
    q = "https://check-host.net/check-http?host=%s&max_nodes=%d" % (urllib.parse.quote(u, safe=""), nodes)
    try:
        rid = get(q).get("request_id")
    except Exception as e:
        return "REQ-ERR " + str(e)[:60]
    if not rid:
        return "NO-ID"
    time.sleep(wait)
    try:
        res = get("https://check-host.net/check-result/%s" % rid)
    except Exception as e:
        return "RES-ERR " + str(e)[:60]
    codes = []
    for node, v in res.items():
        if not v:
            continue
        it = v[0] if isinstance(v, list) else v
        if isinstance(it, dict):
            codes.append(str(it.get("status_code") or it.get("error") or "?"))
    return "200 OK" if any(c.startswith("200") for c in codes) else "NO-200 (%s)" % ", ".join(sorted(set(codes)))


for u in sys.argv[1:]:
    print("%-95s %s" % (u[:95], verdict(u)))
