-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
35 lines (31 loc) · 919 Bytes
/
Copy pathcode
File metadata and controls
35 lines (31 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const https = require("https");
function search() {
const query = encodeURIComponent(args.join(" "));
return new Promise((resolve) => {
const url = `https://api.duckduckgo.com/?q=${query}&format=json&no_redirect=1`;
https.get(url, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const json = JSON.parse(data);
if (json.RelatedTopics && json.RelatedTopics.length > 0) {
const topic = json.RelatedTopics.find((t) => t.Text);
if (topic) {
resolve(topic.Text);
return;
}
}
resolve("No results found.");
} catch {
resolve("Error parsing search results.");
}
});
}).on("error", () => {
resolve("Error fetching search results.");
});
});
}
module.exports = search;