bnicovideo 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/bnicovideo/user_session/mac_os_x.rb +1 -1
- data/lib/bnicovideo/version.rb +1 -1
- data/lib/bnicovideo/video.rb +109 -0
- metadata +2 -2
@@ -62,7 +62,7 @@ module Bnicovideo
|
|
62
62
|
cookies_num = page[4..7].unpack('V')[0]
|
63
63
|
nread_ptr = read_ptr
|
64
64
|
cookies_offset = []
|
65
|
-
cookies_num.
|
65
|
+
cookies_num.times do |i|
|
66
66
|
cookie_offset = page[(8 + i * 4)..(11 + i * 4)].unpack('V')[0]
|
67
67
|
cookies_offset.push(cookie_offset)
|
68
68
|
end
|
data/lib/bnicovideo/version.rb
CHANGED
data/lib/bnicovideo/video.rb
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
require 'net/http'
|
6
6
|
require 'rexml/document'
|
7
7
|
require 'date'
|
8
|
+
require 'cgi'
|
9
|
+
require 'uri'
|
8
10
|
|
9
11
|
module Bnicovideo
|
10
12
|
# This class represents video of Niconico Douga
|
@@ -91,5 +93,112 @@ module Bnicovideo
|
|
91
93
|
end
|
92
94
|
@info_got = true
|
93
95
|
end
|
96
|
+
# Download video
|
97
|
+
# filename :: File name or stream. If nil, return binary
|
98
|
+
def download(filename = nil)
|
99
|
+
self.get_info
|
100
|
+
return nil if @deleted
|
101
|
+
resp = nil
|
102
|
+
Net::HTTP.start('www.nicovideo.jp') do |http|
|
103
|
+
resp = http.get('/watch/' + @video_id,
|
104
|
+
{'Cookie' => 'user_session=' + @user_session.session_id})
|
105
|
+
end
|
106
|
+
adc = {}
|
107
|
+
resp.each do |k, v|
|
108
|
+
if k.downcase == 'set-cookie'
|
109
|
+
z = v.split(';')
|
110
|
+
z.map!{|cp| cp.gsub(/(^ +)|( +$)/, '')}
|
111
|
+
hsh = {}
|
112
|
+
z.each do |ckchr|
|
113
|
+
ckchra = ckchr.split('=', 2)
|
114
|
+
hsh[ckchra[0]] = ckchra[1]
|
115
|
+
end
|
116
|
+
hsh.each do |nk, nv|
|
117
|
+
adc[nk] = nv unless ['expires', 'path', 'domain', 'secure'].include?(nk)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
movie_url = access_getflv_api['url'][0]
|
122
|
+
return if movie_url == nil
|
123
|
+
resp3 = nil
|
124
|
+
huri = URI.parse(movie_url)
|
125
|
+
cks = {'user_session' => @user_session.session_id}.merge(adc)
|
126
|
+
ckarr = []
|
127
|
+
cks.each do |ckk, ckv|
|
128
|
+
ckarr.push(ckk + '=' + ckv)
|
129
|
+
end
|
130
|
+
ckstr = ckarr.join('; ')
|
131
|
+
Net::HTTP.start(huri.host) do |http|
|
132
|
+
resp3 = http.get(huri.request_uri, {'Cookie' => ckstr})
|
133
|
+
end
|
134
|
+
return unless resp3.code.to_i == 200
|
135
|
+
if filename == nil
|
136
|
+
return resp3.body
|
137
|
+
elsif filename.respond_to?(:write)
|
138
|
+
filename.write(resp3.body)
|
139
|
+
else
|
140
|
+
File.open(filename, 'wb') do |file|
|
141
|
+
file.write resp3.body
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
# Get comments
|
146
|
+
def get_comments
|
147
|
+
self.get_info
|
148
|
+
return [] if @deleted
|
149
|
+
hsh = access_getflv_api
|
150
|
+
tid = hsh['thread_id'][0]
|
151
|
+
return unless tid
|
152
|
+
ms = hsh['ms'][0]
|
153
|
+
uid = hsh['user_id'][0]
|
154
|
+
req = '<packet><thread thread="' + tid + '" version="20090904" user_id="' +
|
155
|
+
uid + '" score="1" /><thread_leaves thread="' + tid + '" user_id="' +
|
156
|
+
uid + '" scores="1">0-' + ((@length + 59) / 60).to_s + ':100, 1000' +
|
157
|
+
'</thread_leaves><thread thread="' + tid + '" version="20061206" ' +
|
158
|
+
'res_from="-1000" fork="1" scores="1" /></packet>'
|
159
|
+
uri = URI.parse(ms)
|
160
|
+
resp = nil
|
161
|
+
Net::HTTP.start(uri.host) do |http|
|
162
|
+
resp = http.post(uri.request_uri, req,
|
163
|
+
{'Cookie' => 'user_session=' + @user_session.session_id,
|
164
|
+
'Content-Type' => 'application/xml'})
|
165
|
+
end
|
166
|
+
xml = REXML::Document.new(resp.body)
|
167
|
+
ret = []
|
168
|
+
xml.root.elements.each('chat') do |chn|
|
169
|
+
comno = chn.attributes['no'].to_i
|
170
|
+
vat = chn.attributes['vpos'].to_i / 100.0
|
171
|
+
tat = Time.at(chn.attributes['date'].to_i)
|
172
|
+
is184 = chn.attributes['anonymity'] == '1'
|
173
|
+
user_id = chn.attributes['user_id']
|
174
|
+
is_premium = chn.attributes['premium'] == '1'
|
175
|
+
score = 0
|
176
|
+
if chn.attributes['score']
|
177
|
+
score = chn.attributes['score'].to_i
|
178
|
+
end
|
179
|
+
is_creator_comment = chn.attributes['fork'] == '1'
|
180
|
+
ret.push({
|
181
|
+
:comment_no => comno,
|
182
|
+
:video_position => vat,
|
183
|
+
:comment_at => tat,
|
184
|
+
:anonymous? => is184,
|
185
|
+
:user_id => user_id,
|
186
|
+
:premium? => is_premium,
|
187
|
+
:score => score,
|
188
|
+
:creator_comment? => is_creator_comment
|
189
|
+
})
|
190
|
+
end
|
191
|
+
return ret
|
192
|
+
end
|
193
|
+
# Access getflv API
|
194
|
+
def access_getflv_api
|
195
|
+
resp2 = nil
|
196
|
+
Net::HTTP.start('flapi.nicovideo.jp') do |http|
|
197
|
+
resp2 = http.get('/api/getflv/' + @video_id + '?as3=1',
|
198
|
+
{'Cookie' => 'user_session=' + @user_session.session_id})
|
199
|
+
end
|
200
|
+
flvhsh = CGI.parse(resp2.body)
|
201
|
+
return flvhsh
|
202
|
+
end
|
94
203
|
end
|
95
204
|
end
|