7 attr_accessor :host, :root_path, :jcode, :dlkey, :authorization, :local_dir, :base_html
10 @jcode = ERB::Util.u("
\8a¿
\8e\9a")
14 @base_html = "upload.html"
20 @jcode = ERB::Util.u(NKF.nkf('-Sw', "
\8a¿
\8e\9a"))
25 header = UserAgent.default_http_header.dup
26 header["Authorization"] = authorization if authorization
31 File.join(local_dir, File.basename(path))
34 def local_file_exist?(filename)
35 File.exist?(local_path(filename))
38 def local_file_match?(filename_re)
39 Dir.foreach(local_dir) do |ent|
40 if md = filename_re.match(ent)
47 def download(fileno, filename_re = nil)
48 if filename_re && local_file_exist?(filename_re)
52 Net::HTTP.new(host).start do |http|
53 response = http.post("#{root_path}/upload.cgi", "file=#{fileno}&jcode=#{jcode}&mode=dl&dlkey=#{dlkey}", header)
56 if md = /"1;URL=(.+?)"/.match(body)
58 path.sub!(/\A\./, root_path)
62 open(local_path(path), "wb") do |file|
63 response = http.get(path, header) do |data|
71 def download_file_through_counter(filename)
72 if local_file_exist?(filename)
76 Net::HTTP.new(host).start do |http|
77 response = http.get("#{root_path}/src/#{filename}.html", header)
78 file_re = Regexp.new(Regexp.escape('count.cgi?') + '(.+)')
80 text.scan(/href="(.+?)"/) do |href, |
81 if md = file_re.match(href)
82 path = root_path + '/src/' + md[1]
83 open(local_path(filename), "wb") do |file|
84 response = http.get(path, header) do |data|
93 def download_file(filename)
94 if local_file_exist?(filename)
98 Net::HTTP.new(host).start do |http|
99 response = http.get("#{root_path}/src/#{filename}.html", header)
101 if md = /href="(.+?)"/.match(body)
103 path.sub!(/\A\./, root_path + "/src")
107 open(local_path(path), "wb") do |file|
108 response = http.get(path, header) do |data|
116 def download_file_directly(filename, dir = '/src')
117 if local_file_exist?(filename)
121 path = "#{root_path}#{dir}/#{filename}"
123 Net::HTTP.new(host).start do |http|
124 open(local_path(path), "wb") do |file|
125 response = http.get(path, header) do |data|
132 def download_file_directly_id(filename)
133 if local_file_exist?(filename)
137 path = "#{root_path}/?id=#{filename}"
139 http = Net::HTTP.new(host)
140 http.open_timeout = 10
141 http.read_timeout = 20
144 open(local_path(filename), "wb") do |file|
145 response = http.get(path, header) do |data|
150 rescue Timeout::Error
160 Net::HTTP.new(host).start do |http|
161 response = http.get("#{root_path}/#{base_html}", header)
162 # response.each do |name, value|
163 # puts [name, value].join(': ')