photomk/build-index.py
2025-02-17 17:32:48 +01:00

46 lines
1006 B
Python
Executable File

#!/usr/bin/env python3
import clize
import json
import sys
from datetime import datetime
from pathlib import Path
from typing import Any
def get_exif_timestamp(meta: dict[str, Any]) -> int:
try:
dt = meta["exif"]["datetime"]
return datetime.strptime(dt, "%Y-%m-%d %H:%M").timestamp()
except:
return 0
def build_index(*meta_paths: Path):
metas = []
for meta_path in meta_paths:
with meta_path.open(encoding="utf8") as f:
meta = json.load(f)
metas.append(meta)
metas.sort(key=get_exif_timestamp)
metas_dict = {}
for i, meta in enumerate(metas):
meta["navigation"] = {
"previous": metas[i - 1]["path"]["distRelativeToRoot"],
"next": metas[i + 1 - len(metas)]["path"]["distRelativeToRoot"],
}
metas_dict[meta["key"]] = meta
index = {
"index": metas_dict,
}
print(json.dumps(index, indent=2))
if __name__ == "__main__":
clize.run(build_index)