-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
36 lines (31 loc) · 1.35 KB
/
index.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
const fetch = global.fetch || require("node-fetch");
const REGEX = /\["(\bhttps?:\/\/[^"]+)",(\d+),(\d+)\],null/g;
/**
*
* Async version of g-i-s module
* @async
* @param {String} searchTerm Search term to search
* @param {Object} options Options for search
* @param {Object} options.query You can use a custom query
* @param {String} options.userAgent User agent for request
* @returns {Promise<[{url: string, height: number, width: number }]>} Array of results
*/
module.exports = async function gis(searchTerm, options = {}) {
if (!searchTerm || typeof searchTerm !== "string") throw new TypeError("searchTerm must be a string.");
if (typeof options !== "object") throw new TypeError("options argument must be an object.");
const {
query = {},
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
} = options,
body = await fetch(`http://www.google.com/search?${new URLSearchParams({ ...query, tbm: "isch", q: searchTerm })}`, { headers: { "User-Agent": userAgent } }).then(res => res.text()),
content = body.slice(body.lastIndexOf("ds:1"), body.lastIndexOf("sideChannel")),
results = [];
let result;
while (result = REGEX.exec(content))
results.push({
url: result[1],
height: +result[2],
width: +result[3]
});
return results;
}