]> git.lizzy.rs Git - google_images.git/commitdiff
Add safeSearch option
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 5 Mar 2022 19:52:02 +0000 (20:52 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 5 Mar 2022 19:52:02 +0000 (20:52 +0100)
README.md
init.js
package-lock.json
package.json

index 2dfea40868a88551c5bde1af0d4ac3fc974a221c..856c488dd73c7b1a124e4bebdf5555ad386d585a 100644 (file)
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ The usage of this API does NOT require an API key, nor is it rate limited.
 
 ## Usage
 
-Exports `search` function that takes query string as first argument and optionally user agent as second. Usage of the user agent argument has not been tested.
+Exports `search` function that takes query string as first argument, a boolean safeSearch as second and optionally user agent as second. Usage of the user agent argument has not been tested.
 `search` returns an promise that resolves to an array with objects like this (should be self-explanatory):
 
 ```js
@@ -31,5 +31,7 @@ const google_images = require("free-google-images");
 
 google_images.search("astolfo+images").then(results => results.forEach(r => console.log(r.image.url)))
 google_images.searchRandom("astolfo+images").then(result => console.log(result.image.url))
+
+google_images.searchRandom("hentai", true).then(result => console.log(result.image.url)) // no results because of safe search
 ```
 
diff --git a/init.js b/init.js
index ac455223c05f8ba699d419200a6e3fdfa4c8f111..c6fee778516a02cb40ae72b66b5d243fdc3f214c 100644 (file)
--- a/init.js
+++ b/init.js
@@ -2,39 +2,41 @@ const fetch = require("node-fetch")
 const cheerio = require("cheerio")
 const jsonic = require("jsonic")
 
-module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") =>
-       fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}}).then(res => res.text()).then(data =>
-               cheerio.load(data, null, false)                                               // parse HTML
-                       ("script")                                                                // find script tags
-                       .toArray()                                                                // convert cheerio list to array
-                       .map(script => script.children[0]?.data)                                  // map script tags to their inline code
-                       .filter(script => script?.startsWith("AF_initDataCallback"))              // find script that contains init data
-                       .map(script => script.slice("AF_initDataCallback(".length, -");".length)) // remove call to init function
-                       .map(jsonic)                                                              // jsonic is used because JSON.parse() requires strict JSON and eval() allows remote code execution
-                       .find(data => data.key == "ds:1")                                         // for some reason there are two init datas, one is empty tho
-                       .data[31][0][12][2].map(elem => elem[1] && new Object({                   // map the parts of the init data we know/care about to something readable
-                               image: {
-                                       url: elem[1][3][0],
-                                       size: {
-                                               width: elem[1][3][2],
-                                               height: elem[1][3][1],
+module.exports.search = (query, safeSearch = false, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") =>
+       fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query) + (safeSearch ? "&safe=active" : ""), {headers: {"User-Agent": userAgent}})
+               .then(res => res.text())
+               .then(data =>
+                       cheerio.load(data, null, false)                                               // parse HTML
+                               ("script")                                                                // find script tags
+                               .toArray()                                                                // convert cheerio list to array
+                               .map(script => script.children[0]?.data)                                  // map script tags to their inline code
+                               .filter(script => script?.startsWith("AF_initDataCallback"))              // find script that contains init data
+                               .map(script => script.slice("AF_initDataCallback(".length, -");".length)) // remove call to init function
+                               .map(jsonic)                                                              // jsonic is used because JSON.parse() requires strict JSON and eval() allows remote code execution
+                               .find(data => data.key == "ds:1")                                         // for some reason there are two init datas, one is empty tho
+                               .data[31][0][12][2].map(elem => elem[1] && new Object({                   // map the parts of the init data we know/care about to something readable
+                                       image: {
+                                               url: elem[1][3][0],
+                                               size: {
+                                                       width: elem[1][3][2],
+                                                       height: elem[1][3][1],
+                                               },
                                        },
-                               },
-                               preview: {
-                                       url: elem[1][2][0],
-                                       size: {
-                                               width: elem[1][2][2],
-                                               height: elem[1][2][1],
+                                       preview: {
+                                               url: elem[1][2][0],
+                                               size: {
+                                                       width: elem[1][2][2],
+                                                       height: elem[1][2][1],
+                                               },
                                        },
-                               },
-                               color: elem[1][6],          // probably average color of the image (used as placeholder while loading the image)
-                               link: elem[1][9][2003][2],
-                               title: elem[1][9][2003][3], // there is some more data in elem[1][9] that could potentially be useful
-                       }))
-                       .filter(elem => elem)
-       )
+                                       color: elem[1][6],          // probably average color of the image (used as placeholder while loading the image)
+                                       link: elem[1][9][2003][2],
+                                       title: elem[1][9][2003][3], // there is some more data in elem[1][9] that could potentially be useful
+                               }))
+                               .filter(elem => elem)
+               )
 
-module.exports.searchRandom = (query, userAgent) => module.exports.search(query, userAgent)
+module.exports.searchRandom = (query, safeSearch, userAgent) => module.exports.search(query, safeSearch, userAgent)
        .then(results => results[Math.floor(Math.random() * results.length)])
 
 /*
index e29f96cb6d1c90b69567124047b632be2a350e3c..4015a9add456eb694b4e08ed703937ebb3ddb7d2 100644 (file)
@@ -1,12 +1,12 @@
 {
-  "name": "google_images",
-  "version": "1.1.1",
+  "name": "free-google-images",
+  "version": "1.2.0",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
-      "name": "google_images",
-      "version": "1.0.0",
+      "name": "free-google-images",
+      "version": "1.1.1",
       "license": "MIT",
       "dependencies": {
         "cheerio": "^1.0.0-rc.10",
index 2642bc69243da923dc6f546a27fd98dcbab51c0e..dd92f74289062aa1f8a6865c78b79c125d54e837 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "free-google-images",
-  "version": "1.1.1",
+  "version": "1.2.0",
   "description": "Reverse Engineered Google Image Search API",
   "main": "init.js",
   "scripts": {