2
# nicovideo.rb - tDiary plugin for Nico Nico Video
4
# Copyright (C) TADA Tadashi <t@tdtds.jp>
5
# Distributed under GPL.
8
# Link to the movie and show thumbnail, description...:
9
# <%= nicovideo 'sm99999999' %>
11
# Link to the movie with original label:
12
# <%= nicovideo 'sm99999999', 'movie title' %>
14
# Link to the movie with original label and link:
15
# <%= nicovideo 'sm99999999', 'movie title', 'http://example.com/video' %>
18
# <%= nicovideo_player 'sm99999999' %>
20
# Show Inline player with size:
21
# <%= nicovideo_player 'sm99999999', [400,300] %>
25
require 'rexml/document'
27
def nicovideo_player_js
29
<script type="text/javascript"><!--
30
function nicovideoPlayer( video_id ) {
31
document.getElementById( "thumbnail-" + video_id ).style.display = "none";
32
document.getElementById( "player-" + video_id ).style.display = "inline";
35
function nicovideoThumbnail( video_id ) {
36
document.getElementById( "thumbnail-" + video_id ).style.display = "inline";
37
document.getElementById( "player-" + video_id ).style.display = "none";
48
def nicovideo_call_api( video_id )
49
uri = "http://ext.nicovideo.jp/api/getthumbinfo/#{video_id}"
50
proxy = @conf['proxy']
51
proxy = 'http://' + proxy if proxy
52
xml = timeout( feed? ? 10 : 2 ) { open( uri, :proxy => proxy ) {|f| f.read } }
53
doc = REXML::Document::new( xml ).root
54
res = doc.elements.to_a( '/nicovideo_thumb_response' )[0]
55
if res.attributes['status'] == 'ok' then
56
res.elements.to_a( 'thumb' )[0]
58
raise ::Errno::ENOENT::new
62
def nicovideo_inline( elem, label = nil, link = nil )
64
i[:url] = link || elem.to_a( 'watch_url' )[0].text
65
i[:thumb] = elem.to_a( 'thumbnail_url' )[0].text
66
i[:title] = label || elem.to_a( 'title' )[0].text
67
i[:desc] = elem.to_a( 'description' )[0].text
68
i[:comment] = @conf.mobile_agent? ? '' : elem.to_a( 'last_res_body' )[0].text
69
i[:date] = elem.to_a( 'first_retrieve' )[0].text
70
i[:length] = elem.to_a( 'length' )[0].text
71
i[:view] = elem.to_a( 'view_counter' )[0].text
72
i[:comment_num] = elem.to_a( 'comment_num' )[0].text
73
i[:mylist] = elem.to_a( 'mylist_counter' )[0].text
76
result = nicovideo_feed( i )
78
result = nicovideo_html( i )
80
result.gsub( /^\t+/, '' )
83
def nicovideo_iframe( video_id )
84
%Q|<iframe src="http://www.nicovideo.jp/thumb/#{video_id}" scrolling="no" style="border:1px solid #CCC;" frameborder="0"><a href="http://www.nicovideo.jp/watch/#{video_id}">#{label || 'link for nicovideo'}</a></iframe>\n|
87
def nicovideo_player( video_id, size = [544,384] )
88
if feed? or @conf.mobile_agent? or @conf.iphone? then
93
q = "?w=#{h size[0]}&h=#{h size[1]}"
95
%Q|<script type="text/javascript" src="#{nicovideo_player_path}/thumb_watch/#{video_id}#{q}"></script>|
99
def nicovideo( video_id, label = nil, link = 'INLINE_PLAYER' )
102
r << %Q|<div id="thumbnail-#{video_id}">|
103
thumb = @conf.to_native( nicovideo_inline( nicovideo_call_api( video_id ).elements, label, link ), 'UTF-8' )
104
thumb.gsub!( /"INLINE_PLAYER"/, %Q|"#" onclick="return nicovideoPlayer( '#{video_id}' );"| )
107
if feed? or @conf.mobile_agent? or @conf.iphone? then
108
r.gsub!( /<a(?:[ \t\n\r][^>]*)?>/, '' )
109
r.gsub!( %r{</a[ \t\n\r]*>}, '' )
111
r << %Q|<div id="player-#{video_id}" style="display:none;background-color:#000;margin-left:2em;">|
112
r << %Q|<a name="player-#{video_id}">|
113
r << nicovideo_player( video_id, [544,384] )
115
r << %Q|<div class="nicovideo-player-close"><a href="#" onclick="return nicovideoThumbnail( '#{video_id}' )">Close Player</a></div>|
119
rescue ::Errno::ENOENT
120
"<strong>Sorry, #{video_id} was deleted.</strong>"
121
rescue Timeout::Error, OpenURI::HTTPError, SecurityError
122
nicovideo_iframe( video_id )
126
#def nicovideo( video_id, label = nil, link = nil )
128
# @conf.to_native( nicovideo_inline( nicovideo_call_api( video_id ).elements, label, link ), 'UTF-8' )
129
# rescue ::Errno::ENOENT
130
# "<strong>Sorry, #{video_id} was deleted.</strong>"
131
# rescue Timeout::Error, OpenURI::HTTPError, SecurityError
132
# nicovideo_iframe( video_id )