n8n-nodes-duckduckgo-search
Version:
AI Agent-ready n8n community node for DuckDuckGo search. Search the web, images, news, and videos with no API key required and no outbound telemetry. Optionally fetch and extract the main text of result pages or any URL, and get DuckDuckGo Instant Answers
95 lines (94 loc) • 3.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInstantAnswer = getInstantAnswer;
const axios_1 = __importDefault(require("axios"));
const constants_1 = require("./constants");
const DEFAULT_TIMEOUT = 8000;
function emptyResult() {
return {
heading: '', abstract: '', abstractSource: '', abstractURL: '',
answer: '', answerType: '', definition: '', definitionSource: '',
definitionURL: '', image: '', type: '', relatedTopics: [],
};
}
function flattenRelatedTopics(raw) {
if (!Array.isArray(raw))
return [];
const out = [];
for (const item of raw) {
if (item && typeof item.Text === 'string') {
out.push({ text: item.Text, url: typeof item.FirstURL === 'string' ? item.FirstURL : '' });
}
else if (item && Array.isArray(item.Topics)) {
for (const sub of item.Topics) {
if (sub && typeof sub.Text === 'string') {
out.push({ text: sub.Text, url: typeof sub.FirstURL === 'string' ? sub.FirstURL : '' });
}
}
}
}
return out;
}
function absoluteImage(image) {
if (typeof image !== 'string' || image === '')
return '';
return image.startsWith('http') ? image : `https://duckduckgo.com${image}`;
}
async function getInstantAnswer(query, options = {}) {
var _a, _b;
const timeout = (_a = options.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
if (!query || typeof query !== 'string' || query.trim() === '') {
return { ...emptyResult(), error: 'No query provided' };
}
try {
const params = new URLSearchParams({
q: query,
format: 'json',
no_html: '1',
skip_disambig: '1',
t: 'n8n-nodes-duckduckgo-search',
});
const response = await axios_1.default.get(`https://api.duckduckgo.com/?${params.toString()}`, {
timeout,
responseType: 'json',
headers: {
'User-Agent': constants_1.BROWSER_USER_AGENT,
'Accept': 'application/json',
},
});
const d = response.data || {};
return {
heading: d.Heading || '',
abstract: d.AbstractText || '',
abstractSource: d.AbstractSource || '',
abstractURL: d.AbstractURL || '',
answer: typeof d.Answer === 'string' ? d.Answer : (d.Answer ? JSON.stringify(d.Answer) : ''),
answerType: d.AnswerType || '',
definition: d.Definition || '',
definitionSource: d.DefinitionSource || '',
definitionURL: d.DefinitionURL || '',
image: absoluteImage(d.Image),
type: d.Type || '',
relatedTopics: flattenRelatedTopics(d.RelatedTopics),
};
}
catch (error) {
let message;
if ((error === null || error === void 0 ? void 0 : error.code) === 'ECONNABORTED') {
message = `Timed out after ${timeout}ms`;
}
else if ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.status) {
message = `HTTP ${error.response.status}`;
}
else if (error === null || error === void 0 ? void 0 : error.code) {
message = String(error.code);
}
else {
message = error instanceof Error ? error.message : 'Unknown error';
}
return { ...emptyResult(), error: message };
}
}