|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build small service metadata used by the static site.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import datetime as dt |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +ROOT = Path(__file__).resolve().parents[1] |
| 11 | +DATA_DIR = ROOT / "data" |
| 12 | +DOCS_DATA_DIR = ROOT / "docs" / "data" |
| 13 | + |
| 14 | + |
| 15 | +def read_json(path: Path, default): |
| 16 | + if not path.exists(): |
| 17 | + return default |
| 18 | + with path.open("r", encoding="utf-8") as handle: |
| 19 | + return json.load(handle) |
| 20 | + |
| 21 | + |
| 22 | +def write_json(path: Path, payload) -> None: |
| 23 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 24 | + with path.open("w", encoding="utf-8") as handle: |
| 25 | + json.dump(payload, handle, ensure_ascii=False, indent=2) |
| 26 | + handle.write("\n") |
| 27 | + |
| 28 | + |
| 29 | +def count_snapshots() -> int: |
| 30 | + if not DATA_DIR.exists(): |
| 31 | + return 0 |
| 32 | + count = 0 |
| 33 | + for year_dir in DATA_DIR.iterdir(): |
| 34 | + if not year_dir.is_dir() or not year_dir.name.isdigit(): |
| 35 | + continue |
| 36 | + for month_dir in year_dir.iterdir(): |
| 37 | + if not month_dir.is_dir() or not month_dir.name.isdigit(): |
| 38 | + continue |
| 39 | + for day_dir in month_dir.iterdir(): |
| 40 | + if day_dir.is_dir() and day_dir.name.isdigit() and (day_dir / "organizations.json").exists(): |
| 41 | + count += 1 |
| 42 | + return count |
| 43 | + |
| 44 | + |
| 45 | +def main() -> int: |
| 46 | + organizations = read_json(DATA_DIR / "organizations.json", []) |
| 47 | + trending = read_json(DATA_DIR / "trending.json", {}) |
| 48 | + latest = read_json(DATA_DIR / "latest.json", {}) |
| 49 | + |
| 50 | + payload = { |
| 51 | + "organizations_count": len(organizations) if isinstance(organizations, list) else 0, |
| 52 | + "trending_count": len(trending.get("items", [])) if isinstance(trending, dict) else 0, |
| 53 | + "snapshots_count": count_snapshots(), |
| 54 | + "latest_snapshot": latest.get("snapshot", "") if isinstance(latest, dict) else "", |
| 55 | + "generated_at": dt.datetime.now(dt.timezone.utc).isoformat(), |
| 56 | + } |
| 57 | + |
| 58 | + write_json(DATA_DIR / "service.json", payload) |
| 59 | + write_json(DOCS_DATA_DIR / "service.json", payload) |
| 60 | + print(f"Wrote service data for {payload['organizations_count']} organizations") |
| 61 | + return 0 |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + raise SystemExit(main()) |
0 commit comments