Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/images/google-image.js
1126 views
1
import cheerio from 'cheerio';
2
import got from 'got';
3
export default async function googleImage(query) {
4
const data = await got(`https://www.google.com/search?q=${query}&tbm=isch`, {
5
headers: {
6
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',
7
'accept-encoding': 'gzip, deflate, br',
8
'accept-language': 'en-US,en;q=0.9,id;q=0.8',
9
'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'
10
}
11
}).text();
12
const $ = cheerio.load(data);
13
const pattern = /\[1,\[0,"(?<id>[\d\w\-_]+)",\["https?:\/\/(?:[^"]+)",\d+,\d+\]\s?,\["(?<url>https?:\/\/(?:[^"]+))",\d+,\d+\]/gm;
14
const matches = $.html().matchAll(pattern);
15
const decodeUrl = (url) => decodeURIComponent(JSON.parse(`"${url}"`));
16
return [...matches]
17
.map(({ groups }) => decodeUrl(groups === null || groups === void 0 ? void 0 : groups.url))
18
.filter((v) => /.*\.jpe?g|png$/gi.test(v));
19
}
20
//# sourceMappingURL=google-image.js.map
21