9 attr_accessor :host, :root_path, :jcode, :dlkey, :authorization, :local_dir, :base_html
12 @jcode = ERB::Util.u(NKF.nkf('-Ws', "漢字"))
16 @base_html = "upload.html"
22 @jcode = ERB::Util.u("漢字")
27 header = UserAgent.default_http_header.dup
28 header["Authorization"] = authorization if authorization
33 File.join(local_dir, File.basename(path))
36 def local_file_exist?(filename)
37 File.exist?(local_path(filename))
40 def local_file_match?(filename_re)
41 Dir.foreach(local_dir) do |ent|
42 if md = filename_re.match(ent)
49 def download(fileno, filename_re = nil)
50 if filename_re && local_file_match?(filename_re)
54 Net::HTTP.new(host).start do |http|
55 response = http.post("#{root_path}/upload.cgi", "file=#{fileno}&jcode=#{jcode}&mode=dl&dlkey=#{dlkey}", header)
58 if md = /"1;URL=(.+?)"/.match(body)
60 path.sub!(/\A\./, root_path)
64 open(local_path(path), "wb") do |file|
65 response = http.get(path, header) do |data|
73 def download_file_through_counter(filename)
74 if local_file_exist?(filename)
78 Net::HTTP.new(host).start do |http|
79 response = http.get("#{root_path}/src/#{filename}.html", header)
80 file_re = Regexp.new(Regexp.escape('count.cgi?') + '(.+)')
82 text.scan(/href="(.+?)"/) do |href, |
83 if md = file_re.match(href)
84 path = root_path + '/src/' + md[1]
85 open(local_path(filename), "wb") do |file|
86 response = http.get(path, header) do |data|
95 def download_file_through_clicker(filename)
96 if local_file_exist?(filename)
100 http = Net::HTTP.new(host)
101 http.open_timeout = 10
102 http.read_timeout = 20
105 path = "#{root_path}/src/#{filename}"
106 open(local_path(filename), "wb") do |file|
108 header["Referer"] = "http://#{host}/#{root_path}/src/#{filename}.html"
110 response = http.get(path, header) do |data|
115 rescue Timeout::Error
123 def download_file(filename)
124 if local_file_exist?(filename)
128 Net::HTTP.new(host).start do |http|
129 response = http.get("#{root_path}/src/#{filename}.html", header)
131 if md = /href="(.+?)"/.match(body)
133 path.sub!(/\A\./, root_path + "/src")
137 open(local_path(path), "wb") do |file|
138 response = http.get(path, header) do |data|
146 def download_file_directly(filename, dir = '/src')
147 if local_file_exist?(filename)
151 path = "#{root_path}#{dir}/#{filename}"
153 Net::HTTP.new(host).start do |http|
154 open(local_path(path), "wb") do |file|
155 response = http.get(path, header) do |data|
162 def download_file_directly_id(filename)
163 if local_file_exist?(filename)
167 path = "#{root_path}/?id=#{filename}"
169 http = Net::HTTP.new(host)
170 http.open_timeout = 10
171 http.read_timeout = 20
174 open(local_path(filename), "wb") do |file|
175 response = http.get(path, header) do |data|
180 rescue Timeout::Error
190 Net::HTTP.new(host).start do |http|
191 response = http.get("#{root_path}/#{base_html}", header)
192 # response.each do |name, value|
193 # puts [name, value].join(': ')