]> git.lizzy.rs Git - google_images.git/blob - init.js
Initial commit
[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                 
27                 if (t == "object")
28                         found = found.concat(findStrings(v, p));
29                 else if (t == "string")
30                         found.push([v, p]);             
31         }
32
33         return found;
34 };
35
36         .then(findStrings)
37
38 Dump data
39 ---------
40         const util = require("util");
41
42         .then(obj => util.inspect(obj, {showHidden: false, depth: 3, colors: true}))
43         .then(console.log)
44 */
45
46 const makeImage = elem => {
47         return {
48                 url: elem[0],
49                 size: {
50                         width: elem[1],
51                         height: elem[2],
52                 }
53         }
54 };
55
56 module.exports.search = (query, userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0") => {
57         return fetch("https://www.google.com/search?tbm=isch&q=" + encodeURIComponent(query), {headers: {"User-Agent": userAgent}})
58                 .then(res => res.text())
59                 .then(data => cheerio.load(data, null, false))
60                 .then(content => content("script"))
61                 .then(scripts => scripts.toArray())
62                 .then(scripts => scripts.map(script => script.children[0]?.data))
63                 .then(scripts => scripts.filter(script => script?.search("http") >= 0))
64                 .then(scripts => scripts[4])
65                 .then(script => script.slice("AF_initDataCallback(".length, script.length - ");".length))
66                 .then(jsonic)
67                 .then(data => data.data[31][0][12][2])
68                 .then(data => data.map(elem => elem[1]))
69                 .then(data => data.map(elem => new Object({
70                         preview: makeImage(elem[2]),
71                         image: makeImage(elem[3]),
72                         color: elem[6],
73                         link: elem[9][2003][2],
74                         title: elem[9][2003][3],
75                 })));
76 }