# -*- coding: utf-8 -*-
import requests, re, sys, io, pathlib
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52"); CACHE = BASE / "cache"
H = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36",
     "Accept-Language": "en-US,en;q=0.9"}
def fetch(tag, url):
    p = CACHE / (tag + ".html")
    if p.exists() and p.stat().st_size > 2000:
        return p.read_text(encoding="utf-8", errors="ignore"), "cache"
    try:
        r = requests.get(url, headers=H, timeout=22, allow_redirects=True)
    except Exception as e:
        return "", "ERR " + type(e).__name__
    p.write_text(r.text, encoding="utf-8", errors="ignore"); return r.text, str(r.status_code)
def text(h):
    h = re.sub(r"<script.*?</script>|<style.*?</style>", " ", h, flags=re.S)
    h = re.sub(r"<[^>]+>", " ", h); return re.sub(r"\s+", " ", h)

jobs = [
 ("r346-vandy-msds", "https://computing.vanderbilt.edu/msds/"),
 ("r346-vandy-msds-cur", "https://computing.vanderbilt.edu/msds/curriculum/"),
 ("r346-vandy-msds-adm", "https://computing.vanderbilt.edu/msds/admissions/"),
 ("r346-vandy-msds-tui", "https://computing.vanderbilt.edu/msds/tuition/"),
]
for tag, u in jobs:
    h, st = fetch(tag, u)
    t = text(h)
    (BASE / ("_" + tag + ".txt")).write_text(t, encoding="utf-8")
    print(tag, st, len(t), "|", t[:90])
