]> git.lizzy.rs Git - google_images.git/commitdiff
Fix crashes
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 5 Mar 2022 09:46:16 +0000 (10:46 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 5 Mar 2022 09:46:16 +0000 (10:46 +0100)
init.js
package-lock.json
package.json
test.js

diff --git a/init.js b/init.js
index cede7a1f412e629d10ba365592037ad5892ea8a2..d8ac6816c8e5eb0a9d816653eeaaa3ca6e206604 100644 (file)
--- a/init.js
+++ b/init.js
@@ -8,7 +8,7 @@ const debug = arg => {
 }
 
 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 =>
+       fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(debug(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
@@ -17,7 +17,7 @@ module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86
                        .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 => new Object({                              // map the parts of the init data we know/care about to something readable
+                       .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: {
@@ -36,6 +36,7 @@ module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86
                                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)
        )
 /*
 
index 2acc47d8d3e03edb3ac75e32d0bc20c83a15549e..e19f36067b44f26239b5bfe2768c3b5f17c84d89 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "google_images",
-  "version": "1.0.2",
+  "version": "1.0.3",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
index e5a021fac2d88c822210da6e0bfe497607377306..dbe808a5aee90806210b33436d2868a6d945d3a6 100644 (file)
@@ -1,10 +1,10 @@
 {
   "name": "free-google-images",
-  "version": "1.0.2",
+  "version": "1.0.3",
   "description": "Reverse Engineered Google Image Search API",
   "main": "init.js",
   "scripts": {
-    "test": "node test.js"
+    "test": "node test.js astolfo images"
   },
   "repository": {
     "type": "git",
diff --git a/test.js b/test.js
index 6d4e11e5c827e0c5a531c798d8bbba5b885c4017..6ad6496b93f8e5957f2e5e45e7bb521a740fdf15 100644 (file)
--- a/test.js
+++ b/test.js
@@ -1,5 +1,5 @@
 const {search} = require("./init.js")
 const util = require("util")
 
-search("astolfo+images")
+search(process.argv.slice(2).join("+"))
        .then(obj => console.log(util.inspect(obj, {showHidden: false, depth: null, colors: true})))