#!/usr/bin/env node
"use strict";
const BASE = "https://api.fennec-asr.com/api/v1";
const KEY = process.env.ASR_API_KEY;
const AUDIO = process.argv[2] || "https://upload.wikimedia.org/wikipedia/commons/3/3f/En-History_of_Corpus_Christi%2C_Texas.ogg";
const INTERVAL = 3000, MAX = 300000;
const wait = (ms) => new Promise(r => setTimeout(r, ms));
(async () => {
if (!KEY) throw new Error("Set ASR_API_KEY");
const submit = await fetch(`${BASE}/transcribe/url`, {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": KEY },
body: JSON.stringify({ audio: AUDIO, context: "testing with a node script" }),
});
if (!submit.ok) throw new Error(`Submit ${submit.status}: ${await submit.text()}`);
const { job_id } = await submit.json();
if (!job_id) throw new Error("No job_id");
const start = Date.now();
for (;;) {
const res = await fetch(`${BASE}/transcribe/status/${job_id}`, { headers: { "X-API-Key": KEY } });
if (!res.ok) throw new Error(`Status ${res.status}: ${await res.text()}`);
const s = await res.json();
if (s.status === "completed") return void console.log(s.transcript || "");
if (s.status === "failed") { console.error("Failed:", s.transcript || ""); process.exit(1); }
if (Date.now() - start > MAX) { console.error("Timed out."); process.exit(1); }
await wait(INTERVAL);
}
})().catch(e => { console.error(e.message || e); process.exit(1); });