322 lines
11 KiB
Python
322 lines
11 KiB
Python
"""从 Excel + 25家医院照片合集 生成 hospitals-content.ts 并复制图片到 public。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
from hospitals_en_maps import (
|
|
dept_en,
|
|
expert_label_en,
|
|
hospital_intro_en,
|
|
hospital_name_en,
|
|
name_to_pinyin,
|
|
)
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
WEB = ROOT / "web"
|
|
EXCEL_JSON = WEB / "scripts" / "hospitals-excel-raw.json"
|
|
PHOTOS_ROOT = None
|
|
for d in ROOT.iterdir():
|
|
if d.is_dir() and "25" in d.name and "医院" in d.name:
|
|
inner = d / d.name if (d / d.name).is_dir() else d
|
|
for sub in d.iterdir():
|
|
if sub.is_dir() and "25" in sub.name:
|
|
PHOTOS_ROOT = sub
|
|
break
|
|
if PHOTOS_ROOT is None:
|
|
PHOTOS_ROOT = d
|
|
break
|
|
|
|
OUT_DATA = WEB / "data" / "hospitals-content.ts"
|
|
OUT_HOSPITALS = WEB / "public" / "images" / "hospitals"
|
|
OUT_DOCTORS = WEB / "public" / "images" / "doctors"
|
|
EXPERT_INFO_EN = WEB / "scripts" / "doctor-expert-info-en.json"
|
|
|
|
FOLDER_TO_HOSPITAL: dict[str, str] = {
|
|
"中山一院图": "中山大学附属第一医院(越秀院区、南沙院区)",
|
|
"广东省人民医院": "广东省人民医院",
|
|
"广东省妇幼保健院": "广东省妇幼保健院",
|
|
"孙逸仙纪念医院": "中山大学孙逸仙纪念医院",
|
|
"中山三院": "中山大学附属第三医院",
|
|
"中山大学中山眼科中心": "中山大学中山眼科中心",
|
|
"中山肿瘤": "中山大学肿瘤防治中心",
|
|
"中大口腔": "中山大学附属口腔医院",
|
|
"中山六院": "中山大学附属第六医院",
|
|
"南方医院": "南方医科大学南方医院",
|
|
"珠江医院": "南方医科大学珠江医院",
|
|
"暨大附一院": "暨南大学附属第一医院",
|
|
"广东省中医院": "广东省中医院",
|
|
"汕大附一院": "汕头大学医学院第一附属医院",
|
|
"广医附一院": "广州医科大学附属第一医院",
|
|
"广州市一院": "广州市第一人民医院",
|
|
"广妇儿中心": "广州医科大学附属妇女儿童医疗中心",
|
|
"深圳人医": "深圳市人民医院",
|
|
"北大深圳": "北京大学深圳医院",
|
|
"港大深圳": "香港大学深圳医院",
|
|
"南方深圳": "南方医科大学深圳医院",
|
|
"中山5院": "中山大学附属第五医院",
|
|
"珠海人民医院": "珠海市人民医院",
|
|
"祈福医院": "广东祈福医院",
|
|
"佛山复星": "佛山复星禅诚医院",
|
|
"湘雅二院": "中南大学湘雅二医院",
|
|
}
|
|
|
|
HOSPITAL_IMAGE_HINTS = (
|
|
"医院",
|
|
"院.png",
|
|
"院.jpg",
|
|
"院.jpeg",
|
|
"院.webp",
|
|
"一院",
|
|
"肿瘤",
|
|
"口腔",
|
|
"眼科",
|
|
"妇儿",
|
|
"人民",
|
|
"祈福",
|
|
"复星",
|
|
"禅城",
|
|
)
|
|
|
|
|
|
def extract_doctor_name(expert: str) -> str:
|
|
expert = expert.strip()
|
|
if not expert:
|
|
return expert
|
|
# 去掉常见前缀职称/科室描述,取末尾 2-4 个汉字为人名
|
|
m = re.search(r"([\u4e00-\u9fff]{2,4})$", expert)
|
|
if m:
|
|
return m.group(1)
|
|
parts = expert.split()
|
|
return parts[-1] if parts else expert
|
|
|
|
|
|
def is_hospital_image(filename: str, folder_name: str = "") -> bool:
|
|
stem = Path(filename).stem
|
|
if folder_name and (stem == folder_name or folder_name in stem):
|
|
return True
|
|
if any(h in filename for h in HOSPITAL_IMAGE_HINTS):
|
|
return True
|
|
for hint in ("院", "医院", "中心", "附一", "人民", "肿瘤", "口腔", "眼科", "妇儿", "祈福", "复星", "深圳", "禅城"):
|
|
if hint in stem and len(stem) <= 14:
|
|
return True
|
|
return False
|
|
|
|
|
|
def find_doctor_file(folder: Path, name: str) -> str | None:
|
|
for f in folder.iterdir():
|
|
if not f.is_file() or f.name.startswith("."):
|
|
continue
|
|
if is_hospital_image(f.name, folder.name):
|
|
continue
|
|
stem = f.stem.split("251219")[0] # 孙颖251219.jpg
|
|
if name in stem or stem in name or stem.endswith(name) or name in f.stem:
|
|
return f.name
|
|
if name == stem or name in stem.replace(" ", ""):
|
|
return f.name
|
|
# 模糊:文件名包含人名
|
|
for f in folder.iterdir():
|
|
if not f.is_file() or f.name.startswith(".") or is_hospital_image(f.name, folder.name):
|
|
continue
|
|
if name in f.stem:
|
|
return f.name
|
|
return None
|
|
|
|
|
|
def find_hospital_image(folder: Path) -> str | None:
|
|
candidates = []
|
|
for f in folder.iterdir():
|
|
if not f.is_file() or f.name.startswith("."):
|
|
continue
|
|
if is_hospital_image(f.name, folder.name):
|
|
candidates.append(f.name)
|
|
if not candidates:
|
|
return None
|
|
# 优先 png/webp/jpg 里最短文件名(通常是医院主图)
|
|
candidates.sort(key=lambda x: (len(x), x))
|
|
return candidates[0]
|
|
|
|
|
|
def parse_excel() -> list[dict]:
|
|
with open(EXCEL_JSON, encoding="utf-8") as fh:
|
|
rows = json.load(fh)
|
|
hospitals: list[dict] = []
|
|
current: dict | None = None
|
|
for row in rows[1:]:
|
|
cells = (row + [None] * 9)[:9]
|
|
h, dept, expert, _hospital_en, _idx, expert_info, intro, url, cert = cells
|
|
if h:
|
|
if current:
|
|
hospitals.append(current)
|
|
current = {
|
|
"name": h,
|
|
"intro": intro or "",
|
|
"url": url or "",
|
|
"cert": cert or "",
|
|
"doctors": [],
|
|
}
|
|
if current and expert:
|
|
current["doctors"].append(
|
|
{
|
|
"department": dept or "",
|
|
"expert": expert,
|
|
"name": extract_doctor_name(expert),
|
|
"expertInfo": (expert_info or "").strip() or None,
|
|
}
|
|
)
|
|
if current:
|
|
hospitals.append(current)
|
|
return hospitals
|
|
|
|
|
|
def main() -> None:
|
|
if PHOTOS_ROOT is None:
|
|
raise SystemExit("找不到 25家医院照片合集 目录")
|
|
|
|
excel_hospitals = parse_excel()
|
|
name_to_excel = {h["name"]: h for h in excel_hospitals}
|
|
expert_info_en = json.loads(EXPERT_INFO_EN.read_text(encoding="utf-8")) if EXPERT_INFO_EN.is_file() else {}
|
|
|
|
OUT_HOSPITALS.mkdir(parents=True, exist_ok=True)
|
|
OUT_DOCTORS.mkdir(parents=True, exist_ok=True)
|
|
|
|
hospitals_out: list[dict] = []
|
|
doctors_out: list[dict] = []
|
|
|
|
for folder_name, hospital_name in FOLDER_TO_HOSPITAL.items():
|
|
folder = PHOTOS_ROOT / folder_name
|
|
if not folder.is_dir():
|
|
raise SystemExit(f"缺少照片目录: {folder_name}")
|
|
|
|
excel = name_to_excel.get(hospital_name)
|
|
if not excel:
|
|
raise SystemExit(f"Excel 中未找到医院: {hospital_name}")
|
|
|
|
hid = folder_name
|
|
hosp_img = find_hospital_image(folder)
|
|
if not hosp_img:
|
|
raise SystemExit(f"未找到医院主图: {folder_name}")
|
|
|
|
ext = Path(hosp_img).suffix.lower()
|
|
dest_hosp = f"{hid}{ext}"
|
|
shutil.copy2(folder / hosp_img, OUT_HOSPITALS / dest_hosp)
|
|
|
|
intro = hospital_intro_en(hospital_name, excel.get("cert") or None)
|
|
display_hospital_name = hospital_name_en(hospital_name)
|
|
|
|
hospitals_out.append(
|
|
{
|
|
"id": hid,
|
|
"name": display_hospital_name,
|
|
"intro": intro,
|
|
"url": excel["url"],
|
|
"image": f"/images/hospitals/{dest_hosp}",
|
|
}
|
|
)
|
|
|
|
for doc in excel["doctors"]:
|
|
dname_zh = doc["name"]
|
|
dname_en = name_to_pinyin(dname_zh)
|
|
photo = find_doctor_file(folder, dname_zh)
|
|
if not photo:
|
|
print(f"WARN: 无照片 {folder_name} / {dname_zh} ({doc['expert']})")
|
|
photo = "placeholder.jpg"
|
|
|
|
did = f"{hid}-{dname_zh}"
|
|
if photo != "placeholder.jpg":
|
|
ext_d = Path(photo).suffix.lower()
|
|
dest_doc = f"{did}{ext_d}"
|
|
shutil.copy2(folder / photo, OUT_DOCTORS / dest_doc)
|
|
image_path = f"/images/doctors/{dest_doc}"
|
|
else:
|
|
image_path = "/images/doctor.jpg"
|
|
|
|
record = {
|
|
"id": did,
|
|
"name": dname_en,
|
|
"department": dept_en(doc["department"]),
|
|
"hospitalId": hid,
|
|
"hospitalName": display_hospital_name,
|
|
"image": image_path,
|
|
"expertLabel": expert_label_en(doc["department"], dname_en),
|
|
}
|
|
expert_info = doc.get("expertInfo")
|
|
if expert_info:
|
|
record["expertInfo"] = expert_info_en.get(did, expert_info)
|
|
doctors_out.append(record)
|
|
|
|
# 指定优先排序(按 id),其余按英文名字母顺序顺延
|
|
PRIORITY_ORDER = ["祈福医院", "珠江医院", "广州市一院", "孙逸仙纪念医院", "广东省中医院"]
|
|
priority_rank = {hid: i for i, hid in enumerate(PRIORITY_ORDER)}
|
|
hospitals_out.sort(
|
|
key=lambda x: (priority_rank.get(x["id"], len(PRIORITY_ORDER)), x["name"])
|
|
)
|
|
doctors_out.sort(key=lambda x: (x["hospitalName"], x["name"]))
|
|
# 指定优先排序(按 id),其余保持 (hospitalName, name) 顺序顺延
|
|
DOCTOR_PRIORITY_ORDER = [
|
|
"广东省人民医院-吴一龙",
|
|
"广东省人民医院-钟文昭",
|
|
"中山大学中山眼科中心-陈伟蓉",
|
|
"中山肿瘤-马骏",
|
|
"中山肿瘤-孙颖",
|
|
"广医附一院-何健行",
|
|
"南方医院-侯凡凡",
|
|
"广妇儿中心-陈欣欣",
|
|
"广医附一院-曾国华",
|
|
]
|
|
doctor_rank = {did: i for i, did in enumerate(DOCTOR_PRIORITY_ORDER)}
|
|
indexed_docs = list(enumerate(doctors_out))
|
|
|
|
def doctor_sort_key(item: tuple[int, dict]) -> tuple[int, int]:
|
|
idx, doc = item
|
|
return (doctor_rank.get(doc["id"], len(DOCTOR_PRIORITY_ORDER)), idx)
|
|
|
|
indexed_docs.sort(key=doctor_sort_key)
|
|
doctors_out = [doc for _, doc in indexed_docs]
|
|
|
|
ts_lines = [
|
|
"/** Auto-generated by web/scripts/build-hospitals-data.py — do not edit manually */",
|
|
"",
|
|
"export type HospitalRecord = {",
|
|
" id: string",
|
|
" name: string",
|
|
" intro: string",
|
|
" url: string",
|
|
" image: string",
|
|
"}",
|
|
"",
|
|
"export type DoctorRecord = {",
|
|
" id: string",
|
|
" name: string",
|
|
" department: string",
|
|
" hospitalId: string",
|
|
" hospitalName: string",
|
|
" image: string",
|
|
" /** Full specialist title for future hover bio */",
|
|
" expertLabel: string",
|
|
" /** Specialist bio — shown when available */",
|
|
" expertInfo?: string",
|
|
"}",
|
|
"",
|
|
f"export const hospitals: HospitalRecord[] = {json.dumps(hospitals_out, ensure_ascii=False, indent=2)}",
|
|
"",
|
|
f"export const doctors: DoctorRecord[] = {json.dumps(doctors_out, ensure_ascii=False, indent=2)}",
|
|
"",
|
|
"export function getHospitalById(id: string): HospitalRecord | undefined {",
|
|
" return hospitals.find((h) => h.id === id)",
|
|
"}",
|
|
"",
|
|
"export function getDoctorsByHospitalId(hospitalId: string): DoctorRecord[] {",
|
|
" return doctors.filter((d) => d.hospitalId === hospitalId)",
|
|
"}",
|
|
"",
|
|
]
|
|
OUT_DATA.write_text("\n".join(ts_lines), encoding="utf-8")
|
|
print(f"OK: {len(hospitals_out)} hospitals, {len(doctors_out)} doctors -> {OUT_DATA}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|