root/platform/tdiary/plugin/nicovideo.rb @ 38239

Revision 38239, 4.3 kB (checked in by sho, 4 years ago)

stop loop on mobile phones.

Line 
1#
2# nicovideo.rb - tDiary plugin for Nico Nico Video
3#
4# Copyright (C) TADA Tadashi <t@tdtds.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#
23require 'open-uri'
24require 'timeout'
25require 'rexml/document'
26
27def nicovideo_player_js
28        <<-HTML
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";
33                return false;
34        }
35        function nicovideoThumbnail( video_id ) {
36                document.getElementById( "thumbnail-" + video_id ).style.display = "inline";
37                document.getElementById( "player-" + video_id ).style.display = "none";
38                return false;
39        }
40        //--></script>
41        HTML
42end
43
44add_header_proc do
45        nicovideo_player_js
46end
47
48def 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]
57        else
58                raise ::Errno::ENOENT::new
59        end
60end
61
62def nicovideo_inline( elem, label = nil, link = nil )
63        i = {}
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
74
75        if feed? then
76                result = nicovideo_feed( i )
77        else
78                result = nicovideo_html( i )
79        end
80        result.gsub( /^\t+/, '' )
81end
82
83def 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|
85end
86
87def nicovideo_player( video_id, size = [544,384] )
88        if feed? or @conf.mobile_agent? or @conf.iphone? then
89                nicovideo( video_id )
90        else
91                q = ''
92                if size then
93                        q = "?w=#{h size[0]}&amp;h=#{h size[1]}"
94                end
95                %Q|<script type="text/javascript" src="#{nicovideo_player_path}/thumb_watch/#{video_id}#{q}"></script>|
96        end
97end
98
99def nicovideo( video_id, label = nil, link = 'INLINE_PLAYER' )
100        begin
101                r = ''
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}' );"| )
105                r << thumb
106                r << '</div>'
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]*>}, '' )
110                else
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] )
114                        r << %Q|</a>|
115                        r << %Q|<div class="nicovideo-player-close"><a href="#" onclick="return nicovideoThumbnail( '#{video_id}' )">Close Player</a></div>|
116                        r << %Q|</div>|
117                end
118                r
119        rescue ::Errno::ENOENT
120                "<strong>Sorry, #{video_id} was deleted.</strong>"
121        rescue Timeout::Error, OpenURI::HTTPError, SecurityError
122                nicovideo_iframe( video_id )
123        end
124end
125
126#def nicovideo( video_id, label = nil, link = nil )
127#       begin
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 )
133#       end
134#end
135
Note: See TracBrowser for help on using the browser.