diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
| commit | 53857360bb1ca9cd2080b69d930763ae59db9b06 (patch) | |
| tree | 6c1bc6d6462695cf9048801b2244f7fd0cd21ad5 /etc/get.lua | |
| parent | 7da19138e37c4e0123860f1fecbceb80c3d2627d (diff) | |
| download | luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.gz luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.bz2 luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.zip | |
Finish port to Lua 5. Everything is working fine.
Still doesn't work in Windows.
Diffstat (limited to 'etc/get.lua')
| -rw-r--r-- | etc/get.lua | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/etc/get.lua b/etc/get.lua index ecfecd1..33da653 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
| @@ -13,8 +13,8 @@ function nicetime(s) | |||
| 13 | end | 13 | end |
| 14 | end | 14 | end |
| 15 | end | 15 | end |
| 16 | if l == "s" then return format("%2.0f%s", s, l) | 16 | if l == "s" then return string.format("%2.0f%s", s, l) |
| 17 | else return format("%5.2f%s", s, l) end | 17 | else return string.format("%5.2f%s", s, l) end |
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | -- formats a number of bytes into human readable form | 20 | -- formats a number of bytes into human readable form |
| @@ -32,21 +32,21 @@ function nicesize(b) | |||
| 32 | end | 32 | end |
| 33 | end | 33 | end |
| 34 | end | 34 | end |
| 35 | return format("%7.2f%2s", b, l) | 35 | return string.format("%7.2f%2s", b, l) |
| 36 | end | 36 | end |
| 37 | 37 | ||
| 38 | -- returns a string with the current state of the download | 38 | -- returns a string with the current state of the download |
| 39 | function gauge(got, dt, size) | 39 | function gauge(got, dt, size) |
| 40 | local rate = got / dt | 40 | local rate = got / dt |
| 41 | if size and size >= 1 then | 41 | if size and size >= 1 then |
| 42 | return format("%s received, %s/s throughput, " .. | 42 | return string.format("%s received, %s/s throughput, " .. |
| 43 | "%.0f%% done, %s remaining", | 43 | "%.0f%% done, %s remaining", |
| 44 | nicesize(got), | 44 | nicesize(got), |
| 45 | nicesize(rate), | 45 | nicesize(rate), |
| 46 | 100*got/size, | 46 | 100*got/size, |
| 47 | nicetime((size-got)/rate)) | 47 | nicetime((size-got)/rate)) |
| 48 | else | 48 | else |
| 49 | return format("%s received, %s/s throughput, %s elapsed", | 49 | return string.format("%s received, %s/s throughput, %s elapsed", |
| 50 | nicesize(got), | 50 | nicesize(got), |
| 51 | nicesize(rate), | 51 | nicesize(rate), |
| 52 | nicetime(dt)) | 52 | nicetime(dt)) |
| @@ -57,22 +57,22 @@ end | |||
| 57 | -- kind of copied from luasocket's manual callback examples | 57 | -- kind of copied from luasocket's manual callback examples |
| 58 | function receive2disk(file, size) | 58 | function receive2disk(file, size) |
| 59 | local aux = { | 59 | local aux = { |
| 60 | start = _time(), | 60 | start = socket._time(), |
| 61 | got = 0, | 61 | got = 0, |
| 62 | file = openfile(file, "wb"), | 62 | file = io.open(file, "wb"), |
| 63 | size = size | 63 | size = size |
| 64 | } | 64 | } |
| 65 | local receive_cb = function(chunk, err) | 65 | local receive_cb = function(chunk, err) |
| 66 | local dt = _time() - %aux.start -- elapsed time since start | 66 | local dt = socket._time() - %aux.start -- elapsed time since start |
| 67 | if not chunk or chunk == "" then | 67 | if not chunk or chunk == "" then |
| 68 | write("\n") | 68 | io.write("\n") |
| 69 | closefile(%aux.file) | 69 | aux.file:close() |
| 70 | return | 70 | return |
| 71 | end | 71 | end |
| 72 | write(%aux.file, chunk) | 72 | aux.file:write(chunk) |
| 73 | %aux.got = %aux.got + strlen(chunk) -- total bytes received | 73 | aux.got = aux.got + string.len(chunk) -- total bytes received |
| 74 | if dt < 0.1 then return 1 end -- not enough time for estimate | 74 | if dt < 0.1 then return 1 end -- not enough time for estimate |
| 75 | write("\r", gauge(%aux.got, dt, %aux.size)) | 75 | io.write("\r", gauge(aux.got, dt, aux.size)) |
| 76 | return 1 | 76 | return 1 |
| 77 | end | 77 | end |
| 78 | return receive_cb | 78 | return receive_cb |
| @@ -80,7 +80,7 @@ end | |||
| 80 | 80 | ||
| 81 | -- downloads a file using the ftp protocol | 81 | -- downloads a file using the ftp protocol |
| 82 | function getbyftp(url, file) | 82 | function getbyftp(url, file) |
| 83 | local err = FTP.get_cb { | 83 | local err = socket.ftp.get_cb { |
| 84 | url = url, | 84 | url = url, |
| 85 | content_cb = receive2disk(file), | 85 | content_cb = receive2disk(file), |
| 86 | type = "i" | 86 | type = "i" |
| @@ -91,7 +91,7 @@ end | |||
| 91 | 91 | ||
| 92 | -- downloads a file using the http protocol | 92 | -- downloads a file using the http protocol |
| 93 | function getbyhttp(url, file, size) | 93 | function getbyhttp(url, file, size) |
| 94 | local response = HTTP.request_cb( | 94 | local response = socket.http.request_cb( |
| 95 | {url = url}, | 95 | {url = url}, |
| 96 | {body_cb = receive2disk(file, size)} | 96 | {body_cb = receive2disk(file, size)} |
| 97 | ) | 97 | ) |
| @@ -101,7 +101,7 @@ end | |||
| 101 | 101 | ||
| 102 | -- determines the size of a http file | 102 | -- determines the size of a http file |
| 103 | function gethttpsize(url) | 103 | function gethttpsize(url) |
| 104 | local response = HTTP.request { | 104 | local response = socket.http.request { |
| 105 | method = "HEAD", | 105 | method = "HEAD", |
| 106 | url = url | 106 | url = url |
| 107 | } | 107 | } |
| @@ -113,11 +113,11 @@ end | |||
| 113 | -- determines the scheme and the file name of a given url | 113 | -- determines the scheme and the file name of a given url |
| 114 | function getschemeandname(url, name) | 114 | function getschemeandname(url, name) |
| 115 | -- this is an heuristic to solve a common invalid url poblem | 115 | -- this is an heuristic to solve a common invalid url poblem |
| 116 | if not strfind(url, "//") then url = "//" .. url end | 116 | if not string.find(url, "//") then url = "//" .. url end |
| 117 | local parsed = URL.parse_url(url, {scheme = "http"}) | 117 | local parsed = socket.url.parse(url, {scheme = "http"}) |
| 118 | if name then return parsed.scheme, name end | 118 | if name then return parsed.scheme, name end |
| 119 | local segment = URL.parse_path(parsed.path) | 119 | local segment = socket.url.parse_path(parsed.path) |
| 120 | name = segment[getn(segment)] | 120 | name = segment[table.getn(segment)] |
| 121 | if segment.is_directory then name = nil end | 121 | if segment.is_directory then name = nil end |
| 122 | return parsed.scheme, name | 122 | return parsed.scheme, name |
| 123 | end | 123 | end |
| @@ -134,7 +134,7 @@ end | |||
| 134 | 134 | ||
| 135 | -- main program | 135 | -- main program |
| 136 | arg = arg or {} | 136 | arg = arg or {} |
| 137 | if getn(arg) < 1 then | 137 | if table.getn(arg) < 1 then |
| 138 | write("Usage:\n luasocket -f get.lua <remote-url> [<local-file>]\n") | 138 | io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n") |
| 139 | exit(1) | 139 | os.exit(1) |
| 140 | else get(arg[1], arg[2]) end | 140 | else get(arg[1], arg[2]) end |
