diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
| commit | 3a8ba90dfb0c2eb224f317dd692ede426691e72a (patch) | |
| tree | fe1cc5379a2e0e031663fe9c15d908653844bc73 /etc/get.lua | |
| parent | bce60be30fe8e9c1b0eb33128c23c93d7bca5303 (diff) | |
| download | luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.gz luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.bz2 luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.zip | |
Saving before big changes to support IPv6.
Diffstat (limited to 'etc/get.lua')
| -rw-r--r-- | etc/get.lua | 106 |
1 files changed, 53 insertions, 53 deletions
diff --git a/etc/get.lua b/etc/get.lua index 09f639d..0d631c2 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
| @@ -12,53 +12,53 @@ local ltn12 = require("ltn12") | |||
| 12 | 12 | ||
| 13 | -- formats a number of seconds into human readable form | 13 | -- formats a number of seconds into human readable form |
| 14 | function nicetime(s) | 14 | function nicetime(s) |
| 15 | local l = "s" | 15 | local l = "s" |
| 16 | if s > 60 then | 16 | if s > 60 then |
| 17 | s = s / 60 | 17 | s = s / 60 |
| 18 | l = "m" | 18 | l = "m" |
| 19 | if s > 60 then | 19 | if s > 60 then |
| 20 | s = s / 60 | 20 | s = s / 60 |
| 21 | l = "h" | 21 | l = "h" |
| 22 | if s > 24 then | 22 | if s > 24 then |
| 23 | s = s / 24 | 23 | s = s / 24 |
| 24 | l = "d" -- hmmm | 24 | l = "d" -- hmmm |
| 25 | end | 25 | end |
| 26 | end | 26 | end |
| 27 | end | 27 | end |
| 28 | if l == "s" then return string.format("%5.0f%s", s, l) | 28 | if l == "s" then return string.format("%5.0f%s", s, l) |
| 29 | else return string.format("%5.2f%s", s, l) end | 29 | else return string.format("%5.2f%s", s, l) end |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | -- formats a number of bytes into human readable form | 32 | -- formats a number of bytes into human readable form |
| 33 | function nicesize(b) | 33 | function nicesize(b) |
| 34 | local l = "B" | 34 | local l = "B" |
| 35 | if b > 1024 then | 35 | if b > 1024 then |
| 36 | b = b / 1024 | 36 | b = b / 1024 |
| 37 | l = "KB" | 37 | l = "KB" |
| 38 | if b > 1024 then | 38 | if b > 1024 then |
| 39 | b = b / 1024 | 39 | b = b / 1024 |
| 40 | l = "MB" | 40 | l = "MB" |
| 41 | if b > 1024 then | 41 | if b > 1024 then |
| 42 | b = b / 1024 | 42 | b = b / 1024 |
| 43 | l = "GB" -- hmmm | 43 | l = "GB" -- hmmm |
| 44 | end | 44 | end |
| 45 | end | 45 | end |
| 46 | end | 46 | end |
| 47 | return string.format("%7.2f%2s", b, l) | 47 | return string.format("%7.2f%2s", b, l) |
| 48 | end | 48 | end |
| 49 | 49 | ||
| 50 | -- returns a string with the current state of the download | 50 | -- returns a string with the current state of the download |
| 51 | local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining" | 51 | local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining" |
| 52 | local elapsed_s = "%s received, %s/s throughput, %s elapsed " | 52 | local elapsed_s = "%s received, %s/s throughput, %s elapsed " |
| 53 | function gauge(got, delta, size) | 53 | function gauge(got, delta, size) |
| 54 | local rate = got / delta | 54 | local rate = got / delta |
| 55 | if size and size >= 1 then | 55 | if size and size >= 1 then |
| 56 | return string.format(remaining_s, nicesize(got), nicesize(rate), | 56 | return string.format(remaining_s, nicesize(got), nicesize(rate), |
| 57 | 100*got/size, nicetime((size-got)/rate)) | 57 | 100*got/size, nicetime((size-got)/rate)) |
| 58 | else | 58 | else |
| 59 | return string.format(elapsed_s, nicesize(got), | 59 | return string.format(elapsed_s, nicesize(got), |
| 60 | nicesize(rate), nicetime(delta)) | 60 | nicesize(rate), nicetime(delta)) |
| 61 | end | 61 | end |
| 62 | end | 62 | end |
| 63 | 63 | ||
| 64 | -- creates a new instance of a receive_cb that saves to disk | 64 | -- creates a new instance of a receive_cb that saves to disk |
| @@ -89,10 +89,10 @@ end | |||
| 89 | 89 | ||
| 90 | -- determines the size of a http file | 90 | -- determines the size of a http file |
| 91 | function gethttpsize(u) | 91 | function gethttpsize(u) |
| 92 | local r, c, h = http.request {method = "HEAD", url = u} | 92 | local r, c, h = http.request {method = "HEAD", url = u} |
| 93 | if c == 200 then | 93 | if c == 200 then |
| 94 | return tonumber(h["content-length"]) | 94 | return tonumber(h["content-length"]) |
| 95 | end | 95 | end |
| 96 | end | 96 | end |
| 97 | 97 | ||
| 98 | -- downloads a file using the http protocol | 98 | -- downloads a file using the http protocol |
| @@ -101,7 +101,7 @@ function getbyhttp(u, file) | |||
| 101 | -- only print feedback if output is not stdout | 101 | -- only print feedback if output is not stdout |
| 102 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end | 102 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end |
| 103 | local r, c, h, s = http.request {url = u, sink = save } | 103 | local r, c, h, s = http.request {url = u, sink = save } |
| 104 | if c ~= 200 then io.stderr:write(s or c, "\n") end | 104 | if c ~= 200 then io.stderr:write(s or c, "\n") end |
| 105 | end | 105 | end |
| 106 | 106 | ||
| 107 | -- downloads a file using the ftp protocol | 107 | -- downloads a file using the ftp protocol |
| @@ -114,29 +114,29 @@ function getbyftp(u, file) | |||
| 114 | gett.sink = save | 114 | gett.sink = save |
| 115 | gett.type = "i" | 115 | gett.type = "i" |
| 116 | local ret, err = ftp.get(gett) | 116 | local ret, err = ftp.get(gett) |
| 117 | if err then print(err) end | 117 | if err then print(err) end |
| 118 | end | 118 | end |
| 119 | 119 | ||
| 120 | -- determines the scheme | 120 | -- determines the scheme |
| 121 | function getscheme(u) | 121 | function getscheme(u) |
| 122 | -- this is an heuristic to solve a common invalid url poblem | 122 | -- this is an heuristic to solve a common invalid url poblem |
| 123 | if not string.find(u, "//") then u = "//" .. u end | 123 | if not string.find(u, "//") then u = "//" .. u end |
| 124 | local parsed = url.parse(u, {scheme = "http"}) | 124 | local parsed = url.parse(u, {scheme = "http"}) |
| 125 | return parsed.scheme | 125 | return parsed.scheme |
| 126 | end | 126 | end |
| 127 | 127 | ||
| 128 | -- gets a file either by http or ftp, saving as <name> | 128 | -- gets a file either by http or ftp, saving as <name> |
| 129 | function get(u, name) | 129 | function get(u, name) |
| 130 | local fout = name and io.open(name, "wb") | 130 | local fout = name and io.open(name, "wb") |
| 131 | local scheme = getscheme(u) | 131 | local scheme = getscheme(u) |
| 132 | if scheme == "ftp" then getbyftp(u, fout) | 132 | if scheme == "ftp" then getbyftp(u, fout) |
| 133 | elseif scheme == "http" then getbyhttp(u, fout) | 133 | elseif scheme == "http" then getbyhttp(u, fout) |
| 134 | else print("unknown scheme" .. scheme) end | 134 | else print("unknown scheme" .. scheme) end |
| 135 | end | 135 | end |
| 136 | 136 | ||
| 137 | -- main program | 137 | -- main program |
| 138 | arg = arg or {} | 138 | arg = arg or {} |
| 139 | if table.getn(arg) < 1 then | 139 | if table.getn(arg) < 1 then |
| 140 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") | 140 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") |
| 141 | os.exit(1) | 141 | os.exit(1) |
| 142 | else get(arg[1], arg[2]) end | 142 | else get(arg[1], arg[2]) end |
