llmstxt-corpus / code /crawl_shopify.py
jacobezra93's picture
Mirror the Zenodo deposit
bdc09d3 verified
Raw History Blame Contribute Delete
2.07 kB
import urllib.request, ssl, concurrent.futures as cf, sys
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
UA="Mozilla/5.0 (compatible; SempiteResearch/1.0; +https://sempite.com/research/)"
def classify(dom):
url=f"https://{dom}/llms.txt"
try:
req=urllib.request.Request(url,headers={"User-Agent":UA})
r=urllib.request.urlopen(req,timeout=8,context=ctx)
st=r.status; ct=(r.headers.get("content-type","") or "").lower()
hdrs=" ".join(k.lower() for k in r.headers.keys())
shopify = ("x-shopify" in hdrs) or ("x-sorting-hat" in hdrs) or ("shopify" in (r.headers.get("server","") or "").lower())
body=r.read(6000).decode("utf-8","replace")
low=body.lower()
if "html" in ct or "<html" in low[:400]:
kind="html_masquerade"
elif len(body.strip())<25:
kind="empty"
elif ("agent instructions" in low) or ("ucp.dev" in low) or ("create_checkout" in low) or ("shop.app/skill" in low) or ("universal commerce protocol" in low):
kind="ucp_template"
else:
kind="content_map"
return (dom,st,kind,len(body),int(shopify),ct.split(";")[0])
except urllib.error.HTTPError as e:
return (dom,e.code,"http_error",0,0,"")
except Exception as e:
return (dom,0,"ERR_"+type(e).__name__,0,0,"")
def run(infile,outfile,label):
doms=[l.strip() for l in open(infile) if l.strip()]
n=len(doms); done=0
with open(outfile,"w") as out, cf.ThreadPoolExecutor(max_workers=30) as ex:
out.write("domain\tstatus\tkind\tbytes\tshopify_hdr\tcontent_type\n")
for res in ex.map(classify,doms):
out.write("\t".join(str(x) for x in res)+"\n")
done+=1
if done%500==0: out.flush(); sys.stderr.write(f"[{label}] {done}/{n}\n"); sys.stderr.flush()
sys.stderr.write(f"[{label}] DONE {n}\n")
run("shopify-targets.txt","shopify-results.tsv","custom")
run("myshopify-sample.txt","myshopify-results.tsv","mysh")
sys.stderr.write("ALL DONE\n")