From 983ad7a177ddb05519ff09b68d7bbf03bf9830a5 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Fri, 14 Sep 2012 08:55:39 +0200 Subject: [PATCH] search: improve ddg output with missing sources Sometimes duckduckgo fails to report the source/url of a definition or abstract. Cope with this by removing the leading double dash when no source information is provided --- data/rbot/plugins/search.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/data/rbot/plugins/search.rb b/data/rbot/plugins/search.rb index 1102d5e5..a4c4a89c 100644 --- a/data/rbot/plugins/search.rb +++ b/data/rbot/plugins/search.rb @@ -98,15 +98,28 @@ class SearchPlugin < Plugin answer = xml.elements['//Answer/text()'].to_s # abstract is returned for definitions etc abstract = xml.elements['//AbstractText/text()'].to_s + abfrom = "" unless abstract.empty? - absrc = xml.elements['//AbstractSource/text()'] - aburl = xml.elements['//AbstractURL/text()'] + absrc = xml.elements['//AbstractSource/text()'].to_s + aburl = xml.elements['//AbstractURL/text()'].to_s + unless absrc.empty? and aburl.empty? + abfrom = " --" + abfrom << " " << absrc unless absrc.empty? + abfrom << " " << aburl unless aburl.empty? + end end + # but also definition (yes, you can have both, see e.g. printf) definition = xml.elements['//Definition/text()'].to_s + deffrom = "" unless definition.empty? defsrc = xml.elements['//Definition/@source/text()'].to_s defurl = xml.elements['//Definition/@url/text()'].to_s + unless defsrc.empty? and defurl.empty? + deffrom = " --" + deffrom << " " << defsrc unless defsrc.empty? + deffrom << " " << defurl unless defurl.empty? + end end if heading.empty? and answer.empty? and abstract.empty? and definition.empty? @@ -122,15 +135,15 @@ class SearchPlugin < Plugin # otherwise, return the abstract, followed by as many hits as found unless heading.empty? or abstract.empty? - m.reply "%{bold}%{heading}:%{bold} %{abstract} -- %{absrc} %{aburl}" % { + m.reply "%{bold}%{heading}:%{bold} %{abstract}%{abfrom}" % { :bold => Bold, :heading => heading, - :abstract => abstract, :absrc => absrc, :aburl => aburl + :abstract => abstract, :abfrom => abfrom } end unless heading.empty? or definition.empty? - m.reply "%{bold}%{heading}:%{bold} %{abstract} -- %{absrc} %{aburl}" % { + m.reply "%{bold}%{heading}:%{bold} %{abstract}%{abfrom}" % { :bold => Bold, :heading => heading, - :abstract => definition, :absrc => defsrc, :aburl => defurl + :abstract => definition, :abfrom => deffrom } end # return zeroclick search results -- 2.32.0.93.g670b81a890