import os, ssl, urllib.request, urllib.error, html, re

BASE = os.path.dirname(os.path.abspath(__file__))
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

targets = [
    ("mit-msms", "https://oge.mit.edu/programs/mit-sloan-master-of-science-in-management-studies/"),
    ("mit-scm-res", "https://oge.mit.edu/programs/supply-chain-management-residential/"),
    ("rice-macc", "https://ga.rice.edu/programs-study/departments-programs/business/accounting"),
    ("rice-meecon", "https://ga.rice.edu/programs-study/departments-programs/social-sciences/energy-economics"),
    ("rice-mfin", "https://ga.rice.edu/programs-study/departments-programs/business/finance"),
    ("wustl-cyber", "https://bulletin.wustl.edu/grad/engineering/degrees/sever-cybersecurity-management-ms/"),
]

def fetch(url):
    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'})
    with urllib.request.urlopen(req, timeout=12, context=ctx) as r:
        b = r.read()
        return r.status, b

for tag, url in targets:
    try:
        st, b = fetch(url)
        raw = b.decode('utf-8', errors='replace')
        open(os.path.join(BASE, f'_26_{tag}.txt'), 'w', encoding='utf-8').write(raw)
        txt = re.sub(r'<script[\s\S]*?</script>', ' ', raw)
        txt = re.sub(r'<style[\s\S]*?</style>', ' ', txt)
        txt = re.sub(r'<[^>]+>', ' ', txt)
        txt = html.unescape(re.sub(r'\s+', ' ', txt)).strip()
        print(f"=== {tag} status={st} bytes={len(b)} textlen={len(txt)}")
        print(txt[:500])
        print()
    except Exception as e:
        print(f"=== {tag} FAIL {type(e).__name__} {e}")
