#!/usr/bin/env python3
"""Uji beberapa proxy publik untuk fetch artikel klien dari luar."""
import sys, time, urllib.parse

import subprocess

U = sys.argv[1] if len(sys.argv) > 1 else "https://dasindonesia.com/kompresi-mesin-turun-panduan-lengkap/"
enc = urllib.parse.quote(U, safe="")

PROXIES = [
    ("codetabs", "https://api.codetabs.com/v1/proxy?quest=" + enc),
    ("allorigins", "https://api.allorigins.win/raw?url=" + enc),
    ("corsproxy.io", "https://corsproxy.io/?url=" + enc),
    ("cors.lol", "https://api.cors.lol/?url=" + enc),
    ("thingproxy", "https://thingproxy.freeboard.io/fetch/" + U),
    ("whateverorigin", "http://www.whateverorigin.org/get?url=" + enc),
    ("jina", "https://r.jina.ai/" + U),
    ("textance", "https://api.textance.com/?url=" + enc),
    ("urltotext", "https://urlreader.p.rapidapi.com"),
]

for name, p in PROXIES:
    try:
        r = subprocess.run(["curl", "-s", "-o", "/tmp/px.bin", "-w", "%{http_code} %{size_download} %{time_total}",
                            "--max-time", "35", p], capture_output=True, text=True, timeout=45)
        out = r.stdout.strip()
        head = open("/tmp/px.bin", "rb").read()[:120].decode("utf-8", "replace").replace("\n", " ")
        print("%-15s -> %s | %s" % (name, out, head))
    except Exception as e:
        print("%-15s -> ERR %s" % (name, str(e)[:60]))
    time.sleep(1)
