4 # :title: Slashdot plugin for rbot
6 require 'rexml/document'
8 class SlashdotPlugin < Plugin
10 def help(plugin, topic="")
11 "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.)"
14 # This method defines a filter for /. pages. It's needed because the generic
15 # summarization grabs a comment, not the actual article.
17 # This filter relies on Hpricot being available, since REXML isn't too
18 # happy with the /. pages
19 def slashdot_filter(s)
20 return nil unless defined? Hpricot
21 loc = Utils.check_location(s, /slashdot\.org/)
24 # If we have no title tag in a head tag, return as this is not
25 # a /. page (it's probably a Slashdot RSS
27 return nil if ht.empty?
28 title = ht.first.to_html.ircify_html
29 arts = (h/"div.article")
30 return nil if arts.empty?
34 artitle = (el/"div.generaltitle").first.to_html.ircify_html
37 content = tits.join(" | ")
39 det = (arts.first/"div.details").first.to_html.ircify_html
40 body = (arts.first/"div.body").first.to_html.ircify_html
41 content = [det, body].join(' ')
43 return {:title => title, :content => content}
49 @bot.register_filter(:slashdot, :htmlinfo) { |s| slashdot_filter(s) }
53 def search_slashdot(m, params)
54 max = params[:limit].to_i
55 search = params[:search].to_s
57 xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{CGI.escape(search)}")
59 m.reply "search for #{search} failed"
64 doc = Document.new xml
65 rescue REXML::ParseException => e
67 m.reply "couldn't parse output XML: #{e.class}"
71 m.reply "search for #{search} failed"
77 doc.elements.each("*/item") {|e|
78 desc = e.elements["title"].text
79 desc.gsub!(/(.{150}).*/, '\1..')
80 reply = sprintf("%s | %s", e.elements["link"].text, desc.ircify_html)
86 m.reply "search for #{search} failed"
90 def slashdot(m, params)
92 max = params[:limit].to_i
94 xml = @bot.httputil.get('http://slashdot.org/slashdot.xml')
96 m.reply "slashdot news parse failed"
99 doc = Document.new xml
101 m.reply "slashdot news parse failed (invalid xml)"
112 doc.elements.each("*/story") {|e|
113 matches << [ e.elements["title"].text,
114 e.elements["author"].text,
115 e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
120 m.reply matches.collect{|mat| mat[0]}.join(" | ")
123 m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
128 plugin = SlashdotPlugin.new
129 plugin.map 'slashdot search :limit *search', :action => 'search_slashdot',
130 :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/}
131 plugin.map 'slashdot :limit', :defaults => {:limit => 4},
132 :requirements => {:limit => /^-?\d+$/}