-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
166 lines (129 loc) · 4.26 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var request = require('request');
var cheerio = require('cheerio');
var url = require('url');
function search(options, callback) {
var session = request.defaults({ jar : true });
var host = options.host || 'www.google.com';
var solver = options.solver;
var params = options.params || {};
var results = [];
params.hl = params.hl || options.lang || 'en';
if(options.age) params.tbs = 'qdr:' + options.age;
if(options.query) params.q = options.query;
params.start = params.start || 0;
getPage(params, function onPage(err, body) {
if(err) {
if(err.code !== 'ECAPTCHA' || !solver) return callback(err);
solveCaptcha(err.location, function(err, page) {
if(err) return callback(err);
onPage(null, page);
});
return;
}
var currentResults = extractResults(body);
var newResults = currentResults.filter(function(result) {
return results.indexOf(result) === -1;
});
newResults.forEach(function(result) {
callback(null, result['url'], result);
});
if(newResults.length === 0) {
return;
}
results = results.concat(newResults);
if(!options.limit || results.length < options.limit) {
params.start = results.length;
getPage(params, onPage);
}
});
function getPage(params, callback) {
session.get({
uri: 'https://' + host + '/search',
qs: params,
followRedirect: false
},
function(err, res) {
if(err) return callback(err);
if(res.statusCode === 302) {
var parsed = url.parse(res.headers.location, true);
if(parsed.pathname !== '/search') {
var err = new Error('Captcha');
err.code = 'ECAPTCHA';
err.location = res.headers.location;
this.abort();
return callback(err);
} else {
session.get({
uri: res.headers.location,
qs: params,
followRedirect: false
}, function(err, res) {
if(err) return callback(err);
callback(null, res.body);
});
return;
}
}
callback(null, res.body);
}
);
}
function extractResults(body) {
var results = [];
var $ = cheerio.load(body);
$('#search .g').each(function(i, elem) {
var item = {};
var elemUrl = $(this).find("h3 a");
var elemMeta = $(this).find(".slp");
var elemDesc = $(this).find(".st");
var parsedUrl = url.parse(elemUrl.attr("href"), true);
if (parsedUrl.pathname === '/url') {
item['url'] = parsedUrl.query.q;
}
item['title'] = elemUrl.text();
item['meta'] = elemMeta.text();
item['desc'] = elemDesc.text();
results.push(item);
});
return results;
}
function solveCaptcha(captchaUrl, callback) {
var tmp = url.parse(captchaUrl);
var baseUrl = url.format({
protocol: tmp.protocol,
hostname: tmp.host,
});
// Fetch captcha page
session.get(captchaUrl, function(err, res) {
if(err) return callback(err);
var $ = cheerio.load(res.body);
var captchaId = $('input[name=id]').attr('value');
var continueUrl = $('input[name=continue]').attr('value');
var formAction = $('form').attr('action');
var imgSrc = $('img').attr('src');
// Fetch captcha image
session.get({uri: baseUrl + imgSrc, encoding: null}, function(err, res) {
if(err) return callback(err);
// Send to solver
solver.solve(res.body, function(err, id, solution) {
if(err) return callback(err);
// Try solution
session.get({
uri: baseUrl + '/sorry/' + formAction,
qs: {
id: captchaId,
captcha: solution,
continue: continueUrl
}
},
function(err, res) {
if(res.statusCode !== 200) return callback(new Error('Captcha decoding failed'));
callback(null, res.body);
}
);
});
});
});
}
}
module.exports.search = search;