forked from choraria/pptr-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckduckgo_profiles.js
40 lines (34 loc) · 1.13 KB
/
duckduckgo_profiles.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
const puppeteer = require("puppeteer-core");
const chrome = require("chrome-aws-lambda");
module.exports = async (req, res) => {
try {
const search = req.query.search;
const url = `https://duckduckgo.com/?q=${search}`;
const browser = await puppeteer.launch({
args: [...chrome.args, "--hide-scrollbars", "--disable-web-security"],
defaultViewport: chrome.defaultViewport,
executablePath: await chrome.executablePath,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
await page.goto(url);
const profiles = await page.$$eval(".about-profiles__link", (items) => {
const data = {};
items.forEach((item) => {
data[item.getAttribute("title")] = item.getAttribute("href");
});
return Object.keys(data).length !== 0 ? data : { message: "No profiles found" };
});
await browser.close();
res.statusCode = 200;
res.setHeader("Content-Type", `application/json`);
res.end(JSON.stringify(profiles));
} catch (err) {
console.log(err);
res.statusCode = 500;
res.json({
error: err.toString(),
});
res.end();
}
};