blog/content/post/2010/09/16/2010-09-16-00001390.md

12 KiB

title author date wordtwit_post_info categories
Tumblr の API Gauche でたたく kazu634 2010-09-16
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5347;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
gauche
Lisp

Tumblr で like したものを RSS にしたくて、 Tumblr API を Gauche でたたくプログラムを作成中です。 Shiro さんの net-twitter を参考にして、とりあえず XML を取得するところまでできました。

ちなみに API のドキュメントはこれです:

ソース

;;; Tumblrへのアクセス用
(use rfc.http)
(use gauche.charconv)
;;; Tumblrで自分のページを取得するために必要となるパラメータ
;;; APIのドキュメントをそのまま書き下す
(define-class <tumblr-simple-read> ()
((start :init-keyword :start :init-value )
(num :init-keyword :num :init-value 20)
(type :init-keyword :type :init-value "")
(id :init-keyword :id :init-value "")
(filter :init-keyword :filter :init-value "")
(tagged :init-keyword :tagged :init-value "")
(search :init-keyword :search :init-value "")))
;;; Tumblrで自分が like したものを取得するために必要となるパラメータ
;;; APIのドキュメントをそのまま書き下す
(define-class <tumblr-liked-posts> ()
((email :init-keyword :email :init-value "example@example.com")
(password :init-keyword :password :init-value "password")
(start :init-keyword :start :init-value )
(num :init-keyword :num :init-value 20)
(filter :init-keyword :filter :init-value "")))
;;; <tumblr-simple-read>をリストにする
;;; リストにすると、 http-compose-query を使用して
;;; queryストリングを簡単に生成できる
(define (make-tumblr-simple-read-param-list params)
`((start ,(ref params 'start))
(num ,(ref params 'num))
(type ,(ref params 'type))
(id ,(ref params 'id))
(filter ,(ref params 'filter))
(tagged ,(ref params 'tagged))
(search ,(ref params 'search))))
;;; <tumblr-liked-posts>をリストにする
;;; リストにすると、 http-compose-query を使用して
;;; http-post が post するデータを簡単に生成できる
(define (make-liked-post-list params)
`((email ,(ref params 'email))
(password ,(ref params 'password))
(start ,(ref params 'start))
(num ,(ref params 'num))
(filter ,(ref params 'filter))))

実行例

下の式を評価すれば、 XML を取得できます:

;;; テスト
(receive (status-code http-header contents)
(http-get "kazu634.tumblr.com"
(http-compose-query
"/api/read"
(make-tumblr-simple-read-param-list
(make <tumblr-simple-read>
:type "photo"))))
(if (equal? status-code "200")
(ces-convert contents "*JP")
#f))
;;; テスト
(receive (status-code http-header contents)
(http-post "yourname.tumblr.com"
"/api/likes"
(make-liked-post-list
(make <tumblr-liked-posts>
:email "Your E-mail address"
:password "Your password")))
(if (equal? status-code "200")
(ces-convert contents "*JP")
#f))

ToDo

あとは、これを SXML に変換してから、RSSを生成すればよいわけですな。

プログラミングGauche

プログラミングGauche