|
| 1 | +""" |
| 2 | +Catalog export — generates a JSON + CSV snapshot of the mirrored library. |
| 3 | +
|
| 4 | +Outputs: |
| 5 | + {state_dir}/catalog.json – full machine-readable catalog |
| 6 | + {state_dir}/catalog.csv – flat CSV for spreadsheet / import tools |
| 7 | +
|
| 8 | +Called at the end of each sync run. |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import csv |
| 13 | +import json |
| 14 | +from datetime import datetime, timezone |
| 15 | +from pathlib import Path |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +import aiofiles |
| 19 | + |
| 20 | +from .config import Config |
| 21 | +from .database import Database |
| 22 | +from .logger import get_logger |
| 23 | + |
| 24 | +log = get_logger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +async def export_catalog(config: Config, db: Database) -> dict[str, int]: |
| 28 | + """Build and write catalog files. Returns summary counts.""" |
| 29 | + log.info("catalog.export_start") |
| 30 | + |
| 31 | + # Fetch all complete items and their tracks |
| 32 | + items, total = await db.get_items_paginated( |
| 33 | + status="complete", per_page=100_000 |
| 34 | + ) |
| 35 | + |
| 36 | + rows: list[dict[str, Any]] = [] |
| 37 | + csv_rows: list[dict[str, str]] = [] |
| 38 | + |
| 39 | + for item in items: |
| 40 | + tracks = await db.get_tracks_for_item(item["identifier"]) |
| 41 | + track_list = [ |
| 42 | + { |
| 43 | + "ia_filename": t["ia_filename"], |
| 44 | + "local_filename": t["local_filename"], |
| 45 | + "local_path": t["local_path"], |
| 46 | + "title": t["title"], |
| 47 | + "track_number": t["track_number"], |
| 48 | + "md5": t["md5"], |
| 49 | + } |
| 50 | + for t in tracks |
| 51 | + if t["status"] == "complete" |
| 52 | + ] |
| 53 | + |
| 54 | + rows.append( |
| 55 | + { |
| 56 | + "identifier": item["identifier"], |
| 57 | + "collection": item["collection"], |
| 58 | + "artist": item["artist"], |
| 59 | + "date": item["date"], |
| 60 | + "venue": item["venue"], |
| 61 | + "title": item["title"], |
| 62 | + "folder": item["folder_name"], |
| 63 | + "track_count": len(track_list), |
| 64 | + "tracks": track_list, |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + # One CSV row per track |
| 69 | + for t in track_list: |
| 70 | + csv_rows.append( |
| 71 | + { |
| 72 | + "identifier": item["identifier"], |
| 73 | + "collection": item["collection"] or "", |
| 74 | + "artist": item["artist"] or "", |
| 75 | + "date": item["date"] or "", |
| 76 | + "venue": item["venue"] or "", |
| 77 | + "folder": item["folder_name"] or "", |
| 78 | + "track_number": str(t["track_number"] or ""), |
| 79 | + "title": t["title"] or "", |
| 80 | + "local_filename": t["local_filename"] or "", |
| 81 | + "local_path": t["local_path"] or "", |
| 82 | + "md5": t["md5"] or "", |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + catalog = { |
| 87 | + "generated_at": datetime.now(timezone.utc).isoformat(), |
| 88 | + "collections": config.collections, |
| 89 | + "total_items": len(rows), |
| 90 | + "total_tracks": len(csv_rows), |
| 91 | + "items": rows, |
| 92 | + } |
| 93 | + |
| 94 | + # ── Write JSON ──────────────────────────────────────────────────────── |
| 95 | + async with aiofiles.open(config.catalog_json_path, "w", encoding="utf-8") as f: |
| 96 | + await f.write(json.dumps(catalog, indent=2, ensure_ascii=False)) |
| 97 | + |
| 98 | + # ── Write CSV ───────────────────────────────────────────────────────── |
| 99 | + fieldnames = [ |
| 100 | + "identifier", "collection", "artist", "date", "venue", |
| 101 | + "folder", "track_number", "title", "local_filename", |
| 102 | + "local_path", "md5", |
| 103 | + ] |
| 104 | + csv_text_rows = [",".join(fieldnames)] |
| 105 | + for row in csv_rows: |
| 106 | + csv_text_rows.append( |
| 107 | + ",".join( |
| 108 | + f'"{row[f].replace(chr(34), chr(34)+chr(34))}"' |
| 109 | + for f in fieldnames |
| 110 | + ) |
| 111 | + ) |
| 112 | + async with aiofiles.open(config.catalog_csv_path, "w", encoding="utf-8") as f: |
| 113 | + await f.write("\n".join(csv_text_rows) + "\n") |
| 114 | + |
| 115 | + log.info( |
| 116 | + "catalog.export_done", |
| 117 | + items=len(rows), |
| 118 | + tracks=len(csv_rows), |
| 119 | + json_path=str(config.catalog_json_path), |
| 120 | + csv_path=str(config.catalog_csv_path), |
| 121 | + ) |
| 122 | + |
| 123 | + return {"items": len(rows), "tracks": len(csv_rows)} |
0 commit comments