Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function printUsage()
- print( "Usage:" )
- print( "wget <url> <filename>" )
- end
- local tArgs = { ... }
- if #tArgs < 2 then
- printUsage()
- return
- end
- if not http then
- printError( "wget requires http API" )
- printError( "Set http_enable to true in ComputerCraft.cfg" )
- return
- end
- local function get( sUrl )
- write( "Connecting to " .. sUrl .. "... " )
- local response = http.get( sUrl )
- if not response then
- print( "Failed." )
- return nil
- end
- print( "Success." )
- local sResponse = response.readAll()
- response.close()
- return sResponse
- end
- -- Determine file to download
- local sUrl = tArgs[1]
- local sFile = tArgs[2]
- local force = false
- if string.sub(sFile, 1, 1) == "!" then
- force = true
- sFile = string.sub(sFile, 2)
- end
- local sPath = shell.resolve( sFile )
- if fs.exists( sPath ) then
- if force then
- print("Warning: overwriting existing file")
- else
- print( "File already exists" )
- return
- end
- end
- -- Do the get
- local res = get( sUrl )
- if res then
- local file = fs.open( sPath, "w" )
- file.write( res )
- file.close()
- print( "Downloaded as "..sFile )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement