#!/usr/bin/env python3
"""Do stored google.com/goto links still resolve weeks later? Takes Cloro's redirectLink values from older
stored runs (Cloro returns the destination beside the goto link, so the answer can be checked), follows a
fixed-seed sample at one request a second and compares the final host with Cloro's link. No provider call.
Usage: scripts/check-goto-link-age.py <out.json> [--per-run N] label=dir [label=dir ...]"""
import json, sys, os, glob, random, re, time, http.client
from urllib.parse import urlparse, urljoin
args = sys.argv[1:]; out = args.pop(0); N = 20
if args and args[0] == '--per-run': args.pop(0); N = int(args.pop(0))
UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36'
def host(u):
    h = urlparse(u).netloc.lower() if u else ''
    return h[4:] if h.startswith('www.') else h
def fetch(u):
    p = urlparse(u); c = http.client.HTTPSConnection(p.netloc, timeout=20)
    c.request('GET', p.path + ('?' + p.query if p.query else ''), headers={'User-Agent': UA}); r = c.getresponse()
    loc = r.getheader('Location'); s = r.status; c.close(); return s, loc
random.seed(20261102); res = {}
for a in args:
    label, d = a.split('=', 1); rows = []
    for f in sorted(glob.glob(os.path.join(d, 'serp-cloro-*.json'))):
        r = json.load(open(f))['response']
        for i in ((r.get('result') or {}).get('organicResults') or []):
            if re.match(r'https?://(www\.)?google\.[a-z.]+/goto\b', i.get('redirectLink') or ''): rows.append(i)
    sample = random.sample(rows, min(N, len(rows))); ok = same = 0; other = {}
    for row in sample:
        u = row['redirectLink']; final = None; last = None
        try:
            for _ in range(4):
                last, loc = fetch(u)
                if last in (301, 302, 303, 307, 308) and loc:
                    u = urljoin(u, loc)
                    if not host(u).startswith('google.'): final = u; break
                else: break
        except Exception as e: last = type(e).__name__
        if final:
            ok += 1; same += host(final) == host(row.get('link'))
        else: other[str(last)] = other.get(str(last), 0) + 1
        time.sleep(1)
    res[label] = {'goto_links_in_run': len(rows), 'sampled': len(sample), 'resolved': ok, 'same_host_as_cloro_link': same, 'not_resolved_last_status': other}
json.dump(res, open(out, 'w'), indent=1); print(json.dumps(res, indent=1))
