]> git.lizzy.rs Git - google_images.git/blob - init.js
Fix width/height being swapped
[google_images.git] / init.js
1 const fetch = require("node-fetch");
2 const cheerio = require("cheerio");
3 const jsonic = require("jsonic");
4
5 /*
6 In case google makes minor changes, here are some snippets used to reverse engineer the format:
7
8
9 Find which script to use (use the query astolfo+images for this)
10 ----------------------------------------------------------------
11
12         .then(scripts => scripts.filter(script => script.children[0]?.data?.search("http") >= 0))
13         .then(scripts => scripts.reduce((a, b, i) => b.children[0]?.data?.search("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/622220/f4d2d4074167411a7e15b9a845cf18b434c02af3.jpg") >= 0 ? i : a), -1)
14
15 Reverse engineer data passed to AF_initDataCallback
16 ---------------------------------------------------
17
18 const findStrings = (obj, path = "") => {
19         let found = [];
20
21         for (k in obj) {
22                 let v = obj[k];
23                 let t = typeof v;
24                 let p = path + "." + k
25
26                 if (t == "object")
27                         found = found.concat(findStrings(v, p));
28                 else if (t == "string")
29                         found.push([v, p]);
30         }
31
32         return found;
33 };
34
35         .then(findStrings)
36
37 Dump data
38 ---------
39         const util = require("util");
40
41         .then(obj => util.inspect(obj, {showHidden: false, depth: 3, colors: true}))
42         .then(console.log)
43 */
44
45 const makeImage = elem => {
46         return {
47                 url: elem[0],
48                 size: {
49                         width: elem[2],
50                         height: elem[1],
51                 }
52         }
53 };
54
55 module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
56         return fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}})
57                 .then(res => res.text())
58                 .then(data => cheerio.load(data, null, false))
59                 .then(content => content("script"))
60                 .then(scripts => scripts.toArray())
61                 .then(scripts => scripts.map(script => script.children[0]?.data))
62                 .then(scripts => scripts.filter(script => script?.search("http") >= 0))
63                 .then(scripts => scripts[4])
64                 .then(script => script.slice("AF_ini2tDataCallback(".length, script.length - ");".length))
65                 .then(jsonic)
66                 .then(data => data.data[31][0][12][2])
67                 .then(data => data.map(elem => elem[1]))
68                 .then(data => data.map(elem => new Object({
69                         preview: makeImage(elem[2]),
70                         image: makeImage(elem[3]),
71                         color: elem[6],
72                         link: elem[9][2003][2],
73                         title: elem[9][2003][3],
74                 })));
75 }