X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Frss.rb;h=882a15fbd7c3a5c80c426814044a23d5cfa0f30d;hb=1ea3decbbacd73bf93f52f3ce392914cc924df4f;hp=fcea9197b67a77d04091ba797dfb3c94038ba851;hpb=71e7de53680d14072a93176739c442b3953c70c6;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index fcea9197..882a15fb 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -1,7 +1,12 @@ +#-- vim:sw=2:et +#++ +# # RSS feed plugin for RubyBot # (c) 2004 Stanislav Karchebny # (c) 2005 Ian Monroe # (c) 2005 Mark Kretschmann +# (c) 2006 Giuseppe Bilotta +# # Licensed under MIT License. require 'rss/parser' @@ -54,19 +59,28 @@ class ::RssBlob end def watched_by?(who) - @watchers.include?(who) + # We need to check bot 'who' itself and the String form, because rss + # watches added before the new Irc framework represented watchers as + # Strings whereas they are now Channels. + # + @watchers.include?(who) || @watchers.include?(who.to_s) end def add_watch(who) if watched_by?(who) return nil end - @watchers << who unless watched_by?(who) + # TODO FIXME? should we just store watchers as Strings instead? + # This should then be @watchers << who.downcase + @watchers << who return who end def rm_watch(who) + # See comment to watched_by? + # @watchers.delete(who) + @watchers.delete(who.to_s) end def to_a @@ -94,7 +108,7 @@ class RSSFeedsPlugin < Plugin BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep', :default => 300, :validate => Proc.new{|v| v > 30}, - :desc => "How many characters to use of a RSS item text") + :desc => "How many seconds to sleep before checking RSS feeds again") @@watchThreads = Hash.new @@mutex = Mutex.new @@ -110,6 +124,10 @@ class RSSFeedsPlugin < Plugin rewatch_rss end + def name + "rss" + end + def watchlist @feeds.select { |h, f| f.watched? } end @@ -210,9 +228,17 @@ class RSSFeedsPlugin < Plugin end end + def itemDate(item,ex=nil) + return item.pubDate if item.respond_to?(:pubDate) + return item.date if item.respond_to?(:date) + return ex + end + def freshness_sort(items) notime = Time.at(0) - items.sort { |a, b| (b.pubDate || notime) <=> (a.pubDate || notime) } + items.sort { |a, b| + itemDate(b, notime) || 0 <=> itemDate(a, notime) || 0 + } end def list_rss(m, params) @@ -355,7 +381,7 @@ class RSSFeedsPlugin < Plugin return feed end - def rewatch_rss(m=nil) + def rewatch_rss(m=nil, params=nil) kill_threads # Read watches from list. @@ -398,7 +424,8 @@ class RSSFeedsPlugin < Plugin } if dispItems.length > 0 debug "Found #{dispItems.length} new items in #{feed}" - dispItems.each { |item| + # When displaying watched feeds, publish them from older to newer + dispItems.reverse.each { |item| @@mutex.synchronize { printFormattedRss(feed, item) } @@ -430,7 +457,24 @@ class RSSFeedsPlugin < Plugin if opts places = opts[:places] if opts.key?(:places) handle = opts[:handle].to_s if opts.key?(:handle) - date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S") + " :: " if (opts.key?(:date) && opts[:date] && item.pubDate) + if opts.key?(:date) && opts[:date] + if item.respond_to?(:pubDate) + if item.pubDate.class <= Time + date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S") + else + date = item.pubDate.to_s + end + elsif item.respond_to?(:date) + if item.date.class <= Time + date = item.date.strftime("%Y/%m/%d %H.%M.%S") + else + date = item.date.to_s + end + else + date = "(no date)" + end + date += " :: " + end end title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title desc = item.description.gsub(/\s+/,' ').strip.riphtml.shorten(@bot.config['rss.text_max']) if item.description @@ -460,7 +504,9 @@ class RSSFeedsPlugin < Plugin def fetchRss(feed, m=nil) begin # Use 60 sec timeout, cause the default is too low - xml = @bot.httputil.get_cached(feed.url,60,60) + # Do not use get_cached for RSS until we have proper cache handling + # xml = @bot.httputil.get_cached(feed.url,60,60) + xml = @bot.httputil.get(feed.url,60,60) rescue URI::InvalidURIError, URI::BadURIError => e report_problem("invalid rss feed #{feed.url}", e, m) return @@ -553,6 +599,6 @@ plugin.map 'rss unwatch :handle', :action => 'unwatch_rss' plugin.map 'rss rmwatch :handle', :action => 'unwatch_rss' -plugin.map 'rss rewatch :handle', +plugin.map 'rss rewatch', :action => 'rewatch_rss'