From: Matthias Hecker Date: Thu, 2 Apr 2020 07:19:40 +0000 (+0200) Subject: plugin(search): fix search and gcalc, closes #28, #29 X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=7b792bea7a644309623d67b5d49528ae13da3e7b;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git plugin(search): fix search and gcalc, closes #28, #29 --- diff --git a/data/rbot/plugins/search.rb b/data/rbot/plugins/search.rb index e6803e58..ec39c283 100644 --- a/data/rbot/plugins/search.rb +++ b/data/rbot/plugins/search.rb @@ -15,10 +15,10 @@ # 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/m/search?hl=en&q=" -GOOGLE_WAP_LINK = /"r">(?:]*>)?]*>(.*?)<\/a>/im -GOOGLE_CALC_RESULT = %r{]*>(.+?)]*>\s*]*>(.*?)\s*<\/div>/im +GOOGLE_CALC_RESULT = />Calculator<\/span>(?:<\/?[^>]+>\s*)+([^<]+)/ GOOGLE_COUNT_RESULT = %r{Results 1<\/b> - 10<\/b> of about (.*)<\/b> for} GOOGLE_DEF_RESULT = %r{onebox_result">\s*(.*?)\s*
\s*(.*?)]+>(.+?)<(br|/td)>} @@ -90,7 +90,6 @@ class SearchPlugin < Plugin m.reply "error duckduckgoing for #{what}" return end - debug feed xml = REXML::Document.new feed heading = xml.elements['//Heading/text()'].to_s @@ -229,60 +228,25 @@ class SearchPlugin < Plugin return end - single ||= (results.length==1) - pretty = [] + results = results.map {|result| + url = CGI::parse(Utils.decode_html_entities(result[0]))['q'].first + title = Utils.decode_html_entities(result[1].gsub(/<\/?[^>]+>/, '')) + [url, title] unless url.empty? or title.empty? + }.reject {|item| not item}[0..hits] - begin - urls = Array.new - - debug results - results.each do |res| - t = res[1].ircify_html(:img => "[%{src} %{alt} %{dimensions}]").strip - u = res[0] - if u.sub!(%r{^http://www.google.com/aclk\?},'') - u = CGI::parse(u)['adurl'].first - debug "skipping ad for #{u}" - next - elsif u.sub!(%r{^http://www.google.com/gwt/x\?},'') - u = CGI::parse(u)['u'].first - elsif u.sub!(%r{^/url\?},'') - u = CGI::parse(u)['q'].first - end - urls.push(u) - pretty.push("%{n}%{b}%{t}%{b}%{sep}%{u}" % { - :n => (single ? "" : "#{urls.length}. "), - :sep => (single ? " -- " : ": "), - :b => Bold, :t => t, :u => u - }) - break if urls.length == hits - end - rescue => e - m.reply "failed to understand what google found for #{what}" - error e - debug wml - debug results - return - end + result_string = results.map {|url, title| "#{Bold}#{title}#{NormalText}: #{url}"} if params[:lucky] - m.reply pretty.first + m.reply result_string.first + Utils.get_first_pars([results.map {|url, title| url}.first], first_pars, :message => m) return end - result_string = pretty.join(" | ") - - # If we return a single, full result, change the output to a more compact representation - if single - m.reply "Result for %s: %s -- %s" % [what, result_string, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate - return - end - - m.reply "Results for #{what}: #{result_string}", :split_at => /\s+\|\s+/ + m.reply "Results for #{what}: #{result_string.join(' | ')}", :split_at => /\s+\|\s+/ return unless first_pars > 0 - Utils.get_first_pars urls, first_pars, :message => m - + Utils.get_first_pars(results.map {|url, title| url}, first_pars, :message => m) end def google_define(m, what, params) @@ -342,7 +306,7 @@ class SearchPlugin < Plugin m.reply "couldn't calculate #{what}" return end - result = candidates[1] + result = candidates[1].remove_nonascii debug "replying with: #{result.inspect}" m.reply result.ircify_html @@ -504,11 +468,14 @@ end plugin = SearchPlugin.new -plugin.map "ddg *words", :action => 'duckduckgo', :threaded => true -plugin.map "search *words", :action => 'google', :threaded => true -plugin.map "google *words", :action => 'google', :threaded => true -plugin.map "lucky *words", :action => 'lucky', :threaded => true + plugin.map "ddg *words", :action => 'duckduckgo', :threaded => true + plugin.map "search *words", :action => 'google', :threaded => true + plugin.map "google *words", :action => 'google', :threaded => true + plugin.map "lucky *words", :action => 'lucky', :threaded => true + +# Broken: plugin.map "gcount *words", :action => 'gcount', :threaded => true + plugin.map "gcalc *words", :action => 'gcalc', :threaded => true plugin.map "gdef *words", :action => 'gdef', :threaded => true plugin.map "gtime *words", :action => 'gtime', :threaded => true diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb index 11ebfc61..08278261 100644 --- a/lib/rbot/core/utils/extends.rb +++ b/lib/rbot/core/utils/extends.rb @@ -380,6 +380,18 @@ class ::String return txt end + + # Removes non-ASCII symbols from string + def remove_nonascii(replace='') + encoding_options = { + :invalid => :replace, # Replace invalid byte sequences + :undef => :replace, # Replace anything not defined in ASCII + :replace => replace, + :universal_newline => true # Always break lines with \n + } + + self.encode(Encoding.find('ASCII'), encoding_options) + end end # Extensions to the Regexp class, with some common and/or complex regular