#!/usr/bin/env python3 """瞬间迁移(Phase 2 T4):Halo moments → content/moments/*.md Halo moment.content.html 为富文本(内联样式很脏),经 pandoc 提纯为 纯文本 md;medium(配图)当前库存为空,字段留待将来有图时扩展。 """ import json import subprocess from datetime import datetime, timezone from pathlib import Path OUT = Path("content/moments") def main() -> int: moments = json.load(open(".migration/moments.json", encoding="utf-8")) n = 0 for m in moments: spec = m["spec"] if spec.get("visible") == "PRIVATE": continue html = (spec.get("content") or {}).get("html", "") text = subprocess.run( ["/home/wait4x/.local/bin/pandoc", "-f", "html", "-t", "plain", "--wrap=none"], input=html.encode(), capture_output=True, check=True, ).stdout.decode("utf-8").strip() if not text: continue ts = spec.get("releaseTime") or m["metadata"].get("creationTimestamp", "") day = datetime.fromisoformat(ts.replace("Z", "+00:00")).astimezone(timezone.utc).strftime("%Y-%m-%d") out = OUT / f"{day}.md" i = 2 while out.exists(): out = OUT / f"{day}-{i}.md" i += 1 tags = spec.get("tags") or [] # schema 约束 text/image 在 frontmatter(body 不参与校验),一句话即 text 字段 out.write_text( "---\n" f"pubDate: {day}\n" f"tags: [{', '.join(tags)}]\n" f'text: "{text.replace(chr(34), chr(39))}"\n' "---\n", encoding="utf-8", ) n += 1 print(f"migrated {n} moments") return 0 if __name__ == "__main__": raise SystemExit(main())