# -*- coding: utf-8 -*-
import re, io, sys, os, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
slugs=["aerospace-engineering-ms","materials-science-engineering-ms","physics-ms","public-policy-ms","human-computer-interaction-ms"]
for s in slugs:
    txt=open(f"_49_{s}.html",encoding="utf-8").read()
    # strip
    body=re.sub(r"<script[\s\S]*?</script>","",txt)
    body=re.sub(r"<style[\s\S]*?</style>","",body)
    # program of study section
    m=re.search(r"Program of Study([\s\S]*?)(?:</section>|Program Requirements|College of)",body)
    seg=m.group(1) if m else body
    # extract course rows: <tr><td><a>CODE</a></td><td>TITLE</td><td>CR</td>
    rows=re.findall(r"<tr[^>]*>\s*<td[^>]*>\s*<a[^>]*>([^<]+)</a>\s*</td>\s*<td[^>]*>([^<]+)</td>\s*<td[^>]*>([^<]+)</td>",seg)
    print("===",s,"rows:",len(rows))
    seen=set()
    for code,title,cr in rows:
        code=re.sub(r"\s+"," ",code).strip()
        title=re.sub(r"\s+"," ",title).strip()
        cr=re.sub(r"\s+"," ",cr).strip()
        k=(code,title)
        if k in seen: continue
        seen.add(k)
        print(f"  {code} | {title} | {cr}")
    # total credit
    tc=re.search(r"[Tt]otal (?:Credit|Hours|Hours Credit)[s:]?\s*([0-9]+)",seg)
    if tc: print("  TOTAL:",tc.group(1))
    # program description
    d=re.search(r"<p>([^<]{40,400})</p>",body)
    if d: print("  DESC:",re.sub(r"\s+"," ",d.group(1)).strip()[:200])
