]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/rss.rb
Some fixes for the rss plugin
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rss.rb
index 1bc9b16ecf2e2e007c9f097d8157563edd9a9338..b49b6a1d1ec3adbea42ef4670f0148a334f4d63e 100644 (file)
@@ -136,7 +136,7 @@ class RSSFeedsPlugin < Plugin
   def help(plugin,topic="")\r
     case topic\r
     when "show"\r
-      "rss show #{Bold}handle#{Bold} [#{Bold}limit#{Bold}] : show #{Bold}limit#{Bold} (default: 5, max: 15) entries from rss #{Bold}handle#{Bold}"\r
+      "rss show #{Bold}handle#{Bold} [#{Bold}limit#{Bold}] : show #{Bold}limit#{Bold} (default: 5, max: 15) entries from rss #{Bold}handle#{Bold}; #{Bold}limit#{Bold} can also be in the form a..b, to display a specific range of items"\r
     when "list"\r
       "rss list [#{Bold}handle#{Bold}] : list all rss feeds (matching #{Bold}handle#{Bold})"\r
     when "watched"\r
@@ -174,27 +174,55 @@ class RSSFeedsPlugin < Plugin
 \r
   def show_rss(m, params)\r
     handle = params[:handle]\r
-    limit = params[:limit].to_i\r
-    limit = 15 if limit > 15\r
-    limit = 1 if limit <= 0\r
+    lims = params[:limit].to_s.match(/(\d+)(?:..(\d+))?/)\r
+    debug lims.to_a.inspect\r
+    if lims[2]\r
+      ll = [[lims[1].to_i-1,lims[2].to_i-1].min,  0].max\r
+      ul = [[lims[1].to_i-1,lims[2].to_i-1].max, 14].min\r
+      rev = lims[1].to_i > lims[2].to_i\r
+    else\r
+      ll = 0\r
+      ul = [[lims[1].to_i-1, 1].max, 14].min\r
+      rev = false\r
+    end\r
+\r
     feed = @feeds.fetch(handle, nil)\r
     unless feed\r
       m.reply "I don't know any feeds named #{handle}"\r
       return\r
     end\r
-    m.reply("Please wait, querying...")\r
+\r
+    m.reply "lemme fetch it..."\r
     title = items = nil\r
     @@mutex.synchronize {\r
       title, items = fetchRss(feed, m)\r
     }\r
     return unless items\r
-    m.reply("Channel : #{title}")\r
-    # TODO: optional by-date sorting if dates present\r
-    items[0...limit].reverse.each do |item|\r
+\r
+    # We sort the feeds in freshness order (newer ones first)\r
+    items = freshness_sort(items)\r
+    disp = items[ll..ul]\r
+    disp.reverse! if rev\r
+\r
+    m.reply "Channel : #{title}"\r
+    disp.each do |item|\r
       printFormattedRss(feed, item, {:places=>[m.replyto],:handle=>nil,:date=>true})\r
     end\r
   end\r
 \r
+  def itemDate(item,ex=nil)\r
+    return item.pubDate if item.respond_to?(:pubDate)\r
+    return item.date if item.respond_to?(:date)\r
+    return ex\r
+  end\r
+\r
+  def freshness_sort(items)\r
+    notime = Time.at(0)\r
+    items.sort { |a, b|\r
+      itemDate(b, notime) <=> itemDate(a, notime)\r
+    }\r
+  end\r
+\r
   def list_rss(m, params)\r
     wanted = params[:handle]\r
     reply = String.new\r
@@ -335,7 +363,7 @@ class RSSFeedsPlugin < Plugin
     return feed\r
   end\r
 \r
-  def rewatch_rss(m=nil)\r
+  def rewatch_rss(m=nil, params=nil)\r
     kill_threads\r
 \r
     # Read watches from list.\r
@@ -410,7 +438,24 @@ class RSSFeedsPlugin < Plugin
     if opts\r
       places = opts[:places] if opts.key?(:places)\r
       handle = opts[:handle].to_s if opts.key?(:handle)\r
-      date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S") + " :: " if (opts.key?(:date) && opts[:date] && item.pubDate)\r
+      if opts.key?(:date) && opts[:date]\r
+        if item.respond_to?(:pubDate) \r
+          if item.pubDate.class <= Time\r
+            date = item.pubDate.strftime("%Y/%m/%d %H.%M.%S")\r
+          else\r
+            date = item.pubDate.to_s\r
+          end\r
+        elsif  item.respond_to?(:date)\r
+          if item.date.class <= Time\r
+            date = item.date.strftime("%Y/%m/%d %H.%M.%S")\r
+          else\r
+            date = item.date.to_s\r
+          end\r
+        else\r
+          date = "(no date)"\r
+        end\r
+        date += " :: "\r
+      end\r
     end\r
     title = "#{Bold}#{item.title.chomp.riphtml}#{Bold}" if item.title\r
     desc = item.description.gsub(/\s+/,' ').strip.riphtml.shorten(@bot.config['rss.text_max']) if item.description\r
@@ -440,7 +485,9 @@ class RSSFeedsPlugin < Plugin
   def fetchRss(feed, m=nil)\r
     begin\r
       # Use 60 sec timeout, cause the default is too low\r
-      xml = @bot.httputil.get_cached(feed.url,60,60)\r
+      # Do not use get_cached for RSS until we have proper cache handling\r
+      # xml = @bot.httputil.get_cached(feed.url,60,60)\r
+      xml = @bot.httputil.get(feed.url,60,60)\r
     rescue URI::InvalidURIError, URI::BadURIError => e\r
       report_problem("invalid rss feed #{feed.url}", e, m)\r
       return\r
@@ -503,7 +550,7 @@ plugin = RSSFeedsPlugin.new
 \r
 plugin.map 'rss show :handle :limit',\r
   :action => 'show_rss',\r
-  :requirements => {:limit => /^\d+$/},\r
+  :requirements => {:limit => /^\d+(?:\.\.\d+)?$/},\r
   :defaults => {:limit => 5}\r
 plugin.map 'rss list :handle',\r
   :action => 'list_rss',\r
@@ -533,5 +580,6 @@ plugin.map 'rss unwatch :handle',
   :action => 'unwatch_rss'\r
 plugin.map 'rss rmwatch :handle',\r
   :action => 'unwatch_rss'\r
-plugin.map 'rss rewatch :handle',\r
+plugin.map 'rss rewatch',\r
   :action => 'rewatch_rss'\r
+\r