2010/01/19

Google SpreadSheets のデータをTwitterにランダムにポストする

なんか手軽に使えるデータストアないかなー・・・、ということでgoogle spreadsheetsをデータソースにできるっぽいという話。
インターバルについては・・・。
(use srfi-27)
(use rfc.http)
(use rfc.uri)
(use rfc.base64)
(use sxml.ssax)
(use sxml.sxpath)

(define *ss-server* "spreadsheets.google.com")
(define *ss-uri-prefix* "/feeds/list/")
(define *ss-uri-sufix* "/od6/public/basic?alt=rss")
(define *twitter-post-path* "/statuses/update.xml?status=")
(define *twitter-base-host* "twitter.com")

(define get
  (lambda (server request-uri)
    (http-get server request-uri)))

(define rss->sxml
  (lambda (srv req-uri)
    (call-with-values (lambda ()
                        (get srv req-uri))
      (lambda (ret ls xml)
        (ssax:xml->sxml (open-input-string xml) '())))))

(define make-src-list
  (lambda (sxml)
    (let ((ls '()))
      (map (lambda (item)
             (append ls (cadar ((sxpath '(// title)) item))))
           ((sxpath '(// item)) sxml)))))

;(make-src-list (rss->sxml *ss-server* (string-append *ss-uri-prefix* *ss-id* *ss-uri-sufix*)))

(define make-authorization
  (lambda (username password)
     (string-append "Basic "
                    (base64-encode-string
                     (string-append username ":" password)))))

(define make-poster
  (lambda (base-host post-path)
    (lambda (auth)
      (lambda (message)
        (http-post base-host
                   (string-append post-path
                                  (uri-encode-string message))
                   ""
                   :authorization auth)))))


(define main
  (lambda (args)
    (let ((tw-usr-name (cadr args))
          (tw-psw (caddr args))
          (post-uri (string-append *ss-uri-prefix* (cadddr args) *ss-uri-sufix*)))
      (letrec ((poster ((make-poster *twitter-base-host* *twitter-post-path*)
                        (make-authorization tw-usr-name tw-psw))))
        (let ((src (make-src-list (rss->sxml *ss-server* post-uri))))
          (random-source-randomize! default-random-source)
          (poster (list-ref src (random-integer (length src)))))))))



参考


プログラミングGaucheプログラミングClojureいつでもワープロ、どこでも表計算! Google Docs&Spreadsheetsの使い方

0 件のコメント:

コメントを投稿