X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fslashdot.rb;h=c9e35b9e9592badde10fe4689aa9765e17c55add;hb=3f8710a19b6989cd1c67629a92fd992968579456;hp=b09ac7a789706a477a88bfa19b3fce935295aed9;hpb=21949774b91eaec6ecde4eaa8ad121e2c0a36b87;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/slashdot.rb b/data/rbot/plugins/slashdot.rb index b09ac7a7..c9e35b9e 100644 --- a/data/rbot/plugins/slashdot.rb +++ b/data/rbot/plugins/slashdot.rb @@ -1,42 +1,25 @@ 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 - def privmsg(m) - if m.params && m.params =~ /^search\s+(.*)\s+(\d+)$/ - search = $1 - limit = $2.to_i - search_slashdot m, search, limit - elsif m.params && m.params =~ /^search\s+(.*)$/ - search = $1 - search_slashdot m, search - elsif m.params && m.params =~ /^([-\d]+)$/ - limit = $1.to_i - slashdot m, limit - else - slashdot m - end - end - def search_slashdot(m, search, max=4) - 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 + def search_slashdot(m, params) + max = params[:limit].to_i + search = params[:search].to_s + + 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 end + debug xml.inspect begin doc = Document.new xml rescue REXML::ParseException => e - puts e + warning e.inspect m.reply "couldn't parse output XML: #{e.class}" return end @@ -44,20 +27,27 @@ class SlashdotPlugin < Plugin m.reply "search for #{search} failed" return end + debug doc.inspect max = 8 if max > 8 done = 0 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 } + unless done > 0 + m.reply "search for #{search} failed" + end end - def slashdot(m, max=4) - xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml")) + def slashdot(m, params) + debug params.inspect + max = params[:limit].to_i + debug "max is #{max}" + xml = @bot.httputil.get('http://slashdot.org/slashdot.xml') unless xml m.reply "slashdot news parse failed" return @@ -92,4 +82,7 @@ class SlashdotPlugin < Plugin end end plugin = SlashdotPlugin.new -plugin.register("slashdot") +plugin.map 'slashdot search :limit *search', :action => 'search_slashdot', + :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/} +plugin.map 'slashdot :limit', :defaults => {:limit => 4}, + :requirements => {:limit => /^-?\d+$/}