Skip to content

Commit

Permalink
use google npm package for FeelingLucky, fixes oftn-oswg#55
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand committed May 9, 2016
1 parent ef20b3d commit b30dec6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
43 changes: 13 additions & 30 deletions lib/feelinglucky/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
var HTTP = require("http");
var google = require('google');

// calls callback with {title: string, url: string}
var FeelingLucky = module.exports = function(query, callback) {
var self = this;

var google = HTTP.createClient(80, 'ajax.googleapis.com');
var search_url = "/ajax/services/search/web?v=1.0&q=" + encodeURIComponent(query);
var request = google.request('GET', search_url, {
'host': 'ajax.googleapis.com',
'Referer': 'http://www.v8bot.com',
'User-Agent': 'NodeJS HTTP client',
'Accept': '*/*'});
request.addListener('response', function(response) {
response.setEncoding('utf8');
var body = "";
response.addListener('data', function(chunk) { body += chunk; });
response.addListener('end', function() {
var searchResults = JSON.parse(body);
var results = searchResults.responseData.results;
google(query, function(error, results) {
if (error) {
console.error('FeelingLucky ', error);
return callback(null);
}

if (results && results[0]) {
results[0].url = decodeURIComponent(results[0].url);
callback.call(self,
{
title: results[0].titleNoFormatting.replace(/&#(\d+);/g,
function(a, b){return String.fromCharCode(b);}),
url: results[0].url
});
} else {
callback.call(self, null);
}
});
var result = results.links[0];
if (!result) {
return callback(null);
}

callback({title: result.title, url: result.link})
});
request.end();
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"repository": "https://github.com/oftn/oftn-bot",
"dependencies": {
"bhttp": "^1.2.1",
"object.assign": "^4.0.3",
"twitter": "~0.1.19"
}
"repository": "https://github.com/oftn/oftn-bot",
"dependencies": {
"bhttp": "^1.2.1",
"google": "^2.0.0",
"object.assign": "^4.0.3",
"twitter": "~0.1.19"
}
}

0 comments on commit b30dec6

Please sign in to comment.