# -*- coding: utf-8 -*-
import urllib.request, ssl, re, io, sys, os, json
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):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    r = urllib.request.urlopen(req, timeout=20, context=SCTX)
    return r.read().decode("utf-8", errors="replace")

def clean(html):
    t = re.sub(r"<script[\s\S]*?</script>", "", html)
    t = re.sub(r"<style[\s\S]*?</style>", "", t)
    t = re.sub(r"<[^>]+>", " ", t)
    t = re.sub(r"&#8217;", "'", t)
    t = re.sub(r"&#8211;", "-", t)
    t = re.sub(r"&amp;", "&", t)
    t = re.sub(r"\s+", " ", t)
    return t

def extract(url, label):
    html = fetch(url)
    text = clean(html)
    out = {"url": url, "label": label}
    for kw in ["requires", "credit", "thesis", "comprehensive", "degree", "option", "track", "concentration", "specialization", "admission", "department", "research", "curriculum"]:
        idx = text.lower().find(kw.lower())
        if idx >= 0:
            snippet = text[max(0,idx-60):idx+400]
            out[kw] = snippet
            break
    return out

items = [
    ("nyu-ds", "https://bulletins.nyu.edu/graduate/arts-science/programs/data-science-ms/"),
    ("nyu-chem", "https://bulletins.nyu.edu/graduate/arts-science/programs/chemistry-ms/"),
    ("nyu-skel", "https://bulletins.nyu.edu/graduate/arts-science/programs/human-skeletal-biology-ms/"),
    ("nyu-iren", "https://bulletins.nyu.edu/graduate/arts-science/programs/international-relations-ma/"),
    ("nyu-polish", "https://bulletins.nyu.edu/graduate/arts-science/programs/politics-ma/"),
]

for tag, url in items:
    try:
        r = extract(url, tag)
        print(f"\n===== {tag} =====")
        for k, v in r.items():
            if k in ("url","label"): continue
            print(f"[{k}] {v[:350]}")
    except Exception as e:
        print(f"{tag} ERROR: {e}")
