Update or create _index.json for insights directory.
(self, output_path: str, entries: List[dict])
| 212 | return zh_file, fm |
| 213 | |
| 214 | def update_index(self, output_path: str, entries: List[dict]) -> None: |
| 215 | """Update or create _index.json for insights directory.""" |
| 216 | index_file = self.output_dir / output_path / "_index.json" |
| 217 | existing: List[dict] = [] |
| 218 | if index_file.exists(): |
| 219 | with open(index_file, "r", encoding="utf-8") as f: |
| 220 | data = json.load(f) |
| 221 | existing = data.get("entries", []) |
| 222 | |
| 223 | existing_ids = {e["id"] for e in existing} |
| 224 | for entry in entries: |
| 225 | if entry["id"] not in existing_ids: |
| 226 | existing.append(entry) |
| 227 | |
| 228 | # Sort by published_date descending |
| 229 | existing.sort(key=lambda e: e.get("published_date", ""), reverse=True) |
| 230 | |
| 231 | index_data = { |
| 232 | "category": output_path, |
| 233 | "last_updated": datetime.now().strftime("%Y-%m-%d"), |
| 234 | "count": len(existing), |
| 235 | "entries": existing, |
| 236 | } |
| 237 | with open(index_file, "w", encoding="utf-8") as f: |
| 238 | json.dump(index_data, f, ensure_ascii=False, indent=2) |
| 239 | |
| 240 | def list_posts(self) -> None: |
| 241 | """List all posts and their detected subcategories.""" |