# # nicovideo.rb - tDiary plugin for Nico Nico Video # # Copyright (C) 2012 TADA Tadashi # You can modify and/or distribute it under GPL. # # usage: # Link to the movie and show thumbnail, description...: # <%= nicovideo 'sm99999999' %> # # Link to the movie with original label: # <%= nicovideo 'sm99999999', 'movie title' %> # # Link to the movie with original label and link: # <%= nicovideo 'sm99999999', 'movie title', 'http://example.com/video' %> # # Show Inline player: # <%= nicovideo_player 'sm99999999' %> # # Show Inline player with size: # <%= nicovideo_player 'sm99999999', [400,300] %> # require 'net/http' require 'timeout' require 'rexml/document' enable_js( 'nicovideo.js' ) def nicovideo_call_api( video_id ) uri = "http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}" proxy = @conf['proxy'] proxy = 'http://' + proxy if proxy xml = timeout( feed? ? 10 : 2 ) { px_host, px_port = (@conf['proxy'] || '').split( /:/ ) px_port = 80 if px_host and !px_port Net::HTTP::Proxy( px_host, px_port ).get_response( URI::parse( uri ) ).body } doc = REXML::Document::new( xml ).root res = doc.elements.to_a( '/nicovideo_thumb_response' )[0] if res.attributes['status'] == 'ok' then res.elements.to_a( 'thumb' )[0] else raise ::Errno::ENOENT::new end end def nicovideo_inline( video_id, elem, label = nil, link = nil ) i = {} i[:id] = video_id i[:url] = link || elem.to_a( 'watch_url' )[0].text i[:thumb] = elem.to_a( 'thumbnail_url' )[0].text i[:title] = label || elem.to_a( 'title' )[0].text i[:desc] = elem.to_a( 'description' )[0].text i[:comment] = @conf.mobile_agent? ? '' : elem.to_a( 'last_res_body' )[0].text i[:date] = elem.to_a( 'first_retrieve' )[0].text i[:length] = elem.to_a( 'length' )[0].text i[:view] = elem.to_a( 'view_counter' )[0].text i[:comment_num] = elem.to_a( 'comment_num' )[0].text i[:mylist] = elem.to_a( 'mylist_counter' )[0].text if feed? then result = nicovideo_feed( i ) else result = nicovideo_html( i ) end result.gsub( /^\t+/, '' ) end def nicovideo_iframe( video_id ) %Q|\n| end def nicovideo_player( video_id, size = [544,384] ) if feed? or @conf.mobile_agent? or @conf.iphone? then nicovideo( video_id ) else q = '' if size then q = "?w=#{h size[0]}&h=#{h size[1]}" end %Q|| end end def nicovideo( video_id, label = nil, link = nil ) begin r = '' r << %Q|
| api = nicovideo_call_api( video_id ).elements thumb = @conf.to_native( nicovideo_inline( video_id, api, label, link ), 'UTF-8' ) thumb.gsub!( /"INLINE_PLAYER"/, %Q|"#" onclick="return nicovideoPlayer( '#{video_id}' );"| ) r << thumb r << '
' if feed? or @conf.mobile_agent? or @conf.iphone? then r.gsub!( /]*)?>/, '' ) r.gsub!( %r{}, '' ) else r << %Q|| end r rescue ::Errno::ENOENT "Sorry, #{video_id} was deleted." rescue Timeout::Error, NoMethodError, SecurityError, StandardError nicovideo_iframe( video_id ) end end