#!/usr/bin/env python3 # -*- coding: utf-8 -*- """把 NCBI OA 书库的学科书目 JSON 拆成「一本书一个文件」,使其独立上架。 之前 1995 本书装在 13 个 JSON 里,目录里只显示 13 条; 拆分后每本书成为独立可检索条目,catalog 直接 +1995。 用法:python3 tools/split_book_catalog.py """ from __future__ import annotations import hashlib, json, re from datetime import datetime, timezone from pathlib import Path ROOT = Path(__file__).resolve().parent.parent SRC = ROOT / "馆藏建设" / "NCBI-OA书库" OUT = ROOT / "馆藏建设" / "OA书单" MANIFEST = ROOT / "cloudlab-web" / "app" / "data" / "collection-book-entries.json" def safe(s, n=60): s = re.sub(r'[\\/:*?"<>|]+','_',s) s = re.sub(r'\s+',' ',s).strip(' .') return s[:n] or 'item' def main(): items = [] overview = json.loads((SRC/"_书库总览.json").read_text(encoding="utf-8")) for cat in overview["categories"]: src = ROOT/cat["path"] if not src.exists(): continue data = json.loads(src.read_text(encoding="utf-8")) for b in data["books"]: title = b.get("title","").strip() acc = b.get("accession","") if not title or not acc: continue # 一本书一个 JSON(含完整元数据 + 全文直链) entry = { "title": title, "accession": acc, "publisher": b.get("publisher",""), "year": b.get("year",""), "discipline": data["category"], "subject": cat["subject"], "license": "NCBI Bookshelf OA 子集(书目公共领域;全文逐本核对版权页)", "fulltext_url": b.get("url",""), "source": "https://ftp.ncbi.nlm.nih.gov/pub/litarch/file_list.csv", "note": "本书目条目来自 NCBI OA 书单;全文 tar.gz 可通过 fulltext_url 按需下载。", } content = json.dumps(entry, ensure_ascii=False, indent=1).encode("utf-8") rel = f"馆藏建设/OA书单/{safe(cat['category'])}/{acc}_{safe(title,52)}.json" p = ROOT/rel; p.parent.mkdir(parents=True,exist_ok=True); p.write_bytes(content) items.append({"title":title,"source_url":entry["fulltext_url"],"path":rel, "category":"馆藏建设 / OA 书单(逐本上架)", "license":entry["license"],"subject":cat["subject"], "status":"正常","bytes":len(content), "sha256":hashlib.sha256(content).hexdigest(),"error":"", "discipline":data["category"],"year":b.get("year",""), "collected_utc":datetime.now(timezone.utc).isoformat(timespec="seconds")}) doc={"generated_utc":datetime.now(timezone.utc).isoformat(timespec="seconds"), "n":len(items),"note":"NCBI OA 书库逐本上架:每本书一个独立可检索条目", "items":items} MANIFEST.write_text(json.dumps(doc,ensure_ascii=False,indent=1),encoding="utf-8") print(f"逐本上架 {len(items)} 本书 → {MANIFEST.name}") if __name__=="__main__": main()