-
Notifications
You must be signed in to change notification settings - Fork 34
/
serp.js
73 lines (60 loc) · 1.66 KB
/
serp.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { model, openaiClient } = require("./api");
const { logger } = require("./logger")
const cheerio = require("cheerio");
const google = require("googlethis");
const queryDdg = async (keyword) => {
const url = `https://html.duckduckgo.com/html/?q=${keyword}`;
const html = await fetch(url);
const text = await html.text();
const $ = cheerio.load(text);
const weirdUrls = $(".result__a").slice(0, 5).map((i, el) => {
return $(el);
}).toArray();
const urls = weirdUrls.map((cheerioEl) => {
const url = cheerioEl.attr("href");
const urlObj = new URL(`https:` + url);
return {
title: cheerioEl.text(),
url: urlObj.searchParams.get("uddg"),
};
});
return urls;
};
const queryGoogle = async (keyword) => {
const search = await google.search(keyword);
return search.results.slice(0, 5);
};
const summarizeContent = async (url) => {
let html;
try {
html = await fetch(url);
} catch (e) {
logger.info(`Error fetching ${url}. Skipping...`);
return;
}
const text = await html.text();
const $ = cheerio.load(text);
const body = $("h1, h2, h3, h4, h5, h6, p");
const content = `
Helpfully summarize the following content:
${body.text().slice(0, 14000)}
`;
const messages = [
{
role: "system",
content:
"Act as a helpful program that can summarize the content of an article. You should be detailed as possible.",
},
{ role: "user", content },
];
const response = await openaiClient.chat.completions.create({
model: "gpt-3.5-turbo-16k",
messages,
});
return response.choices[0].message;
};
module.exports = {
queryGoogle,
queryDdg,
summarizeContent,
};