-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.js
37 lines (34 loc) · 1006 Bytes
/
google.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
var http = require('http');
var htmlspecialchars_decode = require("./htmlspecialchars_decode");
module.exports = {
search: function (keyword, cb){
var fullPath = "http://www.google.com.hk/search?q=" + keyword;
var client = http.createClient(80, "google.com");
var req = client.request("GET", fullPath, {
"Host": "google.com",
"Connection": "close"
});
req.addListener('response', function(res) {
res.setEncoding("UTF-8");
var temp = "";
var cLength = 0;
res.addListener('data', function(chunk) {
temp += chunk;
});
res.addListener('end', function() { //full packet received
var resultPattern = /<h3 class=\"r\"><a href=\"([^\"]*)\"[^>]*>(.*?)<\/a>/gm;
var results = [];
while (matches = resultPattern.exec(temp)){
results.push({
url: htmlspecialchars_decode(matches[1]),
title: matches[2]
});
}
if (typeof cb == "function"){
cb(results);
}
});
});
req.end();
}
};