Files
CloudLab/tools/split_book_catalog.py
hmandWorkBuddy b393b3a4a3
CloudLab CI/CD / 门禁 doctor (push) Failing after 34s
CloudLab CI/CD / 单元测试 (push) Skipped
CloudLab CI/CD / 归档完整性 (push) Skipped
CloudLab CI/CD / 构建站点与归档 (push) Skipped
CloudLab CI/CD / 部署到 lab.hdtime.space(rsync) (push) Skipped
CloudLab CI/CD / 构建镜像并推送 Gitea 容器仓库 (push) Skipped
CloudLab CI/CD / 容器部署到 lab.hdtime.space (push) Skipped
CloudLab CI/CD / 发布版本(Tag) (push) Skipped
feat(library): 大规模馆藏建设 catalog 3984→6177
用户反馈数量太少、达不到图书馆程度,并要求纳入医学/化学等基本教科书。
本轮三管齐下(tools/collect_mass.py + split_book_catalog.py):

1. NCBI Bookshelf OA 书库:从 file_list.csv(9771 本)筛出 1995 本主题相关书,
   按 13 学科生成书目索引,并**逐本拆成独立 JSON 上架**(关键改进:之前 1995 本
   塞在 13 个文件里目录只显示 13 条,拆分后每本都可独立检索)
2. 精选教材全文:并发分段下载 10 本 OA 书 tar.gz,抽 33 个章节 PDF/NXML
   (2 本解包失败已用短路径重试恢复)
3. 教材书目 35 学科(Crossref)+ EPMC 60 主题 × 500 条 + NCBI OA 章节 50 篇

诚实边界:NCBI OA 子集里真正的「经典教科书」有限(多为专业专著/报告),
多数经典教材有版权不能下载——以 1995 本书目索引覆盖各分支,全文按需取。

catalog 6177:F 医学 105→2066(1995 本书上架)、K 教材 394→461、
A 总论 56→131;search 5136 文档;站点 6165 文件 / 477.7MB。
科学基线精确更新(含参数敏感性表格,未用全局替换避免误伤常量)。

UI 216/0 全绿。

Co-Authored-By: WorkBuddy <noreply@workbuddy.ai>
2026-11-23 02:26:33 +08:00

69 lines
3.1 KiB
Python

#!/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()