2003-03-28 22:08:50 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Little program to download files from URLs
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket sample files
|
2003-06-09 20:23:40 +02:00
|
|
|
-- Author: Diego Nehab
|
2003-03-28 22:08:50 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-06-21 00:19:54 +02:00
|
|
|
local socket = require("socket")
|
2004-10-11 08:18:57 +02:00
|
|
|
local http = require("socket.http")
|
|
|
|
local ftp = require("socket.ftp")
|
2005-09-30 00:26:35 +02:00
|
|
|
local url = require("socket.url")
|
2004-06-21 00:19:54 +02:00
|
|
|
local ltn12 = require("ltn12")
|
2004-05-28 09:47:41 +02:00
|
|
|
|
2001-09-27 22:02:34 +02:00
|
|
|
-- formats a number of seconds into human readable form
|
|
|
|
function nicetime(s)
|
2011-05-25 22:57:22 +02:00
|
|
|
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
|
|
|
|
if l == "s" then return string.format("%5.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)
|
2011-05-25 22:57:22 +02:00
|
|
|
local l = "B"
|
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
|
|
|
l = "KB"
|
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
|
|
|
l = "MB"
|
|
|
|
if b > 1024 then
|
|
|
|
b = b / 1024
|
|
|
|
l = "GB" -- hmmm
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2004-01-21 02:09:50 +01:00
|
|
|
local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining"
|
|
|
|
local elapsed_s = "%s received, %s/s throughput, %s elapsed "
|
2004-01-19 06:41:30 +01:00
|
|
|
function gauge(got, delta, size)
|
2011-05-25 22:57:22 +02:00
|
|
|
local rate = got / delta
|
|
|
|
if size and size >= 1 then
|
|
|
|
return string.format(remaining_s, nicesize(got), nicesize(rate),
|
|
|
|
100*got/size, nicetime((size-got)/rate))
|
|
|
|
else
|
|
|
|
return string.format(elapsed_s, nicesize(got),
|
|
|
|
nicesize(rate), nicetime(delta))
|
|
|
|
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
|
2004-01-19 06:41:30 +01:00
|
|
|
function stats(size)
|
2004-06-21 00:19:54 +02:00
|
|
|
local start = socket.gettime()
|
2007-03-12 05:08:40 +01:00
|
|
|
local last = start
|
2004-01-19 06:41:30 +01:00
|
|
|
local got = 0
|
|
|
|
return function(chunk)
|
|
|
|
-- elapsed time since start
|
2007-03-12 05:08:40 +01:00
|
|
|
local current = socket.gettime()
|
2005-11-22 09:33:29 +01:00
|
|
|
if chunk then
|
2004-01-19 06:41:30 +01:00
|
|
|
-- total bytes received
|
2005-11-22 09:33:29 +01:00
|
|
|
got = got + string.len(chunk)
|
2004-01-19 06:41:30 +01:00
|
|
|
-- not enough time for estimate
|
2007-03-12 05:08:40 +01:00
|
|
|
if current - last > 1 then
|
|
|
|
io.stderr:write("\r", gauge(got, current - start, size))
|
2004-01-19 06:41:30 +01:00
|
|
|
io.stderr:flush()
|
2007-03-12 05:08:40 +01:00
|
|
|
last = current
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
else
|
2004-01-19 06:41:30 +01:00
|
|
|
-- close up
|
2007-03-12 05:08:40 +01:00
|
|
|
io.stderr:write("\r", gauge(got, current - start), "\n")
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
2004-03-26 01:18:41 +01:00
|
|
|
return chunk
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
-- determines the size of a http file
|
2004-06-04 17:15:45 +02:00
|
|
|
function gethttpsize(u)
|
2011-05-25 22:57:22 +02:00
|
|
|
local r, c, h = http.request {method = "HEAD", url = u}
|
|
|
|
if c == 200 then
|
|
|
|
return tonumber(h["content-length"])
|
|
|
|
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
|
2004-06-04 17:15:45 +02:00
|
|
|
function getbyhttp(u, file)
|
2004-03-16 07:42:53 +01:00
|
|
|
local save = ltn12.sink.file(file or io.stdout)
|
|
|
|
-- only print feedback if output is not stdout
|
2004-06-04 17:15:45 +02:00
|
|
|
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
|
2004-06-17 00:51:04 +02:00
|
|
|
local r, c, h, s = http.request {url = u, sink = save }
|
2011-05-25 22:57:22 +02:00
|
|
|
if c ~= 200 then io.stderr:write(s or c, "\n") end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
-- downloads a file using the ftp protocol
|
2004-06-04 17:15:45 +02:00
|
|
|
function getbyftp(u, file)
|
2004-03-16 07:42:53 +01:00
|
|
|
local save = ltn12.sink.file(file or io.stdout)
|
|
|
|
-- only print feedback if output is not stdout
|
|
|
|
-- and we don't know how big the file is
|
|
|
|
if file then save = ltn12.sink.chain(stats(), save) end
|
2004-06-04 17:15:45 +02:00
|
|
|
local gett = url.parse(u)
|
2004-05-30 23:36:22 +02:00
|
|
|
gett.sink = save
|
|
|
|
gett.type = "i"
|
2005-11-22 09:33:29 +01:00
|
|
|
local ret, err = ftp.get(gett)
|
2011-05-25 22:57:22 +02:00
|
|
|
if err then print(err) end
|
2001-06-06 22:59:36 +02:00
|
|
|
end
|
|
|
|
|
2005-11-22 09:33:29 +01:00
|
|
|
-- determines the scheme
|
2004-06-04 17:15:45 +02:00
|
|
|
function getscheme(u)
|
2011-05-25 22:57:22 +02:00
|
|
|
-- this is an heuristic to solve a common invalid url poblem
|
|
|
|
if not string.find(u, "//") then u = "//" .. u end
|
|
|
|
local parsed = url.parse(u, {scheme = "http"})
|
|
|
|
return parsed.scheme
|
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>
|
2004-06-04 17:15:45 +02:00
|
|
|
function get(u, name)
|
2004-01-19 06:41:30 +01:00
|
|
|
local fout = name and io.open(name, "wb")
|
2011-05-25 22:57:22 +02:00
|
|
|
local scheme = getscheme(u)
|
|
|
|
if scheme == "ftp" then getbyftp(u, fout)
|
|
|
|
elseif scheme == "http" then getbyhttp(u, fout)
|
|
|
|
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 {}
|
2005-11-22 09:33:29 +01:00
|
|
|
if table.getn(arg) < 1 then
|
2011-05-25 22:57:22 +02:00
|
|
|
io.write("Usage:\n lua 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
|