#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""从新增详情页 HTML 中提取最像学校官网的 URL"""
import re, os
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

new_items = [
    ("cmu-mspm", "cmu", "产品管理理学硕士", "MSPM", "details/cmu-product-management-ms.html"),
    ("cornell-bee-ms", "cornell", "生物与环境工程硕士（研究型）", "MS", "details/cornell-bee-ms.html"),
    ("cornell-bme-ms", "cornell", "生物医学工程硕士（研究型）", "MS", "details/cornell-bme-ms.html"),
    ("cornell-mps-aem", "cornell", "应用经济学与管理专业研究硕士", "MPS", "details/cornell-cb05803.html"),
    ("emory-econ-ms", "emory", "经济学理学硕士", "MS", "details/emory-economics-ms.html"),
    ("harvard-stats-am", "harvard", "统计学文学硕士（Terminal AM）", "AM", "details/harvard-statistics.html"),
    ("nyu-a3sr-ms", "nyu", "应用统计与社科研究硕士", "MS", "details/nyu-a3sr-ms.html"),
    ("princeton-cs-mse", "princeton", "计算机科学工程硕士", "MSE", "details/princeton-computer-science.html"),
    ("princeton-mfin", "princeton", "金融学硕士", "MFin", "details/princeton-finance.html"),
    ("rice-meml-oncampus", "rice", "工程管理与领导力硕士（在校全日制）", "MEML", "details/rice-meml-oncampus.html"),
    ("ucsd-ba", "ucsd", "商业分析", "MS", "details/ucsd-business-analytics-ms.html"),
    ("ucsd-bioe", "ucsd", "生物工程", "MS", "details/ucsd-bioengineering-ms.html"),
    ("ucsd-cse-am", "ucsd", "计算机科学与工程（先进制造）", "MS", "details/ucsd-computer-science-and-engineering-advanced-manufacturing-ms.html"),
    ("ucsd-csme", "ucsd", "计算科学、数学与工程", "MS", "details/ucsd-computational-science-mathematics-and-engineering-ms.html"),
    ("ucsd-css", "ucsd", "计算社会科学", "MS", "details/ucsd-computational-social-science-ms.html"),
    ("ucsd-mqf", "ucsd", "量化金融硕士", "MS", "details/ucsd-quantitative-finance-ms.html"),
    ("ucsd-ms-stat-2027", "ucsd", "统计学硕士", "MS", "details/ucsd-statistics-ms.html"),
    ("unc-bhmi", "unc", "生物医学与健康信息学专业硕士", "MPS", "details/unc-biomedical-and-health-informatics-ms.html"),
    ("unc-stats-ds", "unc", "统计、分析与数据科学硕士", "MS", "details/unc-statistics-analytics-and-data-science-ms.html"),
    ("upenn-ee-mse", "up", "电气工程硕士", "MSE", "details/upenn-ee-mse.html"),
    ("upenn-math-ma", "up", "数学文学硕士", "AM", "details/upenn-math-ma.html"),
    ("usc-abe-ms", "usc", "应用生物统计与流行病学硕士", "MS", "details/usc-applied-biostatistics-epidemiology-ms.html"),
    ("usc-cgb-ms", "usc", "癌症基因组学与生物信息学硕士", "MS", "details/usc-cancer-genomics-bioinformatics-ms.html"),
    ("usc-me-emgmt-dual-ms", "usc", "机械工程与工程管理双学位硕士", "MS (Dual)", "details/usc-me-emgmt-dual-ms.html"),
    ("usc-mmi-ms", "usc", "分子微生物学与免疫学硕士", "MS", "details/usc-molecular-microbiology-immunology-ms.html"),
    ("usc-ms-aee", "usc", "应用经济学与计量经济学", "MS", "details/usc-c6e2c49.html"),
    ("usc-phds-ms", "usc", "公共卫生数据科学硕士", "MS", "details/usc-public-health-data-science-ms.html"),
    ("usc-spatial-ds", "usc", "空间数据科学硕士", "MS", "details/usc-spatial-data-science-ms.html"),
    ("uw-civil-engineering-meng-coop", "uw", "土木工程工程硕士（带薪实习 Co-op）", "MEng", "details/uw-civil-engineering-meng-coop.html"),
    ("yale-bme-ms", "yale", "生物医学工程", "MS", "details/yale-biomedical-engineering.html"),
    ("yale-cbb-ms", "yale", "计算生物学与生物医学信息", "MS", "details/yale-computational-biology-biomedical-informatics.html"),
    ("yale-cs-ms", "yale", "计算机科学", "MS", "details/yale-computer-science.html"),
    ("yale-ece-ms", "yale", "电气与计算机工程", "MS", "details/yale-electrical-computer-engineering.html"),
    ("yale-stats-ds-ms", "yale", "统计与数据科学", "MS", "details/yale-statistics-data-science.html"),
]

# 学校域名映射
domains = {
    "cmu":"cmu.edu","cornell":"cornell.edu","emory":"emory.edu","harvard":"harvard.edu",
    "nyu":"nyu.edu","princeton":"princeton.edu","rice":"rice.edu","ucsd":"ucsd.edu",
    "unc":"unc.edu","up":"upenn.edu","usc":"usc.edu","uw":"uwaterloo.ca","yale":"yale.edu",
}

def pick_official_url(html, school_code):
    """从 HTML 中挑出最像学校官网的 URL"""
    urls = re.findall(r'https?://[^"\'<>\s]+', html)
    # 过滤
    cands = []
    for u in urls:
        if "file://" in u or u.startswith("file:"): continue
        if "fonts.g" in u or "googleapis" in u or "gstatic" in u: continue
        if "cdnjs" in u or "jsdelivr" in u or "unpkg" in u: continue
        if "w3.org" in u or "schema.org" in u: continue
        if "localhost" in u: continue
        # 优先学校域名
        dom = domains.get(school_code, "")
        if dom and dom in u:
            cands.append(u)
    if cands:
        # 优先选最具体的（路径最长）
        cands.sort(key=len, reverse=True)
        return cands[0]
    return ""

for pid, sc, n, deg, url in new_items:
    fpath = url
    if not os.path.exists(fpath):
        print(f"[MISS] {pid} -> {fpath}")
        continue
    try:
        content = open(fpath, encoding="utf-8").read()
    except:
        content = open(fpath, encoding="gbk", errors="ignore").read()
    official = pick_official_url(content, sc)
    print(f"{pid}|{sc}|{n}|{deg}|{url}|{official}")
