Skip to content

Commit

Permalink
Merge pull request #17 from choraria/dev
Browse files Browse the repository at this point in the history
adding duckduckgo_profiles.js
  • Loading branch information
choraria committed Oct 10, 2021
2 parents 2467fc5 + e9820c5 commit 2c0ed45
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
31 changes: 29 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ a free and open-source api that runs [puppeteer](https://developers.google.com/w
3. [trace](#trace)
4. [pdf](#pdf)
5. [meta](#meta)
6. [version](#version)
6. [duckduckgo_profiles](#duckduckgo_profiles)
7. [version](#version)
- [contributing](#contributing)
- [credits](#credits)
- [license](#license)
Expand Down Expand Up @@ -139,6 +140,32 @@ View the trace in [timeline-viewer](https://github.com/ChromeDevTools/timeline-v

</details>

### duckduckgo_profiles

- task: retrieve the ["about" profiles links](https://user-images.githubusercontent.com/37455462/136704745-afe6cdca-ae73-47e4-a09c-6bc405894af3.png) (website, twitter, facebook, instagram, youtube etc.) of a search query from duckduckgo
- method: `GET`
- api: `https://pptr.io/api/duckduckgo_profiles?search=taylor+swift`
- source: [duckduckgo_profiles.js](/api/duckduckgo_profiles.js)

<details>
<summary>sample output of the duckduckgo_profiles endpoint</summary>

```json
{
"Website": "https://taylorswift.com",
"Wikipedia": "https://en.wikipedia.org/wiki/Taylor_Swift",
"Twitter": "https://twitter.com/taylorswift13",
"Instagram": "https://instagram.com/taylorswift",
"Facebook": "https://facebook.com/TaylorSwift",
"Spotify": "https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02",
"IMDb": "https://www.imdb.com/name/nm2357847",
"YouTube": "https://youtube.com/channel/UCqECaJ8Gagnn7YCbPEzWH6g",
"SoundCloud": "https://soundcloud.com/taylorswiftofficial"
}
```

</details>

### version

- task: fetch browser user agent / chromium version
Expand Down Expand Up @@ -201,4 +228,4 @@ SOFTWARE.

# host your own

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fchoraria%2Fpptr-io)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fchoraria%2Fpptr-io)
40 changes: 40 additions & 0 deletions api/duckduckgo_profiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
};
24 changes: 11 additions & 13 deletions api/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chrome = require("chrome-aws-lambda");
module.exports = async (req, res) => {
try {
const url = req.query.url;

const browser = await puppeteer.launch({
args: [...chrome.args, "--hide-scrollbars", "--disable-web-security"],
defaultViewport: chrome.defaultViewport,
Expand All @@ -13,19 +13,17 @@ module.exports = async (req, res) => {
});
const page = await browser.newPage();

await page.goto(url, {
// waitUntil: "networkidle0",
});

await page.goto(url);

const metaData = await page.evaluate(() => {
const data = {};
const metaTags = document.querySelectorAll('meta');
metaTags.forEach((tag, i) => {
const key = tag.getAttribute('name') ? tag.getAttribute('name') : (tag.getAttribute('property') ? tag.getAttribute('property') : (tag.getAttribute('http-equiv') ? tag.getAttribute('http-equiv') : tag.getAttribute('itemprop')));
tag.getAttribute('charset') ? data["charset"] = tag.getAttribute('charset') : data[key == null ? i : key] = tag.getAttribute('content');
});
return data;
});
const data = {};
const metaTags = document.querySelectorAll("meta");
metaTags.forEach((tag, i) => {
const key = tag.getAttribute('name') ? tag.getAttribute('name') : (tag.getAttribute('property') ? tag.getAttribute('property') : (tag.getAttribute('http-equiv') ? tag.getAttribute('http-equiv') : tag.getAttribute('itemprop')));
tag.getAttribute('charset') ? data["charset"] = tag.getAttribute('charset') : data[key == null ? i : key] = tag.getAttribute('content');
});
return data;
});

await browser.close();

Expand Down
4 changes: 1 addition & 3 deletions api/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ module.exports = async (req, res) => {
});
const page = await browser.newPage();

await page.goto(url, {
// waitUntil: "networkidle0",
});
await page.goto(url);
const metrics = await page.metrics();
await browser.close();

Expand Down
2 changes: 1 addition & 1 deletion api/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = async (req, res) => {
await page.goto(url, {
waitUntil: "networkidle0",
});

const pdf = await page.pdf({
pageRanges: "1",
printBackground: true,
Expand Down
2 changes: 1 addition & 1 deletion api/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ALLOWED_FILE_TYPES = ["jpeg", "webp", "png"];
module.exports = async (req, res) => {
try {
const url = req.query.url;

const fullPage = req.query.fullPage ? (req.query.fullPage.toString().toLowerCase() == "true" ? true : false) : false;
const screenshotFileType = req.query.type ? req.query.type.toString().toLowerCase() : "png";
const fileType = ALLOWED_FILE_TYPES.includes(screenshotFileType) ? screenshotFileType : "png";
Expand Down
2 changes: 1 addition & 1 deletion api/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ module.exports = async (req, res) => {
version: userAgent.toString(),
});
res.end();
}
};

1 comment on commit 2c0ed45

@vercel
Copy link

@vercel vercel bot commented on 2c0ed45 Oct 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.