import json, subprocess, urllib.request, urllib.error, ssl, time, sys

paths = []
with open('/var/www/html/ARTEX-main/data/tasks/1/i4/api_paths.txt') as f:
    for line in f:
        p = line.strip().strip('"')
        if p:
            paths.append(p)

# dedupe trailing slash
seen = set()
uniq = []
for p in paths:
    key = p.rstrip('/')
    if key not in seen:
        seen.add(key)
        uniq.append(p)
paths = uniq
print(f"unique paths: {len(paths)}", file=sys.stderr)

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

def req(method, path, body=None):
    url = "https://www.fansky.net" + path
    data = None
    headers = {"Accept":"application/json","User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"}
    if body is not None:
        data = json.dumps(body).encode()
        headers["Content-Type"]="application/json"
    r = urllib.request.Request(url, data=data, headers=headers, method=method)
    try:
        resp = urllib.request.urlopen(r, timeout=15, context=ctx)
        code = resp.status
        b = resp.read(400)
        return code, b
    except urllib.error.HTTPError as e:
        b = e.read(400)
        return e.code, b
    except Exception as e:
        return -1, str(e).encode()

results = []
for i, p in enumerate(paths):
    code, b = req("GET", p)
    bstr = b.decode('utf-8','replace')
    results.append((p, "GET", code, bstr[:120]))
    if i % 25 == 0:
        print(f"progress {i}/{len(paths)}", file=sys.stderr)

with open("sweep_get.json","w") as f:
    json.dump(results, f)

# print interesting (non-401, non-404) GET results
for p, m, code, b in results:
    if code != 401 and code != 404:
        print(f"[{code}] GET {p}  -> {b}")
