mugyu
3/8/2012 - 2:09 PM

Gaucheでニコ動のタイトルやサムネ画像URLを取得したりとか

Gaucheでニコ動のタイトルやサムネ画像URLを取得したりとか

(use rfc.http)
(use rfc.uri)

(define-constant *getthumbinfo-url* "http://ext.nicovideo.jp/api/getthumbinfo")
(define-constant *ok-string*   "<nicovideo_thumb_response status=\"ok\">")
(define-constant *fail-string* "<nicovideo_thumb_response status=\"fail\">")
(define-constant *elements* '(title description thumbnail_url watch_url))

(define (getthumbinfo video-id)
  (receive (code status body)
    (http-getthumbinfo video-id)
    (if (rxmatch (string->regexp *ok-string*) body)
      (map (lambda (element) 
             (rxmatch-substring ((element->regexp element) body) 1))
        *elements*)
      #f)))

(define (http-getthumbinfo video-id)
  (receive (scheme user-info hostname port path query frament)
    (uri-parse #`",|*getthumbinfo-url*|/,video-id")
    (http-get hostname path)))

(define (element->regexp element)
  (string->regexp #`"<,|element|>(.*?)<\/,|element|>"))
      
(receive (title description thumbnail_url watch_url)
  (apply values (getthumbinfo 'sm9))
  (print title)             ;=> 新・豪血寺一族 -煩悩解放 - レッツゴー!陰陽師
  (print description)       ;=> レッツゴー!陰陽師(フルコーラスバージョン)
  (print thumbnail_url)     ;=> http://tn-skr2.smilevideo.jp/smile?i=9
  (print watch_url))        ;=> http://www.nicovideo.jp/watch/sm9
(print (getthumbinfo 'smx)) ;=> #f