module Mod_Uploader
def download(url, password, directory, proxy_addr, proxy_port)
require "net/http"
require "uri"
protocol="http://"
if url.slice(0, protocol.length) == protocol then
url=url.slice(protocol.length, url.length-protocol.length)
end
path=directory+'/'+url
puts path
mkdir_p(File.dirname(path))
uri = URI.parse('http://'+url)
Net::HTTP.Proxy(proxy_addr, proxy_port).start(uri.host, uri.port){|http|
request = Net::HTTP::Post.new(uri.path)
boundary="--1"
request.set_content_type("multipart/form-data; boundary=" + boundary)
body = ""
body.concat("--" + boundary + "\r\n")
body.concat("Content-Disposition: form-data; name=\"download_pass\"\r\n")
body.concat("\r\n")
body.concat(password + "\r\n")
body.concat("--" + boundary + "\r\n")
body.concat("Content-Disposition: form-data; name=\"code_pat\"\r\n")
body.concat("\r\n")
body.concat("\xb5\xfe\r\n")
body.concat("--" + boundary + "\r\n")
body.concat("Content-Disposition: form-data; name=\"submit\"\r\n")
body.concat("\r\n")
body.concat("\xa5\xc0\xa5\xa6\xa5\xf3\xa5\xed\xa1\xbc\xa5\xc9\r\n")
body.concat(boundary + "--\r\n")
request.body = body
read_write(http, request, path)
}
end
def read_write(http, request, path)
http.request(request) do |response|
size=response["Content-Length"].to_f
File.open(path, "w") do |file|
start=Time.now
response.read_body do |data|
file.write data
time=Time.now
past_time=time.to_f-start.to_f
past_seconds=past_time.round
speed=(file.tell / past_time / 1024).round
percent=((file.tell * 100 / size).round rescue 100)
remained_seconds=(size - file.tell)/(file.tell / past_time)
if remained_seconds.infinite? then
remained_seconds=999
else
remained_seconds=remained_seconds.round
end
print "\r"
printf("%3d s spent\t%5d kb/s\t%3d%% done\t%3d s left",
past_seconds, speed, percent, remained_seconds)
end
puts
puts 'finished'
end
end
end
def mkdir_p(mpath)
path = ''
mpath.split('/').each do |f|
path.concat(f)
Dir.mkdir(path) unless path == '' || File.exist?(path)
path.concat('/')
end
end
module_function :mkdir_p
module_function :download
module_function :read_write
end