From ab9f1a23b6808b05df2a51c17dd3f83bbc8a8863 Mon Sep 17 00:00:00 2001
From: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Date: Thu, 13 Sep 2012 09:27:19 +0200
Subject: search: be more rbot-ish in ddg

---
 data/rbot/plugins/search.rb | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

(limited to 'data/rbot/plugins')

diff --git a/data/rbot/plugins/search.rb b/data/rbot/plugins/search.rb
index 515cd654..3d9e5d3d 100644
--- a/data/rbot/plugins/search.rb
+++ b/data/rbot/plugins/search.rb
@@ -23,6 +23,8 @@ GOOGLE_COUNT_RESULT = %r{<font size=-1>Results <b>1<\/b> - <b>10<\/b> of about <
 GOOGLE_DEF_RESULT = %r{onebox_result">\s*(.*?)\s*<br/>\s*(.*?)<table}
 GOOGLE_TIME_RESULT = %r{alt="Clock"></td><td valign=[^>]+>(.+?)<(br|/td)>}
 
+DDG_API_SEARCH = "http://api.duckduckgo.com/?format=xml&no_html=1&no_redirect=0&q="
+
 class SearchPlugin < Plugin
   Config.register Config::IntegerValue.new('duckduckgo.hits',
     :default => 3, :validate => Proc.new{|v| v > 0},
@@ -63,20 +65,18 @@ class SearchPlugin < Plugin
   end
 
   def duckduckgo(m, params)
-    terms = params[:words].to_s
-    # DuckDuckGo is picky about white spaces
-    # in the url, so we can't use CGI.escape.
-    terms.gsub!(/\%/, '%25') if terms.include? "%"
-    terms.gsub!(/\+|\s\+\s|\s\+|\+\s/, ' %2B ') if terms.include? "+"
-    terms.gsub!(/\-|\s\-\s|\s\-|\-\s/, ' %2D ') if terms.include? "-"
-    terms.gsub!(/\!/, '%21') if terms.include? "!"
-    terms.gsub!(/\%/, ' %') if terms.include? "%"
-    feed = Net::HTTP.get 'api.duckduckgo.com',
-           "/?q=#{terms}&format=xml&skip_disambig=1&no_html=1&no_redirect=0"
-    if feed.nil? or feed.empty?
-      m.reply "error connecting"
+    what = params[:words].to_s
+    terms = CGI.escape what
+    url = DDG_API_SEARCH + terms
+    begin
+      feed = @bot.httputil.get(url)
+      raise unless feed
+    rescue => e
+      m.reply "error duckduckgoing for #{what}"
       return
     end
+    debug feed
+
     xml = REXML::Document.new feed
     heading = xml.elements['//Heading/text()'].to_s
     # answer is returned for calculations
-- 
cgit v1.2.3