# -*- coding: utf-8 -*-
import io, sys, os, ssl, urllib.request, re, html as ihtml
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

SCTX = ssl.create_default_context()
SCTX.check_hostname = False
SCTX.verify_mode = ssl.CERT_NONE

def fetch(url, timeout=20):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    r = urllib.request.urlopen(req, timeout=timeout, context=SCTX)
    return r.read().decode("utf-8", errors="ignore")

slugs = [
    ("UT Forestry", "https://www.sgs.utoronto.ca/programs/forestry/"),
    ("UT Nursing", "https://www.sgs.utoronto.ca/programs/nursing/"),
    ("UT Dentistry", "https://www.sgs.utoronto.ca/programs/dentistry/"),
]

for name, url in slugs:
    print("="*70)
    print(name)
    try:
        h = fetch(url)
    except Exception as e:
        print("ERR:", e); continue
    m = re.search(r"<title>([^<]+)</title>", h)
    if m: print("TITLE:", ihtml.unescape(m.group(1).strip()))
    qf = re.search(r"Quick Facts([\s\S]{0,6000})", h)
    if qf:
        rows = re.findall(r"<tr[^>]*>(.*?)</tr>", qf.group(1), re.S)
        for row in rows[:15]:
            cells = re.findall(r"<t[dh][^>]*>(.*?)</t[dh]>", row, re.S)
            cells = [re.sub(r"<[^>]+>", "", c).strip() for c in cells]
            cells = [ihtml.unescape(c) for c in cells if c]
            if cells: print("  ROW:", " | ".join(cells)[:240])
    ov = re.search(r"Program Overview([\s\S]{0,1800})", h)
    if ov:
        txt = re.sub(r"<[^>]+>", " ", ov.group(1))
        txt = re.sub(r"\s+", " ", ihtml.unescape(txt)).strip()
        print("  OV:", txt[:300])
