forked from oftn-oswg/oftn-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use google npm package for FeelingLucky, fixes oftn-oswg#55
- Loading branch information
Showing
2 changed files
with
20 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |