]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/digg.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / digg.rb
1 # Hacked up digg headlines plugin...
2
3 require 'time'
4 require 'rexml/document'
5 require 'uri/common'
6
7 class DiggPlugin < Plugin
8   include REXML
9   def help(plugin, topic="")
10     "digg [<max>=5] => show digg headlines, [<max>=5] => return up to <max> headlines (use a negative number to show all the headlines on one line)"
11   end
12   
13   def digg(m, params)
14     max = params[:limit].to_i
15     debug "max is #{max}"
16     xml = @bot.httputil.get_cached(URI.parse("http://digg.com/rss/index.xml"))
17     unless xml
18       m.reply "digg news parse failed"
19       return
20     end
21     doc = Document.new xml
22     unless doc
23       m.reply "digg news parse failed (invalid xml)"
24       return
25     end
26     done = 0
27     oneline = false
28     if max < 0
29       max = (0 - max)
30       oneline = true
31     end
32     max = 8 if max > 8
33     matches = Array.new
34     doc.elements.each("rss/channel/item") {|e|
35       matches << [ e.elements["title"].text, 
36                    Time.parse(e.elements["pubDate"].text).strftime('%a @ %I:%M%p') ]
37       done += 1
38       break if done >= max
39     } 
40     if oneline
41       m.reply matches.collect{|mat| mat[0]}.join(" | ")
42     else
43       matches.each {|mat|
44         m.reply sprintf("%42s | %13s", mat[0][0,42], mat[1])
45       }
46     end
47   end
48 end
49 plugin = DiggPlugin.new
50 plugin.map 'digg :limit', :defaults => {:limit => 5},
51                           :requirements => {:limit => /^-?\d+$/}