-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPic.js
31 lines (28 loc) · 830 Bytes
/
getPic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const https = require('https');
const htmlparser = require("htmlparser2");
//"https://www.google.com/search?tbm=isch&q=elon+musk&oq=elon+musk&"
module.exports=function(search, cb){
let links = [];
let parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "img" && attribs.alt === 'Image result for' + search){
links.push(attribs.src);
}
}
}, {decodeEntities: true});
let searchbit = search.split(' ').join('+');
let url ="https://www.google.com/search?tbm=isch&safe=search&q=" + searchbit +"&oq=" +searchbit;
let dataBuff = "";
https.get(url, (res) => {
res.on('data', (d) => {
dataBuff += d;
});
res.on('end', () =>{
parser.write(dataBuff);
parser.end();
cb(links[(Math.random()*links.length) | 0]);
});
}).on('error', (e) => {
console.error(e);
});
}