tt25 / nicokit

Niko Niko Douga binding Ruby

This URL has Read+Write access

tt25 (author)
Thu May 15 07:30:00 -0700 2008
nicokit / nicokit.rb
100644 346 lines (313 sloc) 7.751 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
96
97
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
145
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
require "open-uri"
require "parsedate"
require "date"
require "rexml/document"
require "rubygems"
require "hpricot"
require "mechanize"
 
 
# POST rawdata
# based on http://d.hatena.ne.jp/emergent/20070909/1189316316
module WWW
  class Mechanize
    self.class_eval {
      def post_data(url, data='')
        cur_page = current_page || Page.new( nil, {'content-type'=>'text/html'})
        request_data = data
        abs_url = to_absolute_uri(url, cur_page)
        request = fetch_request(abs_url, :post)
        request.add_field('Content-Length', request_data.size.to_s)
 
        page = fetch_page(abs_url, request, cur_page, [request_data])
        add_to_history(page)
        page
      end
    }
  end
end
 
module Nicokit
BASE_URI="http://www.nicovideo.jp/"
API_URI=BASE_URI+"api/"
 
@ua=WWW::Mechanize.new
@login=false
 
def self.login mail,pw
url="https://secure.nicovideo.jp/secure/"
f=@ua.get(url).form("login")
f.mail=mail
f.password=pw
@ua.submit(f)
@login=true
end
 
def self.get url,&block
if !@login
raise "まだログインしてません"
end
block ||= Proc.new {}
block.call(@ua.get(url))
end
 
 
def self.post_data url,data,&block
if !@login
raise "まだログインしてません"
end
block ||= Proc.new {}
block.call(@ua.post_data(url,data))
end
 
class My
def initialize
@_fetched={}
@id=nil
@list=nil
"list".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
_get(:#{var},'list')
end
GETTER
}
end
def _get key,meth
key2=key.to_s.gsub(/^@?/,"@")
result=self.instance_variable_get(key2)
if !result && !@_fetched[meth]
send("_fetch_#{meth}")
@_fetched[meth]=true
end
self.instance_variable_get(key2)
end
 
def _fetch_list
mylist_url=Nicokit::BASE_URI+"mylistgroup_edit"
list=[]
 
Nicokit.get(mylist_url){|res|
Hpricot(res.body).search('table#mylistgroups tr[@id $= "row"]'){|tr|
next if tr.attributes["id"].match(/___row$/)
#hash={}
#hash[:thumbnail]=tr.at('td // img[@class="thumb_img_S"]')[:src]
#link=tr.at('td / h3 // a[1]')
#hash[:link]=URI.join(mylist_url,link[:href]).to_s
#hash[:name]=link.inner_html
#hash[:count]=tr.at('td / h3 /strong[1]').inner_html.match(/[0-9]+/)[0]
#hash[:is_public]=(tr.at('td / h3 / a:last')[:class] == "public")
 
#list << hash
#link=
link=tr.at('td / h3 // a[1]')[:href]
list << Nicokit::VideoList.new(link.match(/[0-9]*$/)[0])
}
info=Hpricot(res.body).at('div.mb16p4')
@title=info.at('h1').inner_html
@description=info.at('p[@class="TXT12][1]')
}
@list=list
end
end
 
class VideoList
def initialize id
@id=id
@url=Nicokit::BASE_URI+"mylist/#{@id}"
@vodeos=nil
@title=nil
@description=nil
"videos title description".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
_get :#{var}
end
GETTER
}
end
def _get key
key=key.to_s.gsub(/^@?/,"@")
result=self.instance_variable_get(key)
if !result
_fetch
end
self.instance_variable_get(key)
end
def _fetch
list=[]
Nicokit.get(@url){|res|
Hpricot(res.body).search('table#mylists > tr'){|tr|
next if tr.attributes["id"].match(/_/)
 
# fix strange href
url=URI.join(@url,"/"+tr.at('//h3/a')[:href]).to_s
 
list << Nicokit::Video.new(url)
}
info=Hpricot(res.body).at('div.mb16p4')
@title=info.at('h1').inner_html
@description=info.at('p[@class="TXT12][2]').inner_html
}
@videos=list
end
end
 
class Video
attr_reader :id,:url
 
class FLV
def initialize watch_url,flv
@watch_url=watch_url
@flv=flv
end
def to_s
@flv
end
def save filename
Nicokit.get(@watch_url)
Nicokit.get(@flv){|r|
File.open(filename,"w"){|fp|
fp.puts(r.body)
}
}
end
end
def initialize id
@_fetched={
"info" => false,
"msg" => false,
"comments" => false,
"recommend" => false,
}
if id.match(/^http:/)
@url=id
@id=@url.match(/[a-z]*[0-9]+$/)[0]
else
@id=id.gsub(/^(sm)?/,"sm")
@url=Nicokit::BASE_URI+"watch/#{@id}"
end
 
 
"flv msg_server myid thread".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
get(:#{var},'msg')
end
GETTER
}
"title posted mylist comment thumbnail last_comments description tags view".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
get(:#{var},'info')
end
GETTER
}
"comments".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
get(:#{var},'comments')
end
GETTER
}
"recommend".split(/\s+/).each{|var|
self.instance_variable_set("@#{var}",nil)
instance_eval <<-GETTER
def #{var}
get(:#{var},'recommend')
end
GETTER
}
end
def get key,meth
key2=key.to_s.gsub(/^@?/,"@")
result=self.instance_variable_get(key2)
if !result && !@_fetched[meth]
send("_fetch_#{meth}")
@_fetched[meth]=true
end
self.instance_variable_get(key2)
end
 
def _fetch_msg
api=Nicokit::API_URI+"getflv/#{@id}"
msg={}
Nicokit.get(api){|r|
msg=r.body.split("&").inject({}){|q,param|
tmp=param.split("=")
q[tmp[0].to_sym]=URI.unescape(tmp[1])
q
}
@flv=Nicokit::Video::FLV.new(self.url,msg[:url])
@msg_server=msg[:ms]
@thread=msg[:thread_id]
@myid=msg[:user_id]
}
end
 
def _fetch_recommend
url=Nicokit::API_URI+"getrelation?video=#{self.id}"
recommend=[]
Nicokit.get(url){|res|
rexml=REXML::Document.new(res.body)
list=Nicokit.xml2hash(REXML::XPath.match(rexml,'//video'))
list.map!{|v|
Nicokit::Video.new(v[:url])
}
recommend = list
}
@recommend=recommend
end
 
def _fetch_comments
postxml="<thread user_id=\"#{self.myid}\" res_from=\"-1000\" version=\"20061206\" thread=\"#{self.thread}\" />"
Nicokit.post_data(self.msg_server,postxml){|page|
rexml=REXML::Document.new(page.body)
comments=[]
rexml.elements.each( '//chat' ){|n|
hash={}
hash=n.attributes.inject({}){|r,attr|
r[attr[0].to_sym]=attr[1]
r
}
hash[:comment]=n.text
comments << hash
}
@comments=comments
}
end
 
def _fetch_info
api=Nicokit::API_URI+"getthumbinfo/#{@id}"
 
Nicokit.get(api){|res|
rexml=REXML::Document.new(res.body)
tmp=Nicokit.xml2hash(REXML::XPath.match(rexml,'//thumb'))[0]
next if !tmp
 
@title=tmp[:title]
@posted=DateTime.parse(tmp[:first_retrieve])
@mylist=tmp[:mylist_counter].to_i
@comment=tmp[:comment_num].to_i
@thumbnail=tmp[:thumbnail_url]
@last_comments=tmp[:last_res_body]
@description=tmp[:description]
@tags=tmp[:tags][:tag]
@view=tmp[:view_counter].to_i
 
@url=tmp[:watch_url]
@id=tmp[:video_id]
}
end
end
 
def self.xml2hash doc
tmp=Proc.new {|item|
hash={}
item.each{|n|
next if n.class.to_s.match(/Text/) # whitespace node?
 
# DetailPageURL => :detail_page_url
key=n.name.to_s.gsub(/([a-z])([A-Z]+)/,"\\1_\\2").downcase.to_sym
 
if n.has_elements?
hash[key]=tmp.call(n.elements.to_a)
else
if hash[key]
# <tags> <tag>1</tag><tag>2</tag> </tags>
# to
# hash[:tags]={:tag => [1,2]}
if hash[key].class != [].class
tmp=hash[key]
hash[key]=[tmp]
end
hash[key] << n.text
else
hash[key]=n.text
end
end
}
hash
}
result=[]
doc.each{|n|
result << tmp.call(n)
}
result
end
end