]> git.lizzy.rs Git - google_images.git/blob - init.js
d8ac6816c8e5eb0a9d816653eeaaa3ca6e206604
[google_images.git] / init.js
1 const fetch = require("node-fetch")
2 const cheerio = require("cheerio")
3 const jsonic = require("jsonic")
4
5 const debug = arg => {
6         console.log(arg)
7         return arg
8 }
9
10 module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") =>
11         fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(debug(query)), {headers: {"User-Agent": userAgent}}).then(res => res.text()).then(data =>
12                 cheerio.load(data, null, false)                                               // parse HTML
13                         ("script")                                                                // find script tags
14                         .toArray()                                                                // convert cheerio list to array
15                         .map(script => script.children[0]?.data)                                  // map script tags to their inline code
16                         .filter(script => script?.startsWith("AF_initDataCallback"))              // find script that contains init data
17                         .map(script => script.slice("AF_initDataCallback(".length, -");".length)) // remove call to init function
18                         .map(jsonic)                                                              // jsonic is used because JSON.parse() requires strict JSON and eval() allows remote code execution
19                         .find(data => data.key == "ds:1")                                         // for some reason there are two init datas, one is empty tho
20                         .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
21                                 image: {
22                                         url: elem[1][3][0],
23                                         size: {
24                                                 width: elem[1][3][2],
25                                                 height: elem[1][3][1],
26                                         },
27                                 },
28                                 preview: {
29                                         url: elem[1][2][0],
30                                         size: {
31                                                 width: elem[1][2][2],
32                                                 height: elem[1][2][1],
33                                         },
34                                 },
35                                 color: elem[1][6],          // probably average color of the image (used as placeholder while loading the image)
36                                 link: elem[1][9][2003][2],
37                                 title: elem[1][9][2003][3], // there is some more data in elem[1][9] that could potentially be useful
38                         }))
39                         .filter(elem => elem)
40         )
41 /*
42
43 In case google makes changes, here are some snippets used to reverse engineer the format:
44
45 1. Find which script contains the init data (use the query astolfo+images for this)
46 -----------------------------------------------------------------------------------
47
48         scripts.find(script => script.search("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/622220/f4d2d4074167411a7e15b9a845cf18b434c02af3.jpg") >= 0)
49
50 2. Reverse engineer format of init data passed to AF_initDataCallback
51 ---------------------------------------------------------------------
52
53 const findStrings = (obj, path = "") => {
54         let found = []
55
56         for (k in obj) {
57                 let v = obj[k]
58                 let t = typeof v
59                 let p = path + "." + k
60
61                 if (t == "object")
62                         found = found.concat(findStrings(v, p))
63                 else if (t == "string")
64                         found.push([v, p])
65         }
66
67         return found
68 }
69
70         console.log(findStrings(initData))
71
72 */