| 1 | # |
|---|
| 2 | # nicovideo.rb - tdiary plugin for Nico Nico Video |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) TADA Tadashi <sho@spc.gr.jp> |
|---|
| 5 | # Distributed under GPL. |
|---|
| 6 | # |
|---|
| 7 | # usage: |
|---|
| 8 | # Link to the movie and show thumbnail , description...: |
|---|
| 9 | # <%= nicovideo 'sm99999999' %> |
|---|
| 10 | # |
|---|
| 11 | # Link to the movie with original label: |
|---|
| 12 | # <%= nicovideo 'sm99999999', 'movie title' %> |
|---|
| 13 | # |
|---|
| 14 | # Link to the movie with original label and link: |
|---|
| 15 | # <%= nicovideo 'sm99999999', 'movie title', 'http://example.com/video' %> |
|---|
| 16 | # |
|---|
| 17 | # Show Inline player: |
|---|
| 18 | # <%= nicovideo_player 'sm99999999' %> |
|---|
| 19 | # |
|---|
| 20 | # Show Inline player with size: |
|---|
| 21 | # <%= nicovideo_player 'sm99999999', [400,300] %> |
|---|
| 22 | # |
|---|
| 23 | require 'open-uri' |
|---|
| 24 | require 'timeout' |
|---|
| 25 | require 'rexml/document' |
|---|
| 26 | |
|---|
| 27 | def nicovideo_call_api( video_id ) |
|---|
| 28 | uri = "http://www.nicovideo.jp/api/getthumbinfo/#{video_id}" |
|---|
| 29 | proxy = @conf['proxy'] |
|---|
| 30 | proxy = 'http://' + proxy if proxy |
|---|
| 31 | xml = timeout( feed? ? 10 : 2 ) { open( uri, :proxy => proxy ) {|f| f.read } } |
|---|
| 32 | doc = REXML::Document::new( xml ).root |
|---|
| 33 | res = doc.elements.to_a( '/nicovideo_thumb_response' )[0] |
|---|
| 34 | if res.attributes['status'] == 'ok' then |
|---|
| 35 | res.elements.to_a( 'thumb' )[0] |
|---|
| 36 | else |
|---|
| 37 | raise ::Errno::ENOENT::new |
|---|
| 38 | end |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | def nicovideo_inline( elem, label = nil, link = nil ) |
|---|
| 42 | i = {} |
|---|
| 43 | i[:url] = link || elem.to_a( 'watch_url' )[0].text |
|---|
| 44 | i[:thumb] = elem.to_a( 'thumbnail_url' )[0].text |
|---|
| 45 | i[:title] = label || elem.to_a( 'title' )[0].text |
|---|
| 46 | i[:desc] = elem.to_a( 'description' )[0].text |
|---|
| 47 | i[:comment] = @conf.mobile_agent? ? '' : elem.to_a( 'last_res_body' )[0].text |
|---|
| 48 | i[:date] = elem.to_a( 'first_retrieve' )[0].text |
|---|
| 49 | i[:length] = elem.to_a( 'length' )[0].text |
|---|
| 50 | i[:view] = elem.to_a( 'view_counter' )[0].text |
|---|
| 51 | i[:comment_num] = elem.to_a( 'comment_num' )[0].text |
|---|
| 52 | i[:mylist] = elem.to_a( 'mylist_counter' )[0].text |
|---|
| 53 | |
|---|
| 54 | if feed? then |
|---|
| 55 | result = nicovideo_feed( i ) |
|---|
| 56 | else |
|---|
| 57 | result = nicovideo_html( i ) |
|---|
| 58 | end |
|---|
| 59 | result.gsub( /^\t+/, '' ) |
|---|
| 60 | end |
|---|
| 61 | |
|---|
| 62 | def nicovideo_iframe( video_id ) |
|---|
| 63 | %Q|<iframe src="http://www.nicovideo.jp/thumb/#{video_id}" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"><a href="http://www.nicovideo.jp/watch/#{video_id}">#{label || 'link for nicovideo'}</a></iframe>\n| |
|---|
| 64 | end |
|---|
| 65 | |
|---|
| 66 | def nicovideo( video_id, label = nil, link = nil ) |
|---|
| 67 | begin |
|---|
| 68 | @conf.to_native( nicovideo_inline( nicovideo_call_api( video_id ).elements, label, link ), 'UTF-8' ) |
|---|
| 69 | rescue ::Errno::ENOENT |
|---|
| 70 | "<strong>Sorry, #{video_id} was deleted.</strong>" |
|---|
| 71 | rescue Timeout::Error, OpenURI::HTTPError, SecurityError |
|---|
| 72 | nicovideo_iframe( video_id ) |
|---|
| 73 | end |
|---|
| 74 | end |
|---|
| 75 | |
|---|
| 76 | def nicovideo_player( video_id, size = [544,427] ) |
|---|
| 77 | if feed? or @conf.mobile_agent? then |
|---|
| 78 | nicovideo( video_id ) |
|---|
| 79 | else |
|---|
| 80 | s = '' |
|---|
| 81 | if size then |
|---|
| 82 | s = "?w=#{h size[0]}&h=#{h size[1]}" |
|---|
| 83 | end |
|---|
| 84 | %Q|<script type="text/javascript" src="#{nicovideo_player_path}/thumb_watch/#{video_id}#{s}"></script>| |
|---|
| 85 | end |
|---|
| 86 | end |
|---|