TumblrAPIをRubyから使う
require 'net/http' require 'open-uri' require 'cgi' require 'xmlsimple' class Tumblr def initialize(email=nil, password=nil) Net::HTTP.version_1_2 @email = email @password = password end def to_query_parameter(hash) hash.map{|i| i.map{|j| CGI.escape j.to_s}.join('=') }.join('&') end def method_missing(method_id, *params) params[0][:type] = method_id.to_s params[0][:email] = @email if @email params[0][:password] = @password if @password post(params[0]) end def post(params) Net::HTTP.start("www.tumblr.com", 80) do |http| response = http.post("/api/write", to_query_parameter(params)) end end def get(id, options={}) xml = open("http://#{id}.tumblr.com/api/read/?#{to_query_parameter(options)}").read XmlSimple.xml_in(xml, {'ForceArray' => false}) end end
tumblr = Tumblr.new(<email>,<password>) params = Hash.new params[:source] = "http://img0.gmodules.com/ig/f/_N3WzsBqUfM/intl/ALL_jp/logo.gif" tumblr.photo(params)
例えば写真を投稿する場合はこんな感じ
tumblr = Tumblr.new result = tumblr.get("mostlyfine") puts result
あるユーザのTumblrを取得する場合はこんな感じ
Hashで結果が返ってくる。
ココから下はタイプとかパラメータのメモ。
APIの詳細はTumblr APIを参照
GET
- start(default:0)
- num(default:20)
- type
- regular(メモ)
- quote(引用)
- photo(写真)
- link(リンク)
- conversation(チャット)
- video(動画)
POST
- password
- type
- generator(ポストするアプリケーション名らしい)
Regular
- title(OPTIONAL)
- body
Photo
- source
- data
- caption(OPTIONAL)
Quoto
- quote
- source(OPTIONAL)
Link
- name(OPTIONAL)
- url
- description(OPTIONAL)
Conversation
- title(OPTIONAL)
- conversation
Video
- embed
- caption(OPTIONAL)
- data
Audio
- caption(OPTIONAL)
- data