2017-05-01 13:32:39 +00:00
|
|
|
local function printUsage()
|
2020-11-04 14:03:08 +00:00
|
|
|
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
2020-04-18 09:09:40 +00:00
|
|
|
print("Usage:")
|
2020-11-04 14:03:08 +00:00
|
|
|
print(programName .. " <url> [filename]")
|
|
|
|
print(programName .. " run <url>")
|
2017-05-01 13:32:39 +00:00
|
|
|
end
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
local tArgs = { ... }
|
2019-06-08 13:49:42 +00:00
|
|
|
|
|
|
|
local run = false
|
|
|
|
if tArgs[1] == "run" then
|
2020-04-18 09:09:40 +00:00
|
|
|
table.remove(tArgs, 1)
|
2019-06-08 13:49:42 +00:00
|
|
|
run = true
|
|
|
|
end
|
|
|
|
|
2018-04-06 20:08:54 +00:00
|
|
|
if #tArgs < 1 then
|
2017-05-01 13:32:39 +00:00
|
|
|
printUsage()
|
|
|
|
return
|
|
|
|
end
|
2018-12-17 17:22:15 +00:00
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
local url = table.remove(tArgs, 1)
|
2019-06-08 13:49:42 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
if not http then
|
2020-04-18 09:09:40 +00:00
|
|
|
printError("wget requires the http API")
|
|
|
|
printError("Set http.enabled to true in CC: Tweaked's config")
|
2017-05-01 13:32:39 +00:00
|
|
|
return
|
|
|
|
end
|
2018-04-06 20:08:54 +00:00
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
local function getFilename(sUrl)
|
|
|
|
sUrl = sUrl:gsub("[#?].*" , ""):gsub("/+$" , "")
|
|
|
|
return sUrl:match("/([^/]+)$")
|
2018-04-06 20:08:54 +00:00
|
|
|
end
|
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
local function get(sUrl)
|
2019-06-08 13:49:42 +00:00
|
|
|
-- Check if the URL is valid
|
2020-04-18 09:09:40 +00:00
|
|
|
local ok, err = http.checkURL(url)
|
2019-06-08 13:49:42 +00:00
|
|
|
if not ok then
|
2020-04-18 09:09:40 +00:00
|
|
|
printError(err or "Invalid URL.")
|
2019-06-08 13:49:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
write("Connecting to " .. sUrl .. "... ")
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
local response = http.get(sUrl , nil , true)
|
2017-05-01 13:32:39 +00:00
|
|
|
if not response then
|
2020-04-18 09:09:40 +00:00
|
|
|
print("Failed.")
|
2017-05-01 13:32:39 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
print("Success.")
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
local sResponse = response.readAll()
|
|
|
|
response.close()
|
2021-06-19 10:59:15 +00:00
|
|
|
return sResponse or ""
|
2017-05-01 13:32:39 +00:00
|
|
|
end
|
2018-04-06 20:08:54 +00:00
|
|
|
|
2019-06-08 13:49:42 +00:00
|
|
|
if run then
|
|
|
|
local res = get(url)
|
|
|
|
if not res then return end
|
2018-04-06 20:08:54 +00:00
|
|
|
|
2019-06-08 13:49:42 +00:00
|
|
|
local func, err = load(res, getFilename(url), "t", _ENV)
|
|
|
|
if not func then
|
|
|
|
printError(err)
|
|
|
|
return
|
|
|
|
end
|
2018-04-06 20:08:54 +00:00
|
|
|
|
2019-06-08 13:49:42 +00:00
|
|
|
local ok, err = pcall(func, table.unpack(tArgs))
|
|
|
|
if not ok then
|
2020-04-18 09:09:40 +00:00
|
|
|
printError(err)
|
2019-06-08 13:49:42 +00:00
|
|
|
end
|
|
|
|
else
|
2021-07-28 15:00:17 +00:00
|
|
|
local sFile = tArgs[1] or getFilename(url) or url
|
2020-04-18 09:09:40 +00:00
|
|
|
local sPath = shell.resolve(sFile)
|
|
|
|
if fs.exists(sPath) then
|
|
|
|
print("File already exists")
|
2019-06-08 13:49:42 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local res = get(url)
|
|
|
|
if not res then return end
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2021-06-19 10:59:15 +00:00
|
|
|
local file, err = fs.open(sPath, "wb")
|
|
|
|
if not file then
|
|
|
|
printError("Cannot save file: " .. err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
file.write(res)
|
2017-05-01 13:32:39 +00:00
|
|
|
file.close()
|
|
|
|
|
2020-04-18 09:09:40 +00:00
|
|
|
print("Downloaded as " .. sFile)
|
2017-05-01 13:32:39 +00:00
|
|
|
end
|