# -*- coding: utf-8 -*-
import urllib.request, ssl, re, sys, io, os
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
slugs = [
    ("regulatory-science-msrs", "up-msrs"),
    ("medical-physics-mmp", "up-msmp"),
    ("nonprofit-ngo-leadership-msnpl", "up-msnpl"),
    ("criminology-ms", "up-crim-ms"),
    ("translational-research-mstr", "up-mstr"),
]
out = open("_extract84_out.txt", "w", encoding="utf-8")
for sl, pid in slugs:
    url = f"https://catalog.upenn.edu/graduate/programs/{sl}/"
    req = urllib.request.Request(url, headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language":"en-US,en;q=0.9",
        "Referer":"https://catalog.upenn.edu/graduate/programs/",
    })
    try:
        with urllib.request.urlopen(req, context=SCTX, timeout=25) as r:
            body = r.read().decode("utf-8", errors="ignore")
        out.write(f"\n========== {pid} | {sl} | {len(body)}B ==========\n")
        # school
        m = re.search(r'(School of Engineering and Applied Science|Weitzman School of Design|Graduate School of Education|School of Social Policy and Practice|Perelman School of Medicine|College of Liberal and Professional Studies|School of Arts and Sciences|Wharton School|Annenberg School|School of Nursing|School of Design)', body, re.IGNORECASE)
        out.write(f"COLLEGE: {m.group(1) if m else '?'}\n")
        # description after h1
        m = re.search(r'<h1[^>]*>[\s\S]*?</h1>([\s\S]{0,2500}?)(?:<h2|<h3|Core|Required|Curriculum|Degree)', body, re.IGNORECASE)
        if m:
            txt = re.sub(r'<[^>]+>', ' ', m.group(1))
            txt = re.sub(r'\s+', ' ', txt).strip()
            out.write(f"DESC: {txt[:1200]}\n")
        # credits
        m = re.search(r'(\d+)\s*course\s*units', body, re.IGNORECASE)
        out.write(f"CU: {m.group(0) if m else '?'}\n")
        # format
        for kw in ["on-campus", "online", "part-time", "full-time", "working professional", "evening"]:
            if re.search(kw, body, re.IGNORECASE):
                out.write(f"FORMAT: {kw}\n")
        # STEM
        if re.search(r'STEM[- ]?(?:certified|designated|eligible|OPT)?', body, re.IGNORECASE):
            out.write("STEM: YES(mention)\n")
        # requirements
        m = re.search(r'(?:Required Courses|Core Courses|Degree Requirements|Curriculum)([\s\S]{0,2000}?)(?:<h2|<h3|Electives|Concentrations|Dual|Faculty|Plan)', body, re.IGNORECASE)
        if m:
            txt = re.sub(r'<[^>]+>', ' ', m.group(1))
            txt = re.sub(r'\s+', ' ', txt).strip()
            out.write(f"REQS: {txt[:1200]}\n")
        # duration
        m = re.search(r'(?:1\s*year|2\s*years|1\.\d+-\d+\s*years|12\s*months|18\s*months|24\s*months)', body, re.IGNORECASE)
        out.write(f"DUR: {m.group(0) if m else '?'}\n")
    except Exception as e:
        out.write(f"ERR: {repr(e)[:200]}\n")
out.close()
print("done")
