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

diff --git a/init.js b/init.js
index fd566ad287186d172c40fe3cc1ad7fb3e8b98126..cede7a1f412e629d10ba365592037ad5892ea8a2 100644 (file)
--- a/init.js
+++ b/init.js
@@ -2,36 +2,41 @@ const fetch = require("node-fetch")
 const cheerio = require("cheerio")
 const jsonic = require("jsonic")
 
+const debug = arg => {
+       console.log(arg)
+       return 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 =>
-               jsonic( // jsonic is used because JSON.parse() requires strict JSON and eval() allows for remote code execution
-                       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
-                       .find(script => script?.startsWith("AF_initDataCallback")) // find script that contains init data
-                       .slice("AF_initDataCallback(".length, -");".length)        // remove call to init function
-               ).data[31][0][12][2].map(elem => 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],
+               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 => 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], // average color of the image, probably (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
-               }))
+                               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
+                       }))
        )
-
 /*
 
 In case google makes changes, here are some snippets used to reverse engineer the format:
index 7af8f433301d14c9f869749e67c08e1026d9b1fb..2acc47d8d3e03edb3ac75e32d0bc20c83a15549e 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "google_images",
-  "version": "1.0.1",
+  "version": "1.0.2",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
index 2a702be8a5b09488b8a94179046f462914da145a..e5a021fac2d88c822210da6e0bfe497607377306 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "free-google-images",
-  "version": "1.0.1",
+  "version": "1.0.2",
   "description": "Reverse Engineered Google Image Search API",
   "main": "init.js",
   "scripts": {