Prev / Next

2010-04-16 / Ruby で Tumblr をいじる

We Heart It の画像を手動で reblog するのが面倒なので,
1 個アカウントを用意して We Heart It の rss から Tumblr に投げ,
LDR や メインのアカウントの dashboard で見た方が楽じゃね?
ってことで,Tumblr の API を触ってみた.


gem を探したところ,以下のものがみつかるけど,
どれがいいのかわからない.

$ gem search -r tumblr

*** REMOTE GEMS ***

integrity-tumblr (0.1.1)
ruby-tumblr (0.0.2)
tumblr (2.2.0)
tumblr-api (0.1.4)
tumblr-rb (1.3.0)
tumblr4r (0.7.2)
tumblr_cleanr (0.0.1)


ということで,httparty を使って,さくっと書いてみた.

require 'rubygems'
require 'httparty'

class Tumblr
  include HTTParty
  base_uri "www.tumblr.com"

  def initialize(email, password)
    @email = email
    @password  = password
  end

  def authenticate
    options = {:body => {:email => @email, :password => @password}}
    self.class.post('/api/authenticate', options)
  end

  def post_regular(title, body)
    options = {:body => {:email => @email, :password => @password,
      :type => "regular", :title => title, :body => body}}
    self.class.post('/api/write', options)
  end

  def post_photo(source, caption = nil)
    caption = source if caption.nil?
    options = {:body => {:email => @email, :password => @password,
        :type => "photo", :source => source, :caption => caption}}
    self.class.post('/api/write', options)
  end
end


とりあえず,photo の post ができればいいのでこれだけ.
source に指定できるのは url.
ローカルのファイルをアップロードすることは考えていないので.

使い方はこんな感じ.

tumblr = Tumblr.new(email, password)
response = tumblr.post_regular("test", "test てすと テスト")
response = tumblr.post_photo("http://xmonad.org/images/logo.png", "xmonad")
puts response.body, response.code, response.message, response.headers.inspect


(追記)
daily photo upload limit が 75 で,
リセットされるのが 16 時くらいらしい.

ref.
- API | Tumblr
  http://www.tumblr.com/docs/en/api

comments powered by Disqus