Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed old google lib and replaced it with a YQL query using YUI und…

…er the hood
  • Loading branch information...
commit 7ef6766666accd2036f5244d998bae27719074b0 1 parent 4b22374
@davglass authored
Showing with 50 additions and 40 deletions.
  1. +0 −4 lib/google/README
  2. +0 −15 lib/google/google.js
  3. +50 −21 lib/yuibot.js
View
4 lib/google/README
@@ -1,4 +0,0 @@
-
-Snagged from protobot: http://github.com/gf3/protobot
-
-
View
15 lib/google/google.js
@@ -1,15 +0,0 @@
-/* ------------------------------ Includes && Options ------------------------------ */
-var exec = require('child_process').exec;
-
-/* ------------------------------ Google ------------------------------ */
-function Google() {
- this.search = function(query, hollaback) {
- exec("curl -e 'http://gf3.ca' 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + escape(query) + "'", function (err, stdout, stderr) {
- hollaback.call(this, JSON.parse(stdout)["responseData"]["results"]);
- });
- };
-}
-
-/* ------------------------------ Export ------------------------------ */
-module.exports = Google;
-
View
71 lib/yuibot.js
@@ -2,13 +2,12 @@
var sys = require('sys'),
jerk = require('jerk'),
- Google = require('./google/google'),
+ Y = require('yui3').useSync('yql');
mysql_client = require('mysql').Client,
mysql = new mysql_client(),
YUI3 = require('./yui3-docs').YUI3,
- // Google
- google = new Google(),
- config = require('./config').config;
+ config = require('./config').config,
+ stripHTML = /<\S[^><]*>/g;
mysql.user = config.mysql.username;
@@ -30,31 +29,61 @@ jerk(function(j) {
j.watch_for( /^(?:it )?doesn(?:')?t work(?:\s*@\s*([-\[\]|_\w]+))?/, function( message ) {
message.say( to( message, "doesn't work" ) + ": What do you mean it doesn't work? What happens when you try to run it? What's the output? What's the error message? Saying \"it doesn't work\" is pointless." )
});
-
- j.watch_for( /^g(?:oogle)? ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]|_\w]+))?$/, function( message ) {
+
+ j.watch_for( /^yql ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]\{\}`|_\w]+))?$/, function( message ) {
var user = to( message, 3 ),
- res = +message.match_data[2]-1 || 0;
+ res = +message.match_data[2]-1 || 0,
+ sql = message.match_data[1];
- google.search( message.match_data[1], function( results ) {
- if ( results.length ) {
- message.say( user + ": " + results[res].titleNoFormatting + " - " + results[res].unescapedUrl );
- } else {
- message.say( user + ": Sorry, no results for '" + message.match_data[1] + "'" );
+ if (message.match_data[3]) {
+ user = to(message, 4);
+ }
+
+ Y.YQL(sql, function(r) {
+ if (r.query && r.query.results) {
+ message.say(user + ": " + JSON.stringify(r.query.results.result[res]));
+ } else {
+ message.say( user + ": YQL Returned no results")
}
});
+
+
});
-
- j.watch_for( /^mdc ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]|_\w]+))?$/, function( message ) {
+
+ var webSearch = function(message, scope) {
var user = to( message, 3 ),
- res = +message.match_data[2]-1 || 0;
+ res = +message.match_data[2]-1 || 0,
+ search = message.match_data[1];
- google.search( message.match_data[1] + ' site:developer.mozilla.org', function( results ) {
- if ( results.length ) {
- message.say( user + ": " + results[res].titleNoFormatting + " - " + results[res].unescapedUrl );
- } else {
- message.say( user + ": Sorry, no results for '" + message.match_data[1] + "'" );
+ if (message.match_data[3]) {
+ user = to(message, 4);
}
- });
+ var sql = 'select title,url from search.web where query="' + escape(search) + '"' + ((scope) ? ' and sites="' + scope + '"' : '');
+ console.log('SQL: ', sql);
+
+ Y.YQL(sql, function(r) {
+ if (r.query && r.query.results) {
+ var results = r.query.results.result[res];
+ message.say(user + ": " + results.title.replace(stripHTML, '') + ' - ' + results.url);
+ } else {
+ message.say( user + ": YQL Returned no results")
+ }
+ });
+ };
+
+ j.watch_for( /^search ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]\{\}`|_\w]+))?$/, webSearch);
+
+ j.watch_for( /^g(?:oogle)? ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]\{\}`|_\w]+))?$/, function( message ) {
+ var user = to( message, 3 );
+ if (message.match_data[3]) {
+ user = to(message, 4);
+ }
+ message.say( user + ": We don't use Google search here, using Yahoo! Search instead");
+ webSearch(message);
+ });
+
+ j.watch_for( /^mdc ([^#@]+)(?:\s*#([1-9]))?(?:\s*@\s*([-\[\]\{\}`|_\w]+))?$/, function( message ) {
+ webSearch(message, 'developer.mozilla.org');
});
j.watch_for( /^api ([$\w]+(?:[\.#]\w+)*)(?:\s+@\s*([-\[\]|_\w]+))?/, function( message ) {
Please sign in to comment.
Something went wrong with that request. Please try again.