2003-03-28 22:08:50 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Little program to download files from URLs
|
|
|
|
-- LuaSocket 1.5 sample files
|
|
|
|
-----------------------------------------------------------------------------
|
2001-09-27 22:02:34 +02:00
|
|
|
-- formats a number of seconds into human readable form
|
|
|
|
function nicetime(s)
|
|
|
|
local l = "s"
|
|
|
|
if s > 60 then
|
|
|
|
s = s / 60
|
|
|
|
l = "m"
|
|
|
|
if s > 60 then
|
|
|
|
s = s / 60
|
|
|
|
l = "h"
|
|
|
|
if s > 24 then
|
|
|
|
s = s / 24
|
|
|
|
l = "d" -- hmmm
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-03-20 01:24:44 +01:00
|
|
|
if l == "s" then return string.format("%2.0f%s", s, l)
|
|
|
|
else return string.format("%5.2f%s", s, l) end
|
2001-09-27 22:02:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- formats a number of bytes into human readable form
|
|
|
|
function nicesize(b)
|
|
|
|
local l = "B"
|
2001-06-06 22:59:36 +02:00
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
2001-09-27 22:02:34 +02:00
|
|
|
l = "KB"
|
2001-06-06 22:59:36 +02:00
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
2001-09-27 22:02:34 +02:00
|
|
|
l = "MB"
|
2001-06-06 22:59:36 +02:00
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
2001-09-27 22:02:34 +02:00
|
|
|
l = "GB" -- hmmm
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2003-03-20 01:24:44 +01:00
|
|
|
return string.format("%7.2f%2s", b, l)
|
2001-09-27 22:02:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- returns a string with the current state of the download
|
|
|
|
function gauge(got, dt, size)
|
|
|
|
local rate = got / dt
|
|
|
|
if size and size >= 1 then
|
2003-03-20 01:24:44 +01:00
|
|
|
return string.format("%s received, %s/s throughput, " ..
|
2001-09-27 22:02:34 +02:00
|
|
|
"%.0f%% done, %s remaining",
|
|
|
|
nicesize(got),
|
|
|
|
nicesize(rate),
|
|
|
|
100*got/size,
|
|
|
|
nicetime((size-got)/rate))
|
|
|
|
else
|
2003-03-20 01:24:44 +01:00
|
|
|
return string.format("%s received, %s/s throughput, %s elapsed",
|
2001-09-27 22:02:34 +02:00
|
|
|
nicesize(got),
|
|
|
|
nicesize(rate),
|
|
|
|
nicetime(dt))
|
|
|
|
end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- creates a new instance of a receive_cb that saves to disk
|
|
|
|
-- kind of copied from luasocket's manual callback examples
|
2001-09-27 22:02:34 +02:00
|
|
|
function receive2disk(file, size)
|
2001-06-06 22:59:36 +02:00
|
|
|
local aux = {
|
2003-03-20 01:24:44 +01:00
|
|
|
start = socket._time(),
|
2001-06-06 22:59:36 +02:00
|
|
|
got = 0,
|
2003-03-20 01:24:44 +01:00
|
|
|
file = io.open(file, "wb"),
|
2001-09-27 22:02:34 +02:00
|
|
|
size = size
|
2001-06-06 22:59:36 +02:00
|
|
|
}
|
|
|
|
local receive_cb = function(chunk, err)
|
2003-03-28 22:08:50 +01:00
|
|
|
local dt = socket._time() - aux.start -- elapsed time since start
|
2001-06-06 22:59:36 +02:00
|
|
|
if not chunk or chunk == "" then
|
2003-03-20 01:24:44 +01:00
|
|
|
io.write("\n")
|
|
|
|
aux.file:close()
|
2001-06-06 22:59:36 +02:00
|
|
|
return
|
|
|
|
end
|
2003-03-20 01:24:44 +01:00
|
|
|
aux.file:write(chunk)
|
2003-03-28 22:08:50 +01:00
|
|
|
aux.got = aux.got + string.len(chunk) -- total bytes received
|
|
|
|
if dt < 0.1 then return 1 end -- not enough time for estimate
|
2003-03-20 01:24:44 +01:00
|
|
|
io.write("\r", gauge(aux.got, dt, aux.size))
|
2001-06-06 22:59:36 +02:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
return receive_cb
|
|
|
|
end
|
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- downloads a file using the ftp protocol
|
|
|
|
function getbyftp(url, file)
|
2003-03-20 01:24:44 +01:00
|
|
|
local err = socket.ftp.get_cb {
|
2001-09-27 22:02:34 +02:00
|
|
|
url = url,
|
|
|
|
content_cb = receive2disk(file),
|
|
|
|
type = "i"
|
|
|
|
}
|
|
|
|
print()
|
|
|
|
if err then print(err) end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- downloads a file using the http protocol
|
|
|
|
function getbyhttp(url, file, size)
|
2003-03-20 01:24:44 +01:00
|
|
|
local response = socket.http.request_cb(
|
2001-09-27 22:02:34 +02:00
|
|
|
{url = url},
|
|
|
|
{body_cb = receive2disk(file, size)}
|
|
|
|
)
|
|
|
|
print()
|
|
|
|
if response.code ~= 200 then print(response.status or response.error) end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- determines the size of a http file
|
|
|
|
function gethttpsize(url)
|
2003-03-20 01:24:44 +01:00
|
|
|
local response = socket.http.request {
|
2001-09-27 22:02:34 +02:00
|
|
|
method = "HEAD",
|
|
|
|
url = url
|
|
|
|
}
|
|
|
|
if response.code == 200 then
|
|
|
|
return tonumber(response.headers["content-length"])
|
|
|
|
end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- determines the scheme and the file name of a given url
|
|
|
|
function getschemeandname(url, name)
|
|
|
|
-- this is an heuristic to solve a common invalid url poblem
|
2003-03-20 01:24:44 +01:00
|
|
|
if not string.find(url, "//") then url = "//" .. url end
|
|
|
|
local parsed = socket.url.parse(url, {scheme = "http"})
|
2001-09-27 22:02:34 +02:00
|
|
|
if name then return parsed.scheme, name end
|
2003-03-20 01:24:44 +01:00
|
|
|
local segment = socket.url.parse_path(parsed.path)
|
|
|
|
name = segment[table.getn(segment)]
|
2001-09-27 22:02:34 +02:00
|
|
|
if segment.is_directory then name = nil end
|
|
|
|
return parsed.scheme, name
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2003-03-28 22:08:50 +01:00
|
|
|
-- gets a file either by http or ftp, saving as <name>
|
2001-09-27 22:02:34 +02:00
|
|
|
function get(url, name)
|
|
|
|
local scheme
|
|
|
|
scheme, name = getschemeandname(url, name)
|
|
|
|
if not name then print("unknown file name")
|
|
|
|
elseif scheme == "ftp" then getbyftp(url, name)
|
|
|
|
elseif scheme == "http" then getbyhttp(url, name, gethttpsize(url))
|
|
|
|
else print("unknown scheme" .. scheme) end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- main program
|
2001-06-06 22:59:36 +02:00
|
|
|
arg = arg or {}
|
2003-03-20 01:24:44 +01:00
|
|
|
if table.getn(arg) < 1 then
|
|
|
|
io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n")
|
|
|
|
os.exit(1)
|
2001-06-06 22:59:36 +02:00
|
|
|
else get(arg[1], arg[2]) end
|