~ubuntu-branches/ubuntu/utopic/tdiary/utopic

« back to all changes in this revision

Viewing changes to contrib2/plugin/nicovideo.rb

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#
 
23
require 'open-uri'
 
24
require 'timeout'
 
25
require 'rexml/document'
 
26
 
 
27
def 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
 
42
end
 
43
 
 
44
add_header_proc do
 
45
        nicovideo_player_js
 
46
end
 
47
 
 
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]
 
57
        else
 
58
                raise ::Errno::ENOENT::new
 
59
        end
 
60
end
 
61
 
 
62
def 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+/, '' )
 
81
end
 
82
 
 
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|
 
85
end
 
86
 
 
87
def 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
 
97
end
 
98
 
 
99
def 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
 
124
end
 
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