Files
roux/scripts/import-posts.py
T

82 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Import posts from ~/projects/ginger/posts.csv into Hugo content.
Only imports posts whose generated_image exists in the selected images folder.
Run from the site root: python3 scripts/import-posts.py
"""
import csv
import os
import shutil
from pathlib import Path
GINGER = Path.home() / "projects" / "ginger"
CSV_PATH = GINGER / "posts.csv"
IMG_DIR = GINGER / "images" / "final" / "selected"
SITE = Path(__file__).parent.parent
CONTENT = SITE / "content" / "posts"
CONTENT.mkdir(parents=True, exist_ok=True)
# Collect available images
available = {f.name for f in IMG_DIR.iterdir() if f.is_file()}
matched = []
with open(CSV_PATH, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
gen = row.get("generated_image", "").strip()
if gen and gen in available:
matched.append(row)
print(f"Matched {len(matched)} posts out of {len(available)} available images")
# Sort deterministically by generated_image filename for stable plate numbers
matched.sort(key=lambda r: r["generated_image"])
for idx, row in enumerate(matched, start=1):
plate = f"{idx:03d}"
gen_img = row["generated_image"].strip()
slug = gen_img.replace(".png", "")
title = row["title"].strip()
desc = row["description"].strip()
if desc:
desc = desc[0].upper() + desc[1:]
if not desc.endswith('.'):
desc += '.'
desc = desc.replace('"', '\\"')
raw_cat = row.get("category", "").strip()
raw_tags = row.get("tags", "").strip()
cats = [raw_cat.split(",")[0].strip()] if raw_cat.strip() else ["Uncategorised"]
tags = [t.strip() for t in raw_tags.split(",") if t.strip()]
cats_yaml = "\n".join(f' - "{c}"' for c in cats)
tags_yaml = "\n".join(f' - "{t}"' for t in tags) if tags else ' []'
bundle = CONTENT / slug
bundle.mkdir(exist_ok=True)
src_img = IMG_DIR / gen_img
dst_img = bundle / gen_img
if not dst_img.exists():
shutil.copy2(src_img, dst_img)
md = f"""---
title: "{title.replace('"', '\\"')}"
description: "{desc}"
plate: "{plate}"
slug: "{slug}"
issues:
- "01"
image: "{gen_img}"
weight: {idx}
categories:
{cats_yaml}
tags:
{tags_yaml}
---
"""
(bundle / "index.md").write_text(md, encoding="utf-8")
print(f"Created {len(matched)} page bundles in {CONTENT}")