Skip to content

Instantly share code, notes, and snippets.

@drunkensouljah
Last active December 15, 2015 08:49
Show Gist options
  • Select an option

  • Save drunkensouljah/5233744 to your computer and use it in GitHub Desktop.

Select an option

Save drunkensouljah/5233744 to your computer and use it in GitHub Desktop.
wGet für Computercraft. Original Code von Epsen und Matthew DiBernardo
--[[
wget
A program adapted from HttpTest, by Epsen:
http://www.computercraft.info/forums2/index.php?/topic/82-121-httptest-v13/page__hl__http__fromsearch__1
Credit for the original work goes to him. Additional modifications have been
made to the file to make it suitable for my own purposes.
Matthew DiBernardo [04.14.2012]
--]]
--[[ Setting up variables ]]
local fileWriteOK
local url
local filename
local tArgs = { ... }
local function getHttpBody( url )
http.request( url )
while true do
local event, url, hBody = os.pullEvent()
if event == "http_success" then
print( "DOWNLOAD ERFOLGREICH\nURL = "..url )
return hBody
else
error( "HTTP FEHLER\nURL = "..url ) -- If the error is not catched, this will exit the program.
return nil -- In case this function is called via pcall.
end
end
end
local function processHttpBody( hBody, filename )
local hFile
if hBody then
local body = hBody.readAll() -- Read the whole body.
hFile = io.open( filename, "w" ) -- Open the provided filename for writing.
hFile:write( body ) -- Write the body to the file.
hFile:close() -- Save the changes and close the file.
else
print( "Sorry, kann nicht verarbeitet werden." )
end
hBody.close() -- Do not know for sure if this is really necessary, but just in case.
end
local function checkOverwrite( filename )
term.setCursorBlink( false )
print("\nWARNUNG: Datei existiert bereits. \195\156berschreiben? (J/N)")
while true do
event, choice = os.pullEvent( "char" ) -- Only listen for "char" events.
if string.lower( choice ) == "j" then return true end
if string.lower( choice ) == "n" then return false end
end
end
local function printUsage()
print("wget <url> [dateiname]")
end
--[[ ===== Execution Entry ===== ]]
--[[ Processing arguments ]]
if tArgs[1] then url = tArgs[1] end
if tArgs[2] then filename = tArgs[2] end
--[[ Making sure all necessary arguments were provided by the user ]]
if not url then
printUsage()
elseif not filename then
b = getHttpBody(url)
print(b.readAll())
b.close()
elseif not fs.exists( filename ) or checkOverwrite( filename ) then
processHttpBody( getHttpBody( url ), filename ) --If getHttpBody successfully returns a body, we continue with processing the body and saving it to a file.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment