Skip to content

Instantly share code, notes, and snippets.

@mugyu
Created March 8, 2012 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugyu/2001143 to your computer and use it in GitHub Desktop.
Save mugyu/2001143 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment