X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fslashdot.rb;h=71411458e7300565cd1b069d240b1e39ade44953;hb=29397fb64d302d9dac81521c24e934e588d61ee8;hp=30f58dc9ee31370a1d0ce5514a24adf8cb2f90cd;hpb=10880d600dd3ae7acf3144ef1ab0c9bd329492e2;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/slashdot.rb b/data/rbot/plugins/slashdot.rb index 30f58dc9..71411458 100644 --- a/data/rbot/plugins/slashdot.rb +++ b/data/rbot/plugins/slashdot.rb @@ -1,22 +1,60 @@ +#-- vim:sw=2:et +#++ +# +# :title: Slashdot plugin for rbot + require 'rexml/document' -require 'uri/common' class SlashdotPlugin < Plugin include REXML def help(plugin, topic="") "slashdot search [=4] => search slashdot for , slashdot [=4] => return up to slashdot headlines (use negative max to return that many headlines, but all on one line.)" end + + # This method defines a filter for /. pages. It's needed because the generic + # summarization grabs a comment, not the actual article. + # + # This filter relies on Hpricot being available, since REXML isn't too + # happy with the /. pages + def slashdot_filter(s) + return nil unless defined? Hpricot + loc = Utils.check_location(s, /slashdot\.org/) + return nil unless loc + h = Hpricot(s[:text]) + # If we have no title tag in a head tag, return as this is not + # a /. page (it's probably a Slashdot RSS + ht = h/"head/title" + return nil if ht.empty? + title = ht.first.to_html.ircify_html + arts = (h/"div.article") + return nil if arts.empty? + if arts.length > 1 + tits = [] + arts.each { |el| + artitle = (el/"div.generaltitle").first.to_html.ircify_html + tits << artitle + } + content = tits.join(" | ") + else + det = (arts.first/"div.details").first.to_html.ircify_html + body = (arts.first/"div.body").first.to_html.ircify_html + content = [det, body].join(' ') + end + return {:title => title, :content => content} + end + + def initialize + super + if defined? Hpricot + @bot.register_filter(:slashdot, :htmlinfo) { |s| slashdot_filter(s) } + end + end def search_slashdot(m, params) - max = params[:limit].to_i - search = params[:search].to_s + max = params[:limit].to_i + search = params[:search].to_s - begin - xml = @bot.httputil.get(URI.parse("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}")) - rescue URI::InvalidURIError, URI::BadURIError => e - m.reply "illegal search string #{search}" - return - end + xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{CGI.escape(search)}") unless xml m.reply "search for #{search} failed" return @@ -39,7 +77,7 @@ class SlashdotPlugin < Plugin doc.elements.each("*/item") {|e| desc = e.elements["title"].text desc.gsub!(/(.{150}).*/, '\1..') - reply = sprintf("%s | %s", e.elements["link"].text, desc) + reply = sprintf("%s | %s", e.elements["link"].text, desc.ircify_html) m.reply reply done += 1 break if done >= max @@ -53,7 +91,7 @@ class SlashdotPlugin < Plugin debug params.inspect max = params[:limit].to_i debug "max is #{max}" - xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml")) + xml = @bot.httputil.get('http://slashdot.org/slashdot.xml') unless xml m.reply "slashdot news parse failed" return