diff options
Diffstat (limited to 'etc/get.lua')
-rw-r--r-- | etc/get.lua | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/etc/get.lua b/etc/get.lua index 35de7d7..c1e0542 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -4,9 +4,10 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require"http" | 7 | socket = require("socket") |
8 | require"ftp" | 8 | http = require("http") |
9 | require"url" | 9 | ftp = require("ftp") |
10 | url = require("url") | ||
10 | 11 | ||
11 | -- formats a number of seconds into human readable form | 12 | -- formats a number of seconds into human readable form |
12 | function nicetime(s) | 13 | function nicetime(s) |
@@ -84,55 +85,55 @@ function stats(size) | |||
84 | end | 85 | end |
85 | 86 | ||
86 | -- determines the size of a http file | 87 | -- determines the size of a http file |
87 | function gethttpsize(url) | 88 | function gethttpsize(u) |
88 | local respt = socket.http.request {method = "HEAD", url = url} | 89 | local respt = http.request {method = "HEAD", url = u} |
89 | if respt.code == 200 then | 90 | if respt.code == 200 then |
90 | return tonumber(respt.headers["content-length"]) | 91 | return tonumber(respt.headers["content-length"]) |
91 | end | 92 | end |
92 | end | 93 | end |
93 | 94 | ||
94 | -- downloads a file using the http protocol | 95 | -- downloads a file using the http protocol |
95 | function getbyhttp(url, file) | 96 | function getbyhttp(u, file) |
96 | local save = ltn12.sink.file(file or io.stdout) | 97 | local save = ltn12.sink.file(file or io.stdout) |
97 | -- only print feedback if output is not stdout | 98 | -- only print feedback if output is not stdout |
98 | if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end | 99 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end |
99 | local respt = socket.http.request {url = url, sink = save } | 100 | local respt = http.request {url = u, sink = save } |
100 | if respt.code ~= 200 then print(respt.status or respt.error) end | 101 | if respt.code ~= 200 then print(respt.status or respt.error) end |
101 | end | 102 | end |
102 | 103 | ||
103 | -- downloads a file using the ftp protocol | 104 | -- downloads a file using the ftp protocol |
104 | function getbyftp(url, file) | 105 | function getbyftp(u, file) |
105 | local save = ltn12.sink.file(file or io.stdout) | 106 | local save = ltn12.sink.file(file or io.stdout) |
106 | -- only print feedback if output is not stdout | 107 | -- only print feedback if output is not stdout |
107 | -- and we don't know how big the file is | 108 | -- and we don't know how big the file is |
108 | if file then save = ltn12.sink.chain(stats(), save) end | 109 | if file then save = ltn12.sink.chain(stats(), save) end |
109 | local gett = socket.url.parse(url) | 110 | local gett = url.parse(u) |
110 | gett.sink = save | 111 | gett.sink = save |
111 | gett.type = "i" | 112 | gett.type = "i" |
112 | local ret, err = socket.ftp.get(gett) | 113 | local ret, err = ftp.get(gett) |
113 | if err then print(err) end | 114 | if err then print(err) end |
114 | end | 115 | end |
115 | 116 | ||
116 | -- determines the scheme | 117 | -- determines the scheme |
117 | function getscheme(url) | 118 | function getscheme(u) |
118 | -- this is an heuristic to solve a common invalid url poblem | 119 | -- this is an heuristic to solve a common invalid url poblem |
119 | if not string.find(url, "//") then url = "//" .. url end | 120 | if not string.find(u, "//") then u = "//" .. u end |
120 | local parsed = socket.url.parse(url, {scheme = "http"}) | 121 | local parsed = url.parse(u, {scheme = "http"}) |
121 | return parsed.scheme | 122 | return parsed.scheme |
122 | end | 123 | end |
123 | 124 | ||
124 | -- gets a file either by http or ftp, saving as <name> | 125 | -- gets a file either by http or ftp, saving as <name> |
125 | function get(url, name) | 126 | function get(u, name) |
126 | local fout = name and io.open(name, "wb") | 127 | local fout = name and io.open(name, "wb") |
127 | local scheme = getscheme(url) | 128 | local scheme = getscheme(u) |
128 | if scheme == "ftp" then getbyftp(url, fout) | 129 | if scheme == "ftp" then getbyftp(u, fout) |
129 | elseif scheme == "http" then getbyhttp(url, fout) | 130 | elseif scheme == "http" then getbyhttp(u, fout) |
130 | else print("unknown scheme" .. scheme) end | 131 | else print("unknown scheme" .. scheme) end |
131 | end | 132 | end |
132 | 133 | ||
133 | -- main program | 134 | -- main program |
134 | arg = arg or {} | 135 | arg = arg or {} |
135 | if table.getn(arg) < 1 then | 136 | if table.getn(arg) < 1 then |
136 | io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n") | 137 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") |
137 | os.exit(1) | 138 | os.exit(1) |
138 | else get(arg[1], arg[2]) end | 139 | else get(arg[1], arg[2]) end |