# -*- coding: utf-8 -*-
import pathlib, re, sys, io, html as H
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"
def text(p):
    raw = (CACHE / p).read_text(encoding="utf-8", errors="replace")
    raw = re.sub(r"(?is)<(script|style|nav|footer|header)[^>]*>.*?</\1>", " ", raw)
    raw = re.sub(r"(?is)<br\s*/?>", "\n", raw)
    raw = re.sub(r"(?is)</(p|div|li|h[1-6]|tr|td|th)>", "\n", raw)
    raw = re.sub(r"(?is)<[^>]+>", " ", raw)
    raw = H.unescape(raw)
    raw = re.sub(r"[ \t\xa0]+", " ", raw)
    raw = re.sub(r"\n\s*\n+", "\n", raw)
    return raw.strip()
for k in ["nyu-a3sr-cur", "nu-cs-cur"]:
    t = text(f"r345-{k}.html")
    (BASE / f"_r345_txt_{k}.txt").write_text(t, encoding="utf-8")
    print(k, len(t))
t = text("r345-mcg-mma.html")
for kw in ["Tuition", "tuition", "Curriculum", "credits", "Credits", "Deadline", "deadline", "C$", "Fees"]:
    for m in re.finditer(kw, t):
        print("--", kw, t[max(0,m.start()-160):m.start()+380].replace("\n"," | ")[:540])
        break
