-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgetMainImage.js
48 lines (40 loc) · 1.37 KB
/
getMainImage.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
const puppeteerExtra = require("puppeteer-extra");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
const axios = require("axios");
const imageFinderFunction = require("./imageFinderFunction");
const getMainImage = async (parsedURL, shortenedURL, baseURL) => {
return await axios
.get(shortenedURL)
.then(async (res) => {
return imageFinderFunction(res.data, parsedURL, shortenedURL, baseURL);
})
.catch(async (error) => {
if (error) {
puppeteerExtra.use(StealthPlugin());
let mainImage;
await puppeteerExtra
.launch({
headless: true,
args: ["--disable-features=site-per-process"],
ignoreDefaultArgs: ["--enable-automation", "--disable-extensions"],
})
.then(async (browser) => {
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
await page.goto(shortenedURL);
await page.waitFor(5000);
const html = await page.content();
await browser.close();
const returnedMainImage = await imageFinderFunction(
html,
parsedURL,
shortenedURL,
baseURL
);
mainImage = returnedMainImage;
});
return mainImage;
}
});
};
module.exports = getMainImage;