Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
wget is a program for downloading files from the internet. This is useful for downloading programs created by other players.
If no filename is specified wget will try to determine the filename from the URL by stripping any anchors, parameters and trailing slashes and then taking everything remaining after the last slash.
The HTTP API must be enabled in ComputerCraft.cfg to use this program.

ex:
"wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test".
"wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua"
"wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org"
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

local function printUsage()
print( "Usage:" )
print( "wget <url> <filename>" )
print( "wget <url> [filename]" )
end

local tArgs = { ... }
if #tArgs < 2 then
if #tArgs < 1 then
printUsage()
return
end
Expand All @@ -15,19 +15,15 @@ if not http then
printError( "Set http_enable to true in ComputerCraft.cfg" )
return
end


local function getFilename( sUrl )
sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" )
return sUrl:match( "/([^/]+)$" )
end

local function get( sUrl )
write( "Connecting to " .. sUrl .. "... " )

local ok, err = http.checkURL( sUrl )
if not ok then
print( "Failed." )
if err then
printError( err )
end
return nil
end

local response = http.get( sUrl , nil , true )
if not response then
print( "Failed." )
Expand All @@ -40,10 +36,18 @@ local function get( sUrl )
response.close()
return sResponse
end

-- Determine file to download
local sUrl = tArgs[1]
local sFile = tArgs[2]

--Check if the URL is valid
local ok, err = http.checkURL( sUrl )
if not ok then
printError( err or "Invalid URL." )
return
end

local sFile = tArgs[2] or getFilename( sUrl )
local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then
print( "File already exists" )
Expand Down