X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fgooglefight.rb;h=894d2233cdae37c17ccb9131dfc6c597778fee7a;hb=052217de30c59206d7025b582d4604557a747470;hp=3f7cf5a185fe2c823aa9739848caceb5bda0a988;hpb=5bfce83f0619276cd0db55e02f9fe29fd2333ba3;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/googlefight.rb b/data/rbot/plugins/googlefight.rb index 3f7cf5a1..894d2233 100755 --- a/data/rbot/plugins/googlefight.rb +++ b/data/rbot/plugins/googlefight.rb @@ -11,18 +11,18 @@ class GoogleFightPlugin < Plugin def help(plugin, topic) "googlefight [... ] => battles given keywords based on amount of google search results and announces the winner!" end - + def fight(m, params) keywords = parse_keywords(params) return if keywords.nil? - + keywords.map! do |k| [k, google_count(k)] end - + m.reply output(keywords) end - + def output(result) result = result.sort_by { |e| e[1] }.reverse str = result.map do |kw| @@ -31,30 +31,30 @@ class GoogleFightPlugin < Plugin :count => kw[1].to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,") } end.join(" vs. ") - + unless result[0][1].zero? str << _(" -- %{keyword} wins!") % { :keyword => Bold+result[0][0]+Bold } else str << _(" -- no winner here!") - end + end end - + def parse_keywords(params) str = params[:keywords].join(" ") - + # foo "foo bar" bar # no separators so assume they're all separate keywords if str.match(/(?:"[\w\s]+"|\w+)(?: (?:"[\w\s]+"|\w+))+/) str.scan(/"[^"]+"|\S+/).flatten end end - + def google_count(query) url = 'http://www.google.com/search?hl=en&safe=off&btnG=Search&q=' << CGI.escape(query) html = Net::HTTP.get(URI.parse((url))) - res = html.scan(%r{Results 1 - \d+ of(?: about)? ([\d,]+)}) + res = html.scan(%r{>About ([\d,]+) results}) res.to_s.tr(",", "").to_i end end