Class: Bnicovideo::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/bnicovideo/video.rb

Overview

This class represents video of Niconico Douga

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_session, video_id) ⇒ Video

Initialize from user session and video ID

user_session

Bnicovideo::UserSession object

video_id

Video ID



47
48
49
50
51
# File 'lib/bnicovideo/video.rb', line 47

def initialize(user_session, video_id)
  @user_session = user_session
  @video_id = video_id
  @info_got = false
end

Instance Attribute Details

#comment_numObject (readonly)

How many comments were post



31
32
33
# File 'lib/bnicovideo/video.rb', line 31

def comment_num
  @comment_num
end

#deletedObject (readonly)

Whether this video was deleted



43
44
45
# File 'lib/bnicovideo/video.rb', line 43

def deleted
  @deleted
end

#descriptionObject (readonly)

Video description



19
20
21
# File 'lib/bnicovideo/video.rb', line 19

def description
  @description
end

#embeddableObject (readonly)

Embeddable this video



35
36
37
# File 'lib/bnicovideo/video.rb', line 35

def embeddable
  @embeddable
end

#first_retrieveObject (readonly)

DateTime object when the video was post



23
24
25
# File 'lib/bnicovideo/video.rb', line 23

def first_retrieve
  @first_retrieve
end

#lengthObject (readonly)

Video length in seconds



25
26
27
# File 'lib/bnicovideo/video.rb', line 25

def length
  @length
end

#live_allowedObject (readonly)

Whether this video can play in Niconico live streaming



37
38
39
# File 'lib/bnicovideo/video.rb', line 37

def live_allowed
  @live_allowed
end

#movie_typeObject (readonly)

Movie type(mp4, flv, …)



27
28
29
# File 'lib/bnicovideo/video.rb', line 27

def movie_type
  @movie_type
end

#mylist_counterObject (readonly)

How many mylists have this video



33
34
35
# File 'lib/bnicovideo/video.rb', line 33

def mylist_counter
  @mylist_counter
end

#tagsObject (readonly)

Tags’ hash. Key is language and value is the array of Bnicovideo::Tag object.



39
40
41
# File 'lib/bnicovideo/video.rb', line 39

def tags
  @tags
end

#thumbnail_urlObject (readonly)

Thumbnail URL



21
22
23
# File 'lib/bnicovideo/video.rb', line 21

def thumbnail_url
  @thumbnail_url
end

#titleObject (readonly)

Video title



17
18
19
# File 'lib/bnicovideo/video.rb', line 17

def title
  @title
end

#user_idObject (readonly)

User id of this video’s author



41
42
43
# File 'lib/bnicovideo/video.rb', line 41

def user_id
  @user_id
end

#video_idObject (readonly)

Video ID



15
16
17
# File 'lib/bnicovideo/video.rb', line 15

def video_id
  @video_id
end

#view_counterObject (readonly)

How many times this video was viewed



29
30
31
# File 'lib/bnicovideo/video.rb', line 29

def view_counter
  @view_counter
end

Instance Method Details

#access_getflv_apiObject

Access getflv API



194
195
196
197
198
199
200
201
202
# File 'lib/bnicovideo/video.rb', line 194

def access_getflv_api
  resp2 = nil
  Net::HTTP.start('flapi.nicovideo.jp') do |http|
    resp2 = http.get('/api/getflv/' + @video_id + '?as3=1',
      {'Cookie' => 'user_session=' + @user_session.session_id})
  end
  flvhsh = CGI.parse(resp2.body)
  return flvhsh
end

#download(filename = nil) ⇒ Object

Download video

filename

File name or stream. If nil, return binary



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bnicovideo/video.rb', line 98

def download(filename = nil)
  self.get_info
  return nil if @deleted
  resp = nil
  Net::HTTP.start('www.nicovideo.jp') do |http|
    resp = http.get('/watch/' + @video_id,
      {'Cookie' => 'user_session=' + @user_session.session_id})
  end
  adc = {}
  resp.each do |k, v|
    if k.downcase == 'set-cookie'
      z = v.split(';')
      z.map!{|cp| cp.gsub(/(^ +)|( +$)/, '')}
      hsh = {}
      z.each do |ckchr|
        ckchra = ckchr.split('=', 2)
        hsh[ckchra[0]] = ckchra[1]
      end
      hsh.each do |nk, nv|
        adc[nk] = nv unless ['expires', 'path', 'domain', 'secure'].include?(nk)
      end
    end
  end
  movie_url = access_getflv_api['url'][0]
  return if movie_url == nil
  resp3 = nil
  huri = URI.parse(movie_url)
  cks = {'user_session' => @user_session.session_id}.merge(adc)
  ckarr = []
  cks.each do |ckk, ckv|
    ckarr.push(ckk + '=' + ckv)
  end
  ckstr = ckarr.join('; ')
  Net::HTTP.start(huri.host) do |http|
    resp3 = http.get(huri.request_uri, {'Cookie' => ckstr})
  end
  return unless resp3.code.to_i == 200
  if filename == nil
    return resp3.body
  elsif filename.respond_to?(:write)
    filename.write(resp3.body)
  else
    File.open(filename, 'wb') do |file|
      file.write resp3.body
    end
  end
end

#get_commentsObject

Get comments



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bnicovideo/video.rb', line 146

def get_comments
  self.get_info
  return [] if @deleted
  hsh = access_getflv_api
  tid = hsh['thread_id'][0]
  return unless tid
  ms = hsh['ms'][0]
  uid = hsh['user_id'][0]
  req = '<packet><thread thread="' + tid + '" version="20090904" user_id="' +
    uid + '" score="1" /><thread_leaves thread="' + tid + '" user_id="' +
    uid + '" scores="1">0-' + ((@length + 59) / 60).to_s + ':100, 1000' +
    '</thread_leaves><thread thread="' + tid + '" version="20061206" ' +
    'res_from="-1000" fork="1" scores="1" /></packet>'
  uri = URI.parse(ms)
  resp = nil
  Net::HTTP.start(uri.host) do |http|
    resp = http.post(uri.request_uri, req,
      {'Cookie' => 'user_session=' + @user_session.session_id,
      'Content-Type' => 'application/xml'})
  end
  xml = REXML::Document.new(resp.body)
  ret = []
  xml.root.elements.each('chat') do |chn|
    comno = chn.attributes['no'].to_i
    vat = chn.attributes['vpos'].to_i / 100.0
    tat = Time.at(chn.attributes['date'].to_i)
    is184 = chn.attributes['anonymity'] == '1'
    user_id = chn.attributes['user_id']
    is_premium = chn.attributes['premium'] == '1'
    score = 0
    if chn.attributes['score']
      score = chn.attributes['score'].to_i
    end
    is_creator_comment = chn.attributes['fork'] == '1'
    ret.push({
      :comment_no => comno,
      :video_position => vat,
      :comment_at => tat,
      :anonymous? => is184,
      :user_id => user_id,
      :premium? => is_premium,
      :score => score,
      :creator_comment? => is_creator_comment
      })
  end
  return ret
end

#get_info(refresh = false) ⇒ Object

Get video information

refresh

If this is set true, force get. Otherwise, get only if not gotten.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bnicovideo/video.rb', line 54

def get_info(refresh = false)
  if @info_got && !refresh
    return
  end
  resp = nil
  Net::HTTP.start('ext.nicovideo.jp') do |http|
    resp = http.get('/api/getthumbinfo/' + @video_id,
      {'Cookie' => 'user_session=' + @user_session.session_id})
  end
  resp.value
  xml = REXML::Document.new(resp.body)
  root = xml.root
  if root.attributes['status'] == 'ok'
    @title = root.elements['thumb/title'].text
    @description = root.elements['thumb/description'].text
    @thumbnail_url = root.elements['thumb/thumbnail_url'].text
    @first_retrieve = DateTime.parse(root.elements['thumb/first_retrieve'].text)
    length_string = root.elements['thumb/length'].text
    length_arr = length_string.split(':')
    @length = length_arr[0].to_i * 60 + length_arr[1].to_i
    @movie_type = root.elements['thumb/movie_type'].text
    @view_counter = root.elements['thumb/view_counter'].text.to_i
    @comment_num = root.elements['thumb/comment_num'].text.to_i
    @mylist_counter = root.elements['thumb/mylist_counter'].text.to_i
    @embeddable = root.elements['thumb/embeddable'].text == '1'
    @live_allowed = root.elements['thumb/no_live_play'].text == '0'
    @tags = {}
    root.elements.each('thumb/tags') do |tse|
      key = tse.attributes['domain']
      dtags = []
      tse.elements.each('tag') do |tge|
        dtags.push(Bnicovideo::Tag.new(tge.text,
            tge.attributes['lock'] == '1', tge.attributes['category'] == '1'))
      end
      @tags[key] = dtags
    end
    @user_id = root.elements['thumb/user_id'].text
  else
    @deleted = true
  end
  @info_got = true
end