# -*- coding: utf-8 -*-
import io,sys,urllib.request,ssl,re
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
for code in ["APLPH-MS","GEOPH-MS"]:
    url=f"https://bulletin.stanford.edu/programs/{code}"
    try:
        req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
        raw=urllib.request.urlopen(req,context=ctx,timeout=25).read().decode("utf-8","ignore")
        t=re.sub(r"<script[\s\S]*?</script>","",raw)
        t=re.sub(r"<style[\s\S]*?</style>","",t)
        t=re.sub(r"<[^>]+>"," ",t)
        t=re.sub(r"&nbsp;"," ",t); t=re.sub(r"&amp;","&",t); t=re.sub(r"\s+"," ",t)
        print("="*30, code, "len=",len(t))
        i=t.find("Program Overview")
        if i<0: i=t.lower().find("ms program")
        print(t[i:i+1200])
        print("\n--- 关键词 ---")
        for kw in ["units","GPA","quarter","thesis","research","track","option","prerequisite","online","part-time","HCP","Coterminal","prereq"]:
            k=t.lower().find(kw.lower())
            if k>=0: print(f"[{kw}] ...{t[max(0,k-70):k+160]}...")
        print()
    except Exception as e:
        print(code,"ERR",e)
