# # 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' enable_js('nicovideo.js') unless base_url =~ /\Ahttps:/ 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.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 } status = xml.scan(%r||).flatten.first if status == 'ok' then raw_api = xml.scan(%r|(.*)|m).flatten.first.scan(%r|<(.*?)>(.*)|) api = {} raw_api.each do |key, value| api[key] = @conf.to_native( value ) end api 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['watch_url'] i[:thumb] = elem['thumbnail_url'] i[:title] = label || elem['title'] i[:desc] = elem['description'] i[:comment] = elem['last_res_body'] i[:date] = elem['first_retrieve'] i[:length] = elem['length'] i[:view] = elem['view_counter'] i[:comment_num] = elem['comment_num'] i[:mylist] = elem['mylist_counter'] 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 = [640,360]) if feed? nicovideo(video_id) else q = size ? "?w=#{h size[0]}&h=#{h size[1]}" : '' %Q|| end end def nicovideo( video_id, label = nil, link = nil ) begin r = '' r << %Q|
| api = nicovideo_call_api( video_id ) thumb = @conf.to_native( nicovideo_inline( video_id, api, label, link ), 'UTF-8' ) r << thumb r << '
' if feed? r.gsub!( /]*)?>/, '' ) r.gsub!( %r{}, '' ) else r << %Q|| end r rescue ::Errno::ENOENT "Sorry, #{video_id} was deleted." rescue Timeout::Error, NoMethodError, SecurityError, StandardError puts $! $@.each{|l| puts l} nicovideo_iframe( video_id ) end end