public
Description: Niko Niko Douga binding Ruby
Homepage: http://tt25.org/blog/20080515/nicokit
Clone URL: git://github.com/tt25/nicokit.git
nicokit / nicokit.rb
100644 150 lines (124 sloc) 2.948 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
=begin
 
** CAUTION **
 
Niko Niko Douga does NOT PERMITTED to many access for a short time.
 
 
mail="xxx@xxx.xxx"
password="xxxxxxxx"
Nicokit.login(mail,password)
 
# Show MyList's all video list title
my=Nicokit::My.new
my.list.each{|l|
puts l.title
}
 
# Someone's MyList
list=Nicokit::VideoList.new("1626019") # mylist id
list.videos.each{|v|
puts "#{v.title} - #{v.url}"
}
 
# Save Video to local(FLV)
v=Nicokit::Video.new("sm811361")
v.flv.save("#{v.title}.flv")
 
=end
 
require "open-uri"
require "parsedate"
require "date"
require "rexml/document"
require "rubygems"
require "hpricot"
require "mechanize"
 
require "nicokit/video"
require "nicokit/videolist"
require "nicokit/ranking"
require "nicokit/search"
require "nicokit/my"
 
 
# 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
@_setting={
:auto_wait => true,
:wait_sec => 2,
}
@last_request=nil
 
def self.setting= options={}
@_setting=@_setting.merge(options)
end
 
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
@last_request=Time.now.to_i
end
 
def self.get url,&block
if !@login
raise "must be login first."
end
if @_setting[:auto_wait] && Time.now.to_i - @last_request < @_setting[:wait_sec]
puts "sleep #{@_setting[:wait_sec]}"
sleep(@_setting[:wait_sec])
end
block ||= Proc.new {}
@last_request=Time.now.to_i
block.call(@ua.get(url))
end
 
 
def self.post_data url,data,&block
if !@login
raise "must be login first."
end
block ||= Proc.new {}
block.call(@ua.post_data(url,data))
end
 
def self.xml2hash doc
tmp_f=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_f.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_f.call(n)
}
result
end
end