This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleIt.js
127 lines (111 loc) · 3.37 KB
/
GoogleIt.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var request = require('request')
var fs = require('fs')
var cheerio = require('cheerio')
var colors = require('colors')
const util = require('util')
const exec = require('child_process').exec
// NOTE:
// I chose the User-Agent value from http://www.browser-info.net/useragents
// Not setting one causes Google search to not display results
function logIt(message, disableConsole) {
if (!disableConsole) {
console.log(message)
}
}
function saveToFile(output, results) {
if (output !== undefined) {
fs.writeFile(output, JSON.stringify(results, null, 2), 'utf8', (err) => {
if (err) {
console.err('Error writing to file ' + output + ': ' + err)
}
})
}
}
function errorTryingToOpen(error, stdout, stderr) {
if (error) {
console.log(`Error trying to open link in browser: ${error}`)
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
}
}
function openInBrowser(open, results) {
if (open !== undefined) {
// open is the first X number of links to open
results.slice(0, open).forEach((result, i) => {
exec(`open ${result.link}`, errorTryingToOpen);
})
}
}
function googleIt(config) {
var {query, limit, userAgent, output, open, options = {}} = config
var defaultOptions = {
url: `https://www.google.com/search?q=${query}&gws_rd=ssl&num=${limit || 10}`,
headers: {
'User-Agent': (userAgent || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0')
}
};
return new Promise((resolve, reject) => {
request(Object.assign({}, defaultOptions, options), (error, response, body) => {
if (error) {
return reject("Error making web request: " + error, null)
} else {
var results = getResults(body, config['no-display'], config['disableConsole'], config['only-urls'])
saveToFile(output, results)
openInBrowser(open, results)
return resolve(results);
}
});
});
}
function getSnippet(elem) {
return elem['children'].map(child => {
if (child.data === null) {
return child.children.map(c => c.data)
} else {
return child.data
}
}).join('')
}
function display(results, disableConsole, onlyUrls) {
logIt("\n", disableConsole)
results.forEach((result, i) => {
if (onlyUrls) {
logIt(result.link.green, disableConsole)
} else {
logIt(result.title.blue, disableConsole)
logIt(result.link.green, disableConsole)
logIt(result.snippet, disableConsole)
logIt("\n", disableConsole)
}
})
}
function getResults(data, noDisplay, disableConsole, onlyUrls) {
const $ = cheerio.load(data)
var results = []
// result titles
var titles = $('div.rc > h3.r > a').contents()
titles.each((index, elem) => {
results.push({"title": elem.data})
})
// result links
$('div.rc > h3.r > a').map((index, elem) => {
if (index < results.length) {
results[index] = Object.assign(results[index], {"link": elem['attribs']['href']})
}
})
// result snippets
$('div.rc > div.s > div > span.st').map((index, elem) => {
if (index < results.length) {
var snippet = getSnippet(elem)
results[index] = Object.assign(results[index], {snippet: snippet})
}
})
if (onlyUrls) {
results = results.map(r => ({link: r.link}))
}
if (!noDisplay) {
display(results, disableConsole, onlyUrls)
}
return results
}
module.exports = googleIt