# -*- coding: utf-8 -*-
import os, re, sys, json, time
import requests
base = r"D:\work buddy\2026-11-05-14-15-52"
cache = os.path.join(base, "cache")
os.makedirs(cache, exist_ok=True)
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36",
     "Accept-Language": "en-US,en;q=0.9"}
urls = sys.argv[1:]
for u in urls:
    import hashlib
    key = "r349-" + re.sub(r'[^A-Za-z0-9]+', '-', u.replace("https://", "").replace("http://", ""))[:60] + "-" + hashlib.md5(u.encode()).hexdigest()[:6]
    p = os.path.join(cache, key + ".html")
    st = None
    if os.path.exists(p) and os.path.getsize(p) > 2000:
        st = "CACHE"
        txt = open(p, encoding="utf-8", errors="ignore").read()
    else:
        try:
            r = requests.get(u, headers=H, timeout=12, allow_redirects=True)
            st = r.status_code
            txt = r.text
            if st == 200:
                open(p, "w", encoding="utf-8").write(txt)
        except Exception as e:
            st = "ERR " + type(e).__name__ + ":" + str(e)[:40]
            txt = ""
    plain = re.sub(r'<script.*?</script>|<style.*?</style>', ' ', txt, flags=re.S | re.I)
    plain = re.sub(r'<[^>]+>', ' ', plain)
    plain = re.sub(r'&nbsp;', ' ', plain)
    plain = re.sub(r'\s+', ' ', plain).strip()
    codes = sorted(set(re.findall(r'\b[A-Z]{2,8}[\s\-\.]?\d{3,4}[A-Z]?\b', plain)))[:12]
    print(u, "|", st, "|", len(txt), "|", len(plain), "|", codes)
