Skip to content

Commit b30dec6

Browse files
committed
use google npm package for FeelingLucky, fixes oftn-oswg#55
1 parent ef20b3d commit b30dec6

File tree

2 files changed

+20
-36
lines changed

2 files changed

+20
-36
lines changed

lib/feelinglucky/index.js

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
1-
var HTTP = require("http");
1+
var google = require('google');
22

3+
// calls callback with {title: string, url: string}
34
var FeelingLucky = module.exports = function(query, callback) {
4-
var self = this;
5-
6-
var google = HTTP.createClient(80, 'ajax.googleapis.com');
7-
var search_url = "/ajax/services/search/web?v=1.0&q=" + encodeURIComponent(query);
8-
var request = google.request('GET', search_url, {
9-
'host': 'ajax.googleapis.com',
10-
'Referer': 'http://www.v8bot.com',
11-
'User-Agent': 'NodeJS HTTP client',
12-
'Accept': '*/*'});
13-
request.addListener('response', function(response) {
14-
response.setEncoding('utf8');
15-
var body = "";
16-
response.addListener('data', function(chunk) { body += chunk; });
17-
response.addListener('end', function() {
18-
var searchResults = JSON.parse(body);
19-
var results = searchResults.responseData.results;
5+
google(query, function(error, results) {
6+
if (error) {
7+
console.error('FeelingLucky ', error);
8+
return callback(null);
9+
}
2010

21-
if (results && results[0]) {
22-
results[0].url = decodeURIComponent(results[0].url);
23-
callback.call(self,
24-
{
25-
title: results[0].titleNoFormatting.replace(/&#(\d+);/g,
26-
function(a, b){return String.fromCharCode(b);}),
27-
url: results[0].url
28-
});
29-
} else {
30-
callback.call(self, null);
31-
}
32-
});
11+
var result = results.links[0];
12+
if (!result) {
13+
return callback(null);
14+
}
15+
16+
callback({title: result.title, url: result.link})
3317
});
34-
request.end();
3518
};

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"repository": "https://github.com/oftn/oftn-bot",
3-
"dependencies": {
4-
"bhttp": "^1.2.1",
5-
"object.assign": "^4.0.3",
6-
"twitter": "~0.1.19"
7-
}
2+
"repository": "https://github.com/oftn/oftn-bot",
3+
"dependencies": {
4+
"bhttp": "^1.2.1",
5+
"google": "^2.0.0",
6+
"object.assign": "^4.0.3",
7+
"twitter": "~0.1.19"
8+
}
89
}

0 commit comments

Comments
 (0)