"""Lightpanda 1.0.0 on the 29 homepages in sites.txt. Run on October 3, 2026, from Mumbai, on an Apple M3 Mac."""
import json, subprocess, re
from bs4 import BeautifulSoup
sites=open("sites.txt").read().split()
res=[]
for d in sites:
    url=f"https://{d}/"
    try:
        p=subprocess.run(["/usr/bin/time","-l","lightpanda","fetch","--dump","html",url],capture_output=True,text=True,timeout=60)
        mem=re.search(r"(\d+)\s+maximum resident set size",p.stderr); real=re.search(r"([\d.]+) real",p.stderr)
        s=BeautifulSoup(p.stdout,"html.parser")
        for t in s(["script","style","noscript"]): t.decompose()
        w=len((s.body.get_text(" ") if s.body else "").split())
        r={"domain":d,"words":w,"mb":round(int(mem.group(1))/1048576) if mem else None,"s":float(real.group(1)) if real else None,"exit":p.returncode}
    except subprocess.TimeoutExpired: r={"domain":d,"words":0,"error":"timeout"}
    res.append(r); print(json.dumps(r),flush=True)
json.dump(res,open("lp_real.json","w"),indent=1)
