import sys,re,subprocess,json,time,concurrent.futures as cf
def chk(d):
    r={'domain':d}
    p=subprocess.run(['curl','-sL','-m','25','-A','Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148','-o','-','-w','\n__META__%{http_code} %{time_total} %{url_effective} %{size_download}','https://'+d],capture_output=True,text=True,errors='ignore')
    out=p.stdout
    if '__META__' not in out: r['reachable']=False; return r
    body,meta=out.rsplit('__META__',1)
    code,t,url,size=meta.split()
    r.update(reachable=code=='200',http=int(code),load_s=round(float(t),2),final_url=url,html_kb=round(int(size)/1024,1),https=url.startswith('https'))
    b=body.lower()
    r['viewport']=bool(re.search(r'<meta[^>]+name=["\']?viewport',b))
    yrs=[int(y) for y in re.findall(r'(?:©|&copy;|copyright)\s*(?:[^<\d]{0,40})?((?:19|20)\d\d)(?:\s*[-–]\s*((?:19|20)\d\d))?',b) for y in [y] ] if False else []
    ys=[]
    for m in re.finditer(r'(?:©|&copy;|&#169;|copyright)[^<]{0,60}',b):
        ys+= [int(x) for x in re.findall(r'(?:19|20)\d\d',m.group(0))]
    r['copyright_year']=max(ys) if ys else None
    r['whatsapp']=bool(re.search(r'wa\.me/|api\.whatsapp\.com|whatsapp://',b))
    r['tel_link']='href="tel:' in b or "href='tel:" in b
    tools=['calendly','simplybook','setmore','acuity','bookwhen','squareup.com/appointments','fresha','youcanbook','zoho bookings','book.drivingschool','drivingschoolsoftware','instructorbook','bookingsplus','wix-bookings','bookings']
    r['booking_tool']=next((t for t in tools if t in b),None)
    r['book_online_text']=bool(re.search(r'book (?:your lessons? )?online|book now|online booking',b))
    r['price_shown']=bool(re.search(r'€\s?\d{2,3}|&euro;\s?\d{2,3}|\d{2,3}\s?(?:euro|€)',b))
    gen=re.search(r'<meta[^>]+name=["\']generator["\'][^>]+content=["\']([^"\']+)',b)
    builder=None
    for k,v in [('wix','wix.com'),('squarespace','squarespace'),('wordpress','wp-content'),('godaddy','godaddy'),('weebly','weebly'),('webflow','webflow'),('jimdo','jimdo'),('joomla','joomla'),('duda','duda'),('shopify','shopify')]:
        if v in b: builder=k;break
    r['platform']=builder or (gen.group(1)[:30] if gen else None)
    r['schema_localbusiness']=bool(re.search(r'"@type"\s*:\s*"(?:localbusiness|drivingschool|educationalorganization|organization|autodealer)"',b))
    r['form']='<form' in b
    return r
ds=[l.strip() for l in open('domains.txt') if l.strip()]
with cf.ThreadPoolExecutor(12) as ex: res=list(ex.map(chk,ds))
json.dump(res,open('raw.json','w'),indent=1)
ok=[x for x in res if x.get('reachable')]
print('total',len(res),'reachable',len(ok))
for x in res:
    if not x.get('reachable'): print('NOT OK',x['domain'],x.get('http'))
