#!/usr/bin/env node /** * Text or image search against the nyris Find API. * * NYRIS_API_KEY=... node find.mjs text "hydraulic pump" * NYRIS_API_KEY=... node find.mjs image ./query.jpg * NYRIS_API_KEY=... node find.mjs regions ./query.jpg */ import { readFile } from "node:fs/promises"; const base = process.env.NYRIS_BASE_URL || "https://api.nyris.io"; const apiKey = (process.env.NYRIS_API_KEY || "").trim(); function areaSize(width, height, maxSide = 1024) { if (height === 0) return [width, height]; const aspect = width / height; const area = maxSide * maxSide; const outW = Math.sqrt(area * aspect); const outH = outW / aspect; return [Math.max(1, Math.round(outW)), Math.max(1, Math.round(outH))]; } async function main() { const [mode, arg] = process.argv.slice(2); if (!mode || !arg) { console.error('Usage: node find.mjs text "" | image | regions | resize-check WxH'); process.exit(1); } if (mode === "resize-check") { const [width, height] = arg.split("x").map(Number); console.log(areaSize(width, height).join("x")); return; } if (!apiKey) { console.error("Set NYRIS_API_KEY"); process.exit(1); } let response; if (mode === "text") { const body = new FormData(); body.append("text", arg); response = await fetch(`${base}/find/v1.1`, { method: "POST", headers: { "X-Api-Key": apiKey, Accept: "application/items.complete+json", }, body, }); } else if (mode === "image" || mode === "regions") { const bytes = await readFile(arg); if (mode === "regions") { response = await fetch(`${base}/find/v2/regions`, { method: "POST", headers: { "X-Api-Key": apiKey, "Content-Type": "image/jpeg", }, body: bytes, }); } else { const body = new FormData(); body.append("image", new Blob([bytes], { type: "image/jpeg" }), "query.jpg"); response = await fetch(`${base}/find/v1.1`, { method: "POST", headers: { "X-Api-Key": apiKey, Accept: "application/items.complete+json", }, body, }); } } else { console.error("Unknown mode"); process.exit(1); } const payload = await response.json().catch(() => ({})); if (!response.ok) { console.error(response.status, JSON.stringify(payload).slice(0, 300)); process.exit(1); } const list = payload.results || payload.regions || []; console.log(JSON.stringify({ status: response.status, count: list.length, id: payload.id ?? null }, null, 2)); } main();