From bec794ba32bc6fe8fbb688da7a1f140b33485dba Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sat, 7 Jul 2007 21:56:38 +0000 Subject: search plugin: add google define support --- data/rbot/plugins/search.rb | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'data/rbot/plugins') diff --git a/data/rbot/plugins/search.rb b/data/rbot/plugins/search.rb index c4bf0db1..8453b4ba 100644 --- a/data/rbot/plugins/search.rb +++ b/data/rbot/plugins/search.rb @@ -15,8 +15,11 @@ # TODO:: support localized uncyclopedias -- not easy because they have different names # for most languages +GOOGLE_SEARCH = "http://www.google.com/search?oe=UTF-8&q=" +GOOGLE_WAP_SEARCH = "http://www.google.com/wml/search?hl=en&q=" GOOGLE_WAP_LINK = /(.*?)<\/a>/im GOOGLE_CALC_RESULT = %r{

 (?:

)?(.+)(?:

)?
} +GOOGLE_DEF_RESULT = %r{

(Web definitions for .*?)
(.*?)
(.*?)\s-\s+ => search google for " when "gcalc" "gcalc => use the google calculator to find the answer to " + when "gdef" + "gdef => use the google define mechanism to find a definition of " when "wp" "wp [] => search for on Wikipedia. You can select a national to only search the national Wikipedia" when "unpedia" @@ -60,7 +65,7 @@ class SearchPlugin < Plugin # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches filter = params[:filter] || "" - url = "http://www.google.com/wml/search?q=#{site}#{searchfor}" + url = GOOGLE_WAP_SEARCH + site + searchfor hits = params[:hits] || @bot.config['google.hits'] @@ -108,7 +113,7 @@ class SearchPlugin < Plugin searchfor = CGI.escape(what) debug "Getting gcalc thing: #{searchfor.inspect}" - url = "http://www.google.com/search?oe=UTF-8&q=#{searchfor}" + url = GOOGLE_SEARCH + searchfor begin html = @bot.httputil.get(url) @@ -132,6 +137,35 @@ class SearchPlugin < Plugin m.reply "#{result}" end + def gdef(m, params) + what = params[:words].to_s + searchfor = CGI.escape("define " + what) + + debug "Getting gdef thing: #{searchfor.inspect}" + url = GOOGLE_WAP_SEARCH + searchfor + + begin + html = @bot.httputil.get(url) + rescue => e + m.reply "error googledefining #{what}" + return + end + + debug html + results = html.scan(GOOGLE_DEF_RESULT) + debug "results: #{results.inspect}" + + if results.length != 1 + m.reply "couldn't find a definition for #{what} on Google" + return + end + + head = results[0][0].ircify_html + text = results[0][1].ircify_html + link = results[0][2] + m.reply "#{head} -- #{link}\n#{text}" + end + def wikipedia(m, params) lang = params[:lang] site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org" @@ -159,6 +193,7 @@ plugin = SearchPlugin.new plugin.map "search *words", :action => 'google' plugin.map "google *words", :action => 'google' plugin.map "gcalc *words", :action => 'gcalc' +plugin.map "gdef *words", :action => 'gdef' plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ } plugin.map "wp *words", :action => 'wikipedia' plugin.map "unpedia *words", :action => 'unpedia' -- cgit v1.2.3