X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Frss.rb;h=f8d8c99795647db4672edbd791ee2675c012eb15;hb=24bb60775741d3731400f1e430ef6bf3a2e1b933;hp=2689b3c3a354a6f85e37c4dc59fba6300af284eb;hpb=0a8d35d747b1efab52608ea0705731db06012599;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index 2689b3c3..f8d8c997 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -24,8 +24,13 @@ module ::RSS # 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 + desc = nil + if options[:show_updated] + desc = item.content.content rescue item.description rescue nil + end + [(item.title.content rescue item.title rescue nil), + (item.link.href rescue item.link), + desc].hash end # Add support for Slashdot namespace in RDF. The code is just an adaptation @@ -157,17 +162,10 @@ end class ::RssBlob - attr_accessor :url - attr_accessor :handle - attr_accessor :type - attr :watchers - attr_accessor :refresh_rate - attr_accessor :xml - attr_accessor :title - attr_accessor :items - attr_accessor :mutex - - def initialize(url,handle=nil,type=nil,watchers=[], xml=nil) + attr_accessor :url, :handle, :type, :refresh_rate, :xml, :title, :items, + :mutex, :watchers, :last_fetched + + def initialize(url,handle=nil,type=nil,watchers=[], xml=nil, lf = nil) @url = url if handle @handle = handle @@ -181,6 +179,7 @@ class ::RssBlob @title = nil @items = nil @mutex = Mutex.new + @last_fetched = lf sanitize_watchers(watchers) end @@ -190,7 +189,8 @@ class ::RssBlob @handle, @type ? @type.dup : nil, @watchers.dup, - @xml ? @xml.dup : nil) + @xml ? @xml.dup : nil, + @last_fetched) end end @@ -676,13 +676,23 @@ class RSSFeedsPlugin < Plugin end def rewatch_rss(m=nil, params=nil) - stop_watches + if params and handle = params[:handle] + feed = @feeds.fetch(handle.downcase, nil) + if feed + @bot.timer.reschedule(@watch[feed.handle], 0) + m.okay if m + else + m.reply _("no such feed %{handle}") % { :handle => handle } if m + end + else + stop_watches - # Read watches from list. - watchlist.each{ |handle, feed| - watchRss(feed, m) - } - m.okay if m + # Read watches from list. + watchlist.each{ |handle, feed| + watchRss(feed, m) + } + m.okay if m + end end private @@ -693,13 +703,18 @@ class RSSFeedsPlugin < Plugin end status = Hash.new status[:failures] = 0 - status[:first_run] = true - @watch[feed.handle] = @bot.timer.add(0) { - debug "watcher for #{feed} started" + tmout = 0 + if feed.last_fetched + tmout = feed.last_fetched + calculate_timeout(feed) - Time.now + tmout = 0 if tmout < 0 + end + debug "scheduling a watcher for #{feed} in #{tmout} seconds" + @watch[feed.handle] = @bot.timer.add(tmout) { + debug "watcher for #{feed} wakes up" failures = status[:failures] - first_run = status.delete(:first_run) begin debug "fetching #{feed}" + first_run = !feed.last_fetched oldxml = feed.xml ? feed.xml.dup : nil unless fetchRss(feed) failures += 1 @@ -776,18 +791,31 @@ class RSSFeedsPlugin < Plugin status[:failures] = failures - timer = nil + seconds = calculate_timeout(feed, failures) + debug "watcher for #{feed} going to sleep #{seconds} seconds.." + begin + @bot.timer.reschedule(@watch[feed.handle], seconds) + rescue + warning "watcher for #{feed} failed to reschedule: #{$!.inspect}" + end + } + debug "watcher for #{feed} added" + end + + def calculate_timeout(feed, failures = 0) seconds = @bot.config['rss.thread_sleep'] feed.mutex.synchronize do - timer = @watch[feed.handle] seconds = feed.refresh_rate if feed.refresh_rate end seconds *= failures + 1 seconds += seconds * (rand(100)-50)/100 - debug "watcher for #{feed} going to sleep #{seconds} seconds.." - @bot.timer.reschedule(timer, seconds) rescue warning "watcher for #{feed} failed to reschedule: #{$!.inspect}" - } - debug "watcher for #{feed} added" + return seconds + end + + def select_nonempty(*ar) + debug ar + ret = ar.map { |i| (i && i.empty?) ? nil : i }.compact.first + (ret && ret.empty?) ? nil : ret end def printFormattedRss(feed, item, opts=nil) @@ -835,7 +863,14 @@ class RSSFeedsPlugin < Plugin # limit to 160 characters, and most of them are under 140 characters tit_opt[:limit] = @bot.config['rss.head_max'] unless feed.type == 'twitter' - title = "#{Bold}#{item.title.to_s.ircify_html(tit_opt)}#{Bold}" if item.title + if item.title + base_title = item.title.to_s.dup + # git changesets are SHA1 hashes (40 hex digits), way too long, get rid of them, as they are + # visible in the URL anyway + # TODO make this optional? + base_title.sub!(/^Changeset \[([\da-f]{40})\]:/) { |c| "(git commit)"} if feed.type = 'trac' + title = "#{Bold}#{base_title.ircify_html(tit_opt)}#{Bold}" + end desc_opt = { :limit => @bot.config['rss.text_max'], @@ -857,10 +892,8 @@ class RSSFeedsPlugin < Plugin link = item.link.href rescue item.link.chomp rescue nil - category = item.category.content rescue item.dc_subject rescue nil - category = nil if category and category.empty? - author = item.author.name.content rescue item.dc_creator rescue item.author rescue nil - author = nil if author and author.empty? + category = select_nonempty((item.category.content rescue nil), (idem.dc_subject rescue nil)) + author = select_nonempty((item.author.name.content rescue nil), (item.dc_creator rescue nil), (item.author rescue nil)) line1 = nil line2 = nil @@ -869,10 +902,13 @@ class RSSFeedsPlugin < Plugin case feed.type when 'blog' - author << " " if author + author += " " if author abt = category ? "about #{category} " : "" line1 = "#{handle}#{date}#{author}blogged #{abt}at #{link}" line2 = "#{handle}#{title} - #{desc}" + when 'git' + author += " " if author + line1 = "#{handle}#{date}#{author}commited #{title} @ #{link}" when 'forum' line1 = "#{handle}#{date}#{title}#{at}#{link}" when 'wiki' @@ -881,7 +917,7 @@ class RSSFeedsPlugin < Plugin line1 = "#{handle}#{date}Message #{title} sent by #{author}. #{desc}" when 'trac' line1 = "#{handle}#{date}#{title} @ #{link}" - unless item.title =~ /^Changeset \[(\d+)\]/ + unless item.title =~ /^(?:Changeset \[(?:[\da-f]+)\]|\(git commit\))/ line2 = "#{handle}#{date}#{desc}" end when '/.' @@ -891,6 +927,7 @@ class RSSFeedsPlugin < Plugin line1 = "#{handle}#{date}#{dept}#{title}#{at}#{link} (posted by #{author}#{sec})" else line1 = "#{handle}#{date}#{title}#{at}#{link}" + line1 << " (by #{author})" if author end places.each { |loc| @bot.say loc, line1, :overlong => :truncate @@ -900,6 +937,7 @@ class RSSFeedsPlugin < Plugin end def fetchRss(feed, m=nil, cache=true) + feed.last_fetched = Time.now begin # Use 60 sec timeout, cause the default is too low xml = @bot.httputil.get(feed.url, @@ -988,6 +1026,9 @@ end plugin = RSSFeedsPlugin.new +plugin.default_auth( 'edit', false ) +plugin.default_auth( 'edit:add', true) + plugin.map 'rss show :handle :limit', :action => 'show_rss', :requirements => {:limit => /^\d+(?:\.\.\d+)?$/}, @@ -1003,23 +1044,31 @@ plugin.map 'rss who watches :handle', :defaults => {:handle => nil} plugin.map 'rss add :handle :url :type', :action => 'add_rss', + :auth_path => 'edit', :defaults => {:type => nil} plugin.map 'rss change :what of :handle to :new', :action => 'change_rss', + :auth_path => 'edit', :requirements => { :what => /handle|url|format|type|refresh/ } plugin.map 'rss change :what for :handle to :new', :action => 'change_rss', + :auth_path => 'edit', :requirements => { :what => /handle|url|format|type|refesh/ } plugin.map 'rss del :handle', + :auth_path => 'edit:rm!', :action => 'del_rss' plugin.map 'rss delete :handle', + :auth_path => 'edit:rm!', :action => 'del_rss' plugin.map 'rss rm :handle', + :auth_path => 'edit:rm!', :action => 'del_rss' plugin.map 'rss replace :handle :url :type', + :auth_path => 'edit', :action => 'replace_rss', :defaults => {:type => nil} plugin.map 'rss forcereplace :handle :url :type', + :auth_path => 'edit', :action => 'forcereplace_rss', :defaults => {:type => nil} plugin.map 'rss watch :handle [in :chan]', @@ -1032,6 +1081,5 @@ plugin.map 'rss unwatch :handle [in :chan]', :action => 'unwatch_rss' plugin.map 'rss rmwatch :handle [in :chan]', :action => 'unwatch_rss' -plugin.map 'rss rewatch', +plugin.map 'rss rewatch [:handle]', :action => 'rewatch_rss' -