Forked from altra/research
factcheck.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkGoogleFactCheck = checkGoogleFactCheck;
async function checkGoogleFactCheck(claim, apiKey) {
const params = new URLSearchParams({ query: claim, languageCode: "en" });
if (apiKey)
params.set("key", apiKey);
const url = `https://factchecktools.googleapis.com/v1alpha1/claims:search?${params}`;
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 10_000);
try {
const res = await fetch(url, {
signal: ctrl.signal,
headers: { "User-Agent": "research-plugin/1.0" },
});
if (!res.ok)
throw new Error(`Fact Check API HTTP ${res.status}`);
const data = await res.json();
const factChecks = [];
for (const c of data.claims ?? []) {
for (const r of c.claimReview ?? []) {
factChecks.push({
publisher: r.publisher?.name ?? r.publisher?.site ?? "Unknown",
rating: r.textualRating ?? "Unknown",
url: r.url ?? "",
reviewDate: r.reviewDate ?? "",
});
}
}
return { claim, factChecks, covered: factChecks.length > 0 };
}
finally {
clearTimeout(timer);
}
}