diff options
author | Raine Virta <rane@kapsi.fi> | 2009-02-04 16:07:37 +0200 |
---|---|---|
committer | Raine Virta <rane@kapsi.fi> | 2009-02-19 03:49:24 +0200 |
commit | 5bfce83f0619276cd0db55e02f9fe29fd2333ba3 (patch) | |
tree | 2013e0dbec0148721b69c440997ae1e7a55ec185 /data/rbot | |
parent | 15a64e8a1f63ba01e3e873579a8496d2aa1d735a (diff) |
googlefight plugin
Diffstat (limited to 'data/rbot')
-rwxr-xr-x | data/rbot/plugins/googlefight.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/data/rbot/plugins/googlefight.rb b/data/rbot/plugins/googlefight.rb new file mode 100755 index 00000000..3f7cf5a1 --- /dev/null +++ b/data/rbot/plugins/googlefight.rb @@ -0,0 +1,64 @@ +#-- vim:sw=2:et +#++ +# +# :title: Googlefight plugin for rbot +# +# Author:: Raine Virta <rane@kapsi.fi +# Copyright:: (C) 2009 Raine Virta +# License:: GPL v2 + +class GoogleFightPlugin < Plugin + def help(plugin, topic) + "googlefight <keyword 1> <keyword 2> [... <keyword n+1>] => 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| + "%{keyword} (%{count})" % { + :keyword => Bold+kw[0]+Bold, + :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 + + 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 <b>1</b> - <b>\d+</b> of(?: about)? <b>([\d,]+)</b>}) + res.to_s.tr(",", "").to_i + end +end + +plugin = GoogleFightPlugin.new +plugin.map "googlefight *keywords", :action => "fight", + :requirements => { :keywords => /^[\w\s"]+? (?:(?:(?:\||vs\.) )?[\w\s"]+?)+/ }
\ No newline at end of file |