# -*- coding: utf-8 -*-
import sys, io, os, requests, re, concurrent.futures as cf
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36"}
URLS = {
 "wlu-cal-grad": "https://uwaterloo.ca/graduate-studies-academic-calendar",
 "wlu-cal-cs": "https://uwaterloo.ca/graduate-studies-academic-calendar/mathematics/computer-science",
 "wlu-cal-stat": "https://uwaterloo.ca/graduate-studies-academic-calendar/mathematics/statistics-and-actuarial-science",
 "wlu-fin-tuition": "https://uwaterloo.ca/finance/student-financial-services/tuition-and-fees",
 "wlu-fin-grad": "https://uwaterloo.ca/finance/graduate-tuition",
 "wlu-gs-tuition": "https://uwaterloo.ca/graduate-studies-post-doctoral-studies/tuition",
}
def go(kv):
    k, u = kv
    try:
        r = requests.get(u, headers=H, timeout=22, allow_redirects=True)
        b = r.text
        open(BASE + "\\cache\\r348-" + k + ".html", "w", encoding="utf-8", errors="replace").write(b)
        return k, r.status_code, len(b), r.url
    except Exception as e:
        return k, "ERR", str(e)[:70], u
with cf.ThreadPoolExecutor(max_workers=6) as ex:
    for k, st, ln, u in ex.map(go, URLS.items()):
        print(f"{st} {ln:7} {k:18s} {r if False else u}")

# Brown masters
p = BASE + r"\cache\r348-brown-masters.html"
if os.path.exists(p):
    h = open(p, encoding="utf-8", errors="replace").read()
    h = re.sub(r"<script.*?</script>", " ", h, flags=re.S)
    h = re.sub(r"<style.*?</style>", " ", h, flags=re.S)
    h = re.sub(r"<[^>]+>", "\n", h)
    h = re.sub(r"&nbsp;", " ", h); h = re.sub(r"&amp;", "&", h)
    h = re.sub(r"\n\s*\n+", "\n", h)
    open(BASE + r"\_r348-new-brown-masters.txt", "w", encoding="utf-8").write(h)
    print("\nBROWN chars", len(h))
    print(re.sub(r"\s+", " ", h)[1000:5000])
