feat(plugins): csv and json import

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas Winther
2026-10-30 15:48:59 +01:00
co-authored by Claude Sonnet 5.5
parent 06a59e1b5c
commit a540c7d34d
7 changed files with 429 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
import { createHash } from "node:crypto";
import { parseCsv } from "@sttracker/plugin-sdk/csv";
import { definePlugin } from "@sttracker/plugin-sdk/types";
import type { EngagementFields, EntryFields, SourceRecord, WorkHint } from "@sttracker/plugin-sdk/types";
import {
cleanId, defaultScale, normalizeRating, parseDate, parseMapping, parseMediaType, parseStatus, parseTags, rowsToFieldRows,
type FieldRow,
} from "./src/mapping.ts";
const str = (v: string | string[] | undefined): string | undefined => (typeof v === "string" && v.trim() !== "" ? v.trim() : undefined);
function jsonRows(text: string): FieldRow[] {
let v: unknown;
try {
v = JSON.parse(text);
} catch {
throw new Error("csv plugin: file is not valid JSON");
}
if (!Array.isArray(v)) throw new Error("csv plugin: JSON must be an array of objects");
return v.map((o) => {
const r: Record<string, string | string[]> = {};
if (typeof o === "object" && o !== null) {
for (const [k, x] of Object.entries(o)) {
if (x == null || x === "") continue;
r[k] = Array.isArray(x) ? x.map(String) : String(x);
}
}
return r as FieldRow;
});
}
function* toRecords(rows: FieldRow[], rawRows: unknown[]): Generator<SourceRecord> {
const scale = defaultScale(rows);
const goodreadsLike = (r: FieldRow) => r.isbn13 !== undefined;
for (const [i, r] of rows.entries()) {
const title = str(r.title);
if (!title) continue;
const mediaType = parseMediaType(str(r.mediaType), goodreadsLike(r) || r.author !== undefined ? "book" : "other");
const finished = parseDate(str(r.finishedAt));
const started = parseDate(str(r.startedAt));
const hash = createHash("sha1").update([title, mediaType, finished?.value ?? ""].join("\u0000")).digest("hex");
const key = `row:${hash}`;
const externalIds: Record<string, string> = {};
for (const f of ["isbn13", "imdb", "tmdb_movie", "igdb"] as const) {
const id = cleanId(r[f]);
if (id) externalIds[f] = id;
}
const author = str(r.author);
const work: WorkHint = { title, mediaType, externalIds, ...(author ? { creators: [{ name: author }] } : {}) };
const entry: EntryFields = {};
const status = parseStatus(str(r.status));
if (status) entry.status = status;
const rating = normalizeRating(r, scale);
if (rating !== null) entry.rating = rating;
const review = str(r.review);
if (review) entry.review = review;
const notes = str(r.notes);
if (notes) entry.notes = notes;
const tags = parseTags(r.tags);
if (tags) entry.tags = tags;
const raw = rawRows[i];
yield { kind: "entry", key, raw, work, entry };
if (finished || started) {
const engagement: EngagementFields = {};
if (started) engagement.startedAt = started;
if (finished) engagement.finishedAt = finished;
yield { kind: "engagement", key: `${key}:engagement`, raw, work, engagement };
}
}
}
export default definePlugin({
manifest: {
id: "csv",
name: "CSV / JSON import",
version: "0.1.0",
sdkVersion: 1,
mediaTypes: ["book", "comic", "manga", "game", "movie", "show", "podcast", "audio_drama", "album", "video", "other"],
capabilities: ["importFile"],
config: [
{
key: "mapping",
label: "Column mapping",
type: "string",
help: 'JSON { "column": "field" }. Fields: title mediaType status rating ratingScale(5|10|100) review finishedAt startedAt tags isbn13 imdb tmdb_movie igdb notes. Leave empty to guess from header names (Goodreads exports work out of the box). Ignored for .json.',
},
],
},
async *importFile(ctx, file) {
const text = new TextDecoder("utf-8").decode(file.bytes);
const lower = file.name.toLowerCase();
if (lower.endsWith(".json")) {
const rows = jsonRows(text.replace(/^/, ""));
yield* toRecords(rows, JSON.parse(text.replace(/^/, "")) as unknown[]);
} else if (lower.endsWith(".csv")) {
const table = parseCsv(text);
const rows = rowsToFieldRows(table, parseMapping(ctx.config.mapping));
const header = table[0] ?? [];
yield* toRecords(rows, table.slice(1).map((cells) => Object.fromEntries(header.map((h, i) => [h, cells[i] ?? ""]))));
} else {
throw new Error(`csv plugin: unsupported file "${file.name}" (expected .csv or .json)`);
}
},
});