Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@bochilteam/scraper/lib/cjs/images/google-image.js
1126 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
const cheerio_1 = __importDefault(require("cheerio"));
7
const got_1 = __importDefault(require("got"));
8
async function googleImage(query) {
9
const data = await (0, got_1.default)(`https://www.google.com/search?q=${query}&tbm=isch`, {
10
headers: {
11
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
12
'accept-encoding': 'gzip, deflate, br',
13
'accept-language': 'en-US,en;q=0.9,id;q=0.8',
14
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
15
}
16
}).text();
17
const $ = cheerio_1.default.load(data);
18
const pattern = /\[1,\[0,"(?<id>[\d\w\-_]+)",\["https?:\/\/(?:[^"]+)",\d+,\d+\]\s?,\["(?<url>https?:\/\/(?:[^"]+))",\d+,\d+\]/gm;
19
const matches = $.html().matchAll(pattern);
20
const decodeUrl = (url) => decodeURIComponent(JSON.parse(`"${url}"`));
21
return [...matches]
22
.map(({ groups }) => decodeUrl(groups === null || groups === void 0 ? void 0 : groups.url))
23
.filter((v) => /.*\.jpe?g|png$/gi.test(v));
24
}
25
exports.default = googleImage;
26
//# sourceMappingURL=google-image.js.map
27