async-g-i-s
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -11,3 +11,2 @@ interface Result { | ||
userAgent?: string; | ||
newRegex?: boolean; | ||
} | ||
@@ -19,9 +18,8 @@ | ||
* @async | ||
* @param {String} searchTerm Search term for search | ||
* @param {String} searchTerm Search term to search | ||
* @param {Options} options Options for search | ||
* @param {Object} options.query You can use custom query | ||
* @param {Object} options.query You can use a custom query | ||
* @param {String} options.userAgent User agent for request | ||
* @param {Boolean} options.newRegex Better filtering, if you want to find everything, set as false | ||
* @returns {Promise<Result[]>} Array of results | ||
*/ | ||
export default function gis(searchTerm: string, options?: Options): Promise<Result[]>; |
25
index.js
@@ -1,4 +0,3 @@ | ||
const fetch = global.fetch || require('node-fetch'); | ||
const LEGACY_REGEX = /\["(http.+?)",(\d+),(\d+)\]/g; | ||
const REGEX = /"(\bhttps?:\/\/[^"]+)",(\d+),(\d+)],null,\d+,"rgb\((\d+),(\d+),(\d+)\)"/g; | ||
const fetch = global.fetch || require("node-fetch"); | ||
const REGEX = /\["(\bhttps?:\/\/[^"]+)",(\d+),(\d+)\],null/g; | ||
@@ -11,19 +10,16 @@ /** | ||
* @param {Object} options Options for search | ||
* @param {Object} options.query You can use custom query | ||
* @param {Object} options.query You can use a custom query | ||
* @param {String} options.userAgent User agent for request | ||
* @param {Boolean} options.newRegex Better filtering, if you want to find everything, set as false | ||
* @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."); | ||
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/116.0.0.0 Safari/537.36', | ||
newRegex = true | ||
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')), | ||
regex = newRegex ? REGEX : LEGACY_REGEX, | ||
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 = []; | ||
@@ -33,8 +29,7 @@ | ||
while (result = regex.exec(content)) | ||
while (result = REGEX.exec(content)) | ||
results.push({ | ||
url: result[1], | ||
height: +result[2], | ||
width: +result[3], | ||
color: result.slice(4, 7).map(Number) | ||
width: +result[3] | ||
}); | ||
@@ -41,0 +36,0 @@ |
{ | ||
"name": "async-g-i-s", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Async Google Image Search", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ # async-g-i-s | ||
## Note for old Node.js versions (below 16): | ||
## Note for old Node.js versions (below 18): | ||
This package works with `fetch` api, if you use it on older versions of Node.js, you need to install `node-fetch` package defined in package.json. | ||
@@ -43,4 +43,3 @@ | ||
height: 1477, | ||
width: 1034, | ||
color: [ 48, 48, 48 ] | ||
width: 1034 | ||
}, | ||
@@ -50,4 +49,3 @@ { | ||
height: 350, | ||
width: 350, | ||
color: [ 16, 16, 16 ] | ||
width: 350 | ||
}, | ||
@@ -57,4 +55,3 @@ { | ||
height: 1908, | ||
width: 1656, | ||
color: [ 192, 192, 192 ] | ||
width: 1656 | ||
}, | ||
@@ -64,4 +61,3 @@ { | ||
height: 450, | ||
width: 300, | ||
color: [ 248, 248, 242 ] | ||
width: 300 | ||
}, | ||
@@ -71,4 +67,3 @@ { | ||
height: 486, | ||
width: 864, | ||
color: [ 152, 46, 56 ] | ||
width: 864 | ||
}, | ||
@@ -78,4 +73,3 @@ { | ||
height: 779, | ||
width: 500, | ||
color: [ 2, 8, 8 ] | ||
width: 500 | ||
}, | ||
@@ -85,4 +79,3 @@ { | ||
height: 390, | ||
width: 300, | ||
color: [ 24, 18, 11 ] | ||
width: 300 | ||
}, | ||
@@ -92,4 +85,3 @@ { | ||
height: 720, | ||
width: 1280, | ||
color: [ 50, 252, 172 ] | ||
width: 1280 | ||
} | ||
@@ -100,9 +92,8 @@ ] | ||
```js | ||
gis("akif", { | ||
gis("", { | ||
query: {}, | ||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' | ||
newRegex: true | ||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' | ||
}); | ||
``` | ||
- **New regex**: it is more stable, **and turns with colors** (that means the colors wont come with old regex). But it is slower than old regex. | ||
- **User agent**: it is default user agent of Chrome. You can change it to anything you want. | ||
@@ -109,0 +100,0 @@ - **Query**: You can add additional queries to URL. For example, you can add `safe: "on"` to filter out NSFW results. |
13
test.js
@@ -5,3 +5,3 @@ const gis = require('.'); | ||
test("new regex", async () => { | ||
test("common test", async () => { | ||
const results = await gis("akif9748"); | ||
@@ -12,13 +12,2 @@ assert(results.length > 0); | ||
assert(results[0].width > 0); | ||
assert(results[0].color[0] > 0); | ||
}); | ||
test("legacy regex", async () => { | ||
const results = await gis("akif9748", { newRegex: false }); | ||
assert(results.length > 0); | ||
assert(results[0].url.length > 0); | ||
assert(results[0].height > 0); | ||
assert(results[0].width > 0); | ||
assert(!results[0].color[0]); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6635
60
98