import os, ssl, urllib.request, urllib.error, hashlib, 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 = [
    ("up-c0e9103", "https://catalog.upenn.edu/graduate/programs/data-science-artificial-intelligence-mse/"),
    ("yale-statistics", "https://gsas.yale.edu/programs-of-study/statistics"),
    ("cmu-c2ee442", "https://www.csd.cmu.edu/education/master/index.html"),
    ("notre-ds", "https://graduateschool.nd.edu/degree-programs/data-science-ms---masters-professional/"),
    ("mit-finance", "https://oge.mit.edu/programs/mit-sloan-master-of-finance/"),
]

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'_25_{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[:600])
        print()
    except Exception as e:
        print(f"=== {tag} FAIL {type(e).__name__} {e}")
