kindle-zotero_importer/src/kindle_zotero_importer/final_plan.py
Utku Bilen Demir 830b27e510 0.6.4: incremental id-based import, Mappings/Integrated, black iA Duo, resizable tables
- id-based incremental (new_ids - prev_integrated_ids) with --full flag, kindle-id tag, deletions handling
- Mappings tab with delete to return to Conflicts, sorted by updated_at newest first
- Integrated tab with Added On / Integrated dates and Citekey
- black over white iA Writer Duo theme, candidate-list per-row Use buttons, resizable/sortable tables
- Settings editable + save, Artifacts Open/Reveal, Re-import bar with last file reuse
- Fix override single-field validation and ignore cleanup
2026-09-16 14:44:31 +02:00

67 lines
2.5 KiB
Python

from __future__ import annotations
import datetime
from typing import Any
FINAL_FORMAT = "kindle-zotero-importer.zotero-writer-plan.v1"
def build_final_writer_plan(positioned_plan: dict[str, Any]) -> dict[str, Any]:
annotations = []
skipped: dict[str, int] = {}
for item in positioned_plan["items"]:
if item.get("status") != "positioned":
skipped[item["status"]] = skipped.get(item["status"], 0) + 1
continue
attachment = item["zotero"]["attachment"]
annotation = item["annotation"]
if not annotation.get("position"):
skipped["positioned-missing-writer-fields"] = (
skipped.get("positioned-missing-writer-fields", 0) + 1
)
continue
clipping = item["clipping"]
annotations.append(
{
"clipping_id": clipping["id"],
"clipping_title": clipping["title"],
"clipping_added_on": clipping.get("added_on"),
"clipping_added_on_iso": clipping.get("added_on_iso"),
"integrated_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"attachment_item_id": attachment["item_id"],
"attachment_key": attachment["key"],
"parent_item_id": item["zotero"]["parent_item_id"],
"parent_key": item["zotero"]["parent_key"],
"citation_key": item["zotero"].get("citation_key"),
"annotation": {
"type": annotation["type"],
"text": annotation.get("text") or "",
"comment": annotation.get("comment") or "",
"color": annotation.get("color") or "#ffd400",
"pageLabel": annotation.get("pageLabel") or "",
"sortIndex": annotation.get("sortIndex"),
"position": annotation["position"],
"tags": [
{"name": "kindle-import"},
{"name": f"kindle-id:{clipping['id']}"},
],
},
}
)
annotations.sort(
key=lambda entry: (
entry.get("attachment_item_id") or 0,
entry["annotation"].get("sortIndex") or "",
entry.get("clipping_id") or "",
)
)
return {
"format": FINAL_FORMAT,
"source_format": positioned_plan.get("format"),
"annotation_count": len(annotations),
"skipped_counts": skipped,
"annotations": annotations,
}