1 require 'rexml/document'
4 class SlashdotPlugin < Plugin
6 def help(plugin, topic="")
7 "slashdot search <string> [<max>=4] => search slashdot for <string>, slashdot [<max>=4] => return up to <max> slashdot headlines (use negative max to return that many headlines, but all on one line.)"
10 def search_slashdot(m, params)
11 max = params[:limit].to_i
12 search = params[:search].to_s
15 xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}")
16 rescue URI::InvalidURIError, URI::BadURIError => e
17 m.reply "illegal search string #{search}"
21 m.reply "search for #{search} failed"
26 doc = Document.new xml
27 rescue REXML::ParseException => e
29 m.reply "couldn't parse output XML: #{e.class}"
33 m.reply "search for #{search} failed"
39 doc.elements.each("*/item") {|e|
40 desc = e.elements["title"].text
41 desc.gsub!(/(.{150}).*/, '\1..')
42 reply = sprintf("%s | %s", e.elements["link"].text, desc)
48 m.reply "search for #{search} failed"
52 def slashdot(m, params)
54 max = params[:limit].to_i
56 xml = @bot.httputil.get('http://slashdot.org/slashdot.xml')
58 m.reply "slashdot news parse failed"
61 doc = Document.new xml
63 m.reply "slashdot news parse failed (invalid xml)"
74 doc.elements.each("*/story") {|e|
75 matches << [ e.elements["title"].text,
76 e.elements["author"].text,
77 e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
82 m.reply matches.collect{|mat| mat[0]}.join(" | ")
85 m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
90 plugin = SlashdotPlugin.new
91 plugin.map 'slashdot search :limit *search', :action => 'search_slashdot',
92 :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/}
93 plugin.map 'slashdot :limit', :defaults => {:limit => 4},
94 :requirements => {:limit => /^-?\d+$/}