]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/tumblr.rb
tumblr: escape the non-range dash in the group regex
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / tumblr.rb
index 55489743e11fffad43b86e3c84b385ad5d9e8b5e..515dc479026e26289ffcf5d1c768eeffe741b172 100644 (file)
@@ -9,10 +9,10 @@
 #
 # Submit URLs to channel-specific tumblr accounts
 #
-# TODO reblog tumblr URLs
-# TODO support video better (e.g. Vimeo or anything else with embed)
+# TODO support other video providers (maybe detect embed codes?)
 # TODO support image better (e.g. pages with a single big image)
 # TODO customize caption/description format
+# TODO do not reblog own posts (maybe?)
 
 require 'rexml/document'
 require 'cgi'
@@ -20,12 +20,18 @@ require 'cgi'
 class TumblrPlugin < Plugin
   RBOT = CGI.escape("rbot #{$version.split.first}")
   WRITE_URL = "http://www.tumblr.com/api/write"
+  REBLOG_URL = "http://www.tumblr.com/api/reblog"
+  READ_URL = "http://%{user}.tumblr.com/api/read?id=%{id}"
   LOGIN = "email=%{email}&password=%{pwd}&group=%{group}&format=markdown&generator=" + RBOT
   PHOTO = "&type=photo&source=%{src}&click-through-url=%{src}"
   VIDEO = "&type=video&embed=%{src}"
   CAPTION = "&caption=%{desc}"
   LINK = "&type=link&url=%{src}"
-  DESC = "&name=%{desc}"
+  NAME = "&name=%{name}"
+  DESC = "&description=%{desc}"
+  REBLOG = "&post-id=%{id}&reblog-key=%{reblog}"
+  COMMENT = "&comment=%{desc}"
+  TAGS = "&tags=%{tags}"
 
   def help(plugin, topic="")
     case topic
@@ -50,25 +56,68 @@ class TumblrPlugin < Plugin
     if line and nick = options[:nick]
       line = "<#{nick}> #{line}"
     end
+    html_line = line ? CGI.escapeHTML(line) : line
+    tags = line ? line.scan(/\[([^\]]+)\]/).flatten : []
 
     req = LOGIN % account
-    type = options[:htmlinfo][:headers]['content-type'].first rescue nil
-    case type
-    when /^image\/.*/
-      data = PHOTO
-      data << CAPTION if line
-    else
-      if url.match(%r{^http://(\w+\.)?youtube\.com/watch.*})
-        data = VIDEO
+    ready = false
+    api_url = WRITE_URL
+    tumblr = options[:htmlinfo][:headers]['x-tumblr-user'].to_s rescue nil
+    if tumblr
+      id = url.match(/\/post\/(\d+)/)
+      if id
+        id = id[1]
+
+        read_url = READ_URL % { :user => tumblr, :id => id}
+        # TODO seems to return 503 a little too frequently
+        xml = @bot.httputil.get(read_url)
+
+        if xml
+          reblog = REXML::Document.new(xml).elements["//post"].attributes["reblog-key"] rescue nil
+          if reblog and not reblog.empty?
+            api_url = REBLOG_URL
+            data = REBLOG
+            data << COMMENT
+            html_line = CGI.escapeHTML("(via <a href=%{url}>%{tumblr}</a>" % {
+              :url => url, :tumblr => tmblr
+            }) unless html_line
+            req << (data % {
+              :id => id,
+              :reblog => reblog,
+              :desc => CGI.escape(html_line)
+            })
+            ready = true
+          end
+        end
+      end
+    end
+
+    if not ready
+      type = options[:htmlinfo][:headers]['content-type'].first rescue nil
+      case type
+      when /^image\/.*/
+        data = PHOTO
         data << CAPTION if line
       else
-        data = LINK
-        data << DESC if line
+        if url.match(%r{^http://(\w+\.)?(youtube\.com/watch.*|vimeo.com/\d+)})
+          data = VIDEO
+          data << CAPTION if line
+        else
+          data = LINK
+          data << NAME if line
+        end
       end
+      data << TAGS unless tags.empty?
+      req << (data % {
+        :src => CGI.escape(url),
+        :desc => CGI.escape(html_line),
+        :tags => CGI.escape(tags.join(',')),
+        :name => CGI.escape(line)
+      })
     end
-    req << (data % { :src => CGI.escape(url), :desc => CGI.escape(line) })
+
     debug "posting #{req.inspect}"
-    resp  = @bot.httputil.post(WRITE_URL, req)
+    resp  = @bot.httputil.post(api_url, req)
     debug "tumblr response: #{resp.inspect}"
   end
 
@@ -139,5 +188,5 @@ plugin.map 'tumblr configure [:channel]', :action => :configuration
 plugin.map 'tumblr deconfigure [:channel]', :action => :deconfigure
 plugin.map 'tumblr configure [:channel] :email :pwd [:group]',
   :action => :configure,
-  :requirements => {:channel => Regexp::Irc::GEN_CHAN, :email => /.+@.+/, :group => /[A-Za-z-]+/}
+  :requirements => {:channel => Regexp::Irc::GEN_CHAN, :email => /\S+@\S+/, :group => /[A-Za-z\-.]+/}