]> git.lizzy.rs Git - google_images.git/commitdiff
Style fixes
authorElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 4 Mar 2022 22:01:29 +0000 (23:01 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Fri, 4 Mar 2022 22:01:29 +0000 (23:01 +0100)
init.js
test.js

diff --git a/init.js b/init.js
index d5f3e5236f43a56d0877285d17941a53df49e4dd..f22ba4ed927399e39ebae537bd96441929d96858 100644 (file)
--- a/init.js
+++ b/init.js
@@ -1,6 +1,38 @@
-const fetch = require("node-fetch");
-const cheerio = require("cheerio");
-const jsonic = require("jsonic");
+const fetch = require("node-fetch")
+const cheerio = require("cheerio")
+const jsonic = require("jsonic")
+
+const makeImage = elem => {
+       return {
+               url: elem[0],
+               size: {
+                       width: elem[2],
+                       height: elem[1],
+               }
+       }
+}
+
+module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
+       return 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))
+               .then(content => content("script"))
+               .then(scripts => scripts.toArray())
+               .then(scripts => scripts.map(script => script.children[0]?.data))
+               .then(scripts => scripts.filter(script => script?.search("http") >= 0))
+               .then(scripts => scripts[4])
+               .then(script => script.slice("AF_ini2tDataCallback(".length, script.length - ");".length))
+               .then(jsonic)
+               .then(data => data.data[31][0][12][2])
+               .then(data => data.map(elem => elem[1]))
+               .then(data => data.map(elem => new Object({
+                       preview: makeImage(elem[2]),
+                       image: makeImage(elem[3]),
+                       color: elem[6],
+                       link: elem[9][2003][2],
+                       title: elem[9][2003][3],
+               })))
+}
 
 /*
 In case google makes minor changes, here are some snippets used to reverse engineer the format:
@@ -33,43 +65,4 @@ const findStrings = (obj, path = "") => {
 };
 
        .then(findStrings)
-
-Dump data
----------
-       const util = require("util");
-
-       .then(obj => util.inspect(obj, {showHidden: false, depth: 3, colors: true}))
-       .then(console.log)
 */
-
-const makeImage = elem => {
-       return {
-               url: elem[0],
-               size: {
-                       width: elem[2],
-                       height: elem[1],
-               }
-       }
-};
-
-module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
-       return 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))
-               .then(content => content("script"))
-               .then(scripts => scripts.toArray())
-               .then(scripts => scripts.map(script => script.children[0]?.data))
-               .then(scripts => scripts.filter(script => script?.search("http") >= 0))
-               .then(scripts => scripts[4])
-               .then(script => script.slice("AF_ini2tDataCallback(".length, script.length - ");".length))
-               .then(jsonic)
-               .then(data => data.data[31][0][12][2])
-               .then(data => data.map(elem => elem[1]))
-               .then(data => data.map(elem => new Object({
-                       preview: makeImage(elem[2]),
-                       image: makeImage(elem[3]),
-                       color: elem[6],
-                       link: elem[9][2003][2],
-                       title: elem[9][2003][3],
-               })));
-}
diff --git a/test.js b/test.js
index 9e1533a6da0d7649b3d14d2b073aef00b4645ffe..be0dae75555bcec52b1e9bb79102da14b09e3363 100644 (file)
--- a/test.js
+++ b/test.js
@@ -1,5 +1,5 @@
-const util = require("util");
-const {search} = require("./init.js");
+const {search} = require("./init.js")
+const util = require("util")
 
 search("astolfo+images")
        .then(obj => util.inspect(obj, {showHidden: false, depth: null, colors: true}))