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