X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Frss.rb;h=905daad81ffcfa49c36fc88ea484441c8a5bdbda;hb=ad4317c01095675546e5d907a917168d82a0580e;hp=a067cbba1aad5a258a2281637b622eb111d682b7;hpb=b14aff9aee87df83c2aab47c92b58cc7bf74432e;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index a067cbba..905daad8 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -16,10 +16,20 @@ require 'rss' -# Add support for Slashdot namespace in RDF. The code is just an adaptation of -# the DublinCore code. module ::RSS + # Make an 'unique' ID for a given item, based on appropriate bot options + # Currently only suppored is bot.config['rss.show_updated']: when true, the + # description is included in the uid hashing, otherwise it's not + # + def RSS.item_uid_for_bot(item, opts={}) + options = { :show_updated => true}.merge(opts) + desc = options[:show_updated] ? item.description : nil + [item.title, item.link, desc].hash + end + + # Add support for Slashdot namespace in RDF. The code is just an adaptation + # of the DublinCore code. unless defined?(SLASH_PREFIX) SLASH_PREFIX = 'slash' SLASH_URI = "http://purl.org/rss/1.0/modules/slash/" @@ -220,17 +230,21 @@ end class RSSFeedsPlugin < Plugin BotConfig.register BotConfigIntegerValue.new('rss.head_max', - :default => 30, :validate => Proc.new{|v| v > 0 && v < 200}, + :default => 100, :validate => Proc.new{|v| v > 0 && v < 200}, :desc => "How many characters to use of a RSS item header") BotConfig.register BotConfigIntegerValue.new('rss.text_max', - :default => 90, :validate => Proc.new{|v| v > 0 && v < 400}, + :default => 200, :validate => Proc.new{|v| v > 0 && v < 400}, :desc => "How many characters to use of a RSS item text") BotConfig.register BotConfigIntegerValue.new('rss.thread_sleep', :default => 300, :validate => Proc.new{|v| v > 30}, :desc => "How many seconds to sleep before checking RSS feeds again") + BotConfig.register BotConfigBooleanValue.new('rss.show_updated', + :default => true, + :desc => "Whether feed items for which the description was changed should be shown as new") + # We used to save the Mutex with the RssBlob, which was idiotic. And # since Mutexes dumped in one version might not be resotrable in another, # we need a few tricks to be able to restore data from other versions of Ruby @@ -246,6 +260,8 @@ class RSSFeedsPlugin < Plugin end end + attr_reader :feeds + def initialize super if @registry.has_key?(:feeds) @@ -273,6 +289,7 @@ class RSSFeedsPlugin < Plugin } @feeds = @registry[:feeds] + raise unless @feeds @registry.recovery = nil @@ -302,6 +319,7 @@ class RSSFeedsPlugin < Plugin def cleanup stop_watches + super end def save @@ -320,7 +338,7 @@ class RSSFeedsPlugin < Plugin debug "Stopping watch #{handle}" @bot.timer.remove(@watch[handle]) @watch.delete(handle) - rescue => e + rescue Exception => e report_problem("Failed to stop watch for #{handle}", e, nil) end end @@ -397,7 +415,25 @@ class RSSFeedsPlugin < Plugin m.reply "lemme fetch it..." title = items = nil - fetched = fetchRss(feed, m, false) + we_were_watching = false + + if @watch.key?(feed.handle) + # If a feed is being watched, we run the watcher thread + # so that all watchers can be informed of changes to + # the feed. Before we do that, though, we remove the + # show requester from the watchlist, if present, lest + # he gets the update twice. + if feed.watched_by?(m.replyto) + we_were_watching = true + feed.rm_watch(m.replyto) + end + @bot.timer.reschedule(@watch[feed.handle], 0) + if we_were_watching + feed.add_watch(m.replyto) + end + else + fetched = fetchRss(feed, m, false) + end return unless fetched or feed.xml if not fetched and feed.items m.reply "using old data" @@ -447,7 +483,7 @@ class RSSFeedsPlugin < Plugin reply = "no feeds found" reply << " matching #{wanted}" if wanted end - m.reply reply + m.reply reply, :max_lines => reply.length end def watched_rss(m, params) @@ -558,6 +594,7 @@ class RSSFeedsPlugin < Plugin def del_rss(m, params, pass=false) feed = unwatch_rss(m, params, true) + return unless feed if feed.watched? m.reply "someone else is watching #{feed.handle}, I won't remove it from my list" return @@ -641,16 +678,21 @@ class RSSFeedsPlugin < Plugin end status = Hash.new status[:failures] = 0 + status[:first_run] = true @watch[feed.handle] = @bot.timer.add(0, status) { debug "watcher for #{feed} started" failures = status[:failures] + first_run = status.delete(:first_run) begin debug "fetching #{feed}" oldxml = feed.xml ? feed.xml.dup : nil unless fetchRss(feed) failures += 1 else - if oldxml and oldxml == feed.xml + if first_run + debug "first run for #{feed}, getting items" + parseRss(feed) + elsif oldxml and oldxml == feed.xml debug "xml for #{feed} didn't change" failures -= 1 if failures > 0 else @@ -659,16 +701,45 @@ class RSSFeedsPlugin < Plugin parseRss(feed) failures -= 1 if failures > 0 else - otxt = feed.items.map { |item| item.to_s } + # This one is used for debugging + otxt = [] + + # These are used for checking new items vs old ones + uid_opts = { :show_updated => @bot.config['rss.show_updated'] } + oids = Set.new feed.items.map { |item| + uid = RSS.item_uid_for_bot(item, uid_opts) + otxt << item.to_s + debug [uid, item].inspect + debug [uid, otxt.last].inspect + uid + } + unless parseRss(feed) debug "no items in feed #{feed}" failures += 1 else debug "Checking if new items are available for #{feed}" failures -= 1 if failures > 0 + # debug "Old:" + # debug oldxml + # debug "New:" + # debug feed.xml + dispItems = feed.items.reject { |item| - otxt.include?(item.to_s) + uid = RSS.item_uid_for_bot(item, uid_opts) + txt = item.to_s + if oids.include?(uid) + debug "rejecting old #{uid} #{item.inspect}" + debug [uid, txt].inspect + true + else + debug "accepting new #{uid} #{item.inspect}" + debug [uid, txt].inspect + warning "same text! #{txt}" if otxt.include?(txt) + false + end } + if dispItems.length > 0 debug "Found #{dispItems.length} new items in #{feed}" # When displaying watched feeds, publish them from older to newer @@ -710,13 +781,13 @@ class RSSFeedsPlugin < Plugin 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") + date = item.pubDate.strftime("%Y/%m/%d %H:%M") 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") + date = item.date.strftime("%Y/%m/%d %H:%M") else date = item.date.to_s end @@ -727,15 +798,17 @@ class RSSFeedsPlugin < Plugin end end - title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title + title = "#{Bold}#{item.title.ircify_html :limit => @bot.config['rss.head_max']}#{Bold}" if item.title - desc = item.description.gsub(/\s+/,' ').strip.riphtml if item.description + desc = item.description.ircify_html(:limit => @bot.config['rss.text_max'], :a_href => :link_out) if item.description link = item.link.chomp if item.link debug item.inspect - category = item.dc_subject rescue item.category rescue nil + category = item.dc_subject rescue item.category.content rescue nil + category = nil if category and category.empty? author = item.dc_creator rescue item.author rescue nil + author = nil if author and author.empty? line1 = nil line2 = nil @@ -743,8 +816,9 @@ class RSSFeedsPlugin < Plugin at = ((item.title && item.link) ? ' @ ' : '') case feed.type when 'blog' + author << " " if author abt = category ? "about #{category} " : "" - line1 = "#{handle}#{date}#{author} blogged #{abt}at #{link}" + line1 = "#{handle}#{date}#{author}blogged #{abt}at #{link}" line2 = "#{handle}#{title} - #{desc}" when 'forum' line1 = "#{handle}#{date}#{title}#{at}#{link}" @@ -891,6 +965,9 @@ plugin.map 'rss replace :handle :url :type', plugin.map 'rss forcereplace :handle :url :type', :action => 'forcereplace_rss', :defaults => {:type => nil} +plugin.map 'rss watch :handle [in :chan]', + :action => 'watch_rss', + :defaults => {:url => nil, :type => nil} plugin.map 'rss watch :handle :url :type [in :chan]', :action => 'watch_rss', :defaults => {:url => nil, :type => nil}