Path: blob/master/node_modules/@bochilteam/scraper/lib/cjs/images/google-image.js
1126 views
"use strict";1var __importDefault = (this && this.__importDefault) || function (mod) {2return (mod && mod.__esModule) ? mod : { "default": mod };3};4Object.defineProperty(exports, "__esModule", { value: true });5const cheerio_1 = __importDefault(require("cheerio"));6const got_1 = __importDefault(require("got"));7async function googleImage(query) {8const data = await (0, got_1.default)(`https://www.google.com/search?q=${query}&tbm=isch`, {9headers: {10accept: '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',11'accept-encoding': 'gzip, deflate, br',12'accept-language': 'en-US,en;q=0.9,id;q=0.8',13'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'14}15}).text();16const $ = cheerio_1.default.load(data);17const pattern = /\[1,\[0,"(?<id>[\d\w\-_]+)",\["https?:\/\/(?:[^"]+)",\d+,\d+\]\s?,\["(?<url>https?:\/\/(?:[^"]+))",\d+,\d+\]/gm;18const matches = $.html().matchAll(pattern);19const decodeUrl = (url) => decodeURIComponent(JSON.parse(`"${url}"`));20return [...matches]21.map(({ groups }) => decodeUrl(groups === null || groups === void 0 ? void 0 : groups.url))22.filter((v) => /.*\.jpe?g|png$/gi.test(v));23}24exports.default = googleImage;25//# sourceMappingURL=google-image.js.map2627