# -*- coding: utf-8 -*-
import io, sys, os, json, ssl, re, urllib.request, html as H
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
todo=json.load(open("todo_upgrade.json",encoding="utf-8"))
bee=[t for t in todo if t.get("s")=="cornell" and ("biological" in (t.get("en","").lower()) or "environmental engineering" in (t.get("en","").lower()) or "BEE" in t.get("en",""))]
for t in bee: print(json.dumps(t,ensure_ascii=False))
# 探测 CALS 页
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
UA={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
url="https://cals.cornell.edu/biological-environmental-engineering"
try:
    req=urllib.request.Request(url,headers=UA)
    r=urllib.request.urlopen(req,context=ctx,timeout=20)
    txt=r.read().decode("utf-8","ignore")
    open("_cornell_bee_raw.html","w",encoding="utf-8").write(txt)
    print("\nSTATUS",r.status,"len",len(txt))
    body=re.sub(r"<script.*?</script>","",txt,flags=re.S); body=re.sub(r"<style.*?</style>","",body,flags=re.S)
    text=H.unescape(re.sub(r"\s+"," ",re.sub(r"<[^>]+>"," ",body)))
    open("_cornell_bee.txt","w",encoding="utf-8").write(text)
    print("正文",len(text))
    for kw in ["Master of Engineering","M.Eng","MS","PhD","credit","semester","concentration","curriculum","admission","deadline","tuition","GRE","language","TOEFL","IELTS","Distance","online","part-time"]:
        i=text.lower().find(kw.lower())
        if i>=0: print(f"[{kw}] ...{text[max(0,i-70):i+150]}...")
    links=sorted(set(re.findall(r'href="([^"]*(?:graduate|master|meng|ms|degree|admission)[^"]*)"',txt)))
    print("\nLINKS:")
    for l in links[:25]: print("  ",l)
except Exception as e:
    print("ERR",type(e).__name__,e)
