Advertisement
cduclaux

Computercraft wget

May 7th, 2014
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. --[[
  2. Original pastebin is at http://pastebin.com/Kva1wLTe
  3. HttpTest by Espen v1.4
  4. Name changed to wget
  5. An example code to show how to use the ComputerCraft HTTP functions.
  6. Changes made by Arclight306 so it works like WGet
  7.  
  8. Thanks to Advert for making me aware of the redundant call to http.get after http.request!
  9. 1.4: Changed the else-condition in getHttpBody() to explicitly check for an http_failure, instead of just assuming one if the event isn't an http_success. *derp*
  10. --]]
  11.  
  12. --[[ Setting up variables ]]
  13. local fileWriteOK
  14. local url
  15. local filename
  16. local tArgs = { ... }
  17.  
  18.  
  19. local function getHttpBody( url )
  20.     http.request( url )
  21.    
  22.     while true do
  23.         local event, url, hBody = os.pullEvent()
  24.         if event == "http_success" then
  25.             print( "HTTP SUCCESS\nURL = "..url )
  26.             return hBody
  27.         elseif event == "http_failure" then
  28.             error( "HTTP FAILURE\nURL = "..url )    -- If the error is not catched, this will exit the program.
  29.             return nil    -- In case this function is called via pcall.
  30.         end
  31.     end
  32. end
  33.  
  34. local function processHttpBody( hBody, filename )
  35.     local hFile
  36.    
  37.     if hBody then
  38.             local body = hBody.readAll()    -- Read the whole body.
  39.             hFile = io.open( filename, "w" )    -- Open the provided filename for writing.
  40.             hFile:write( body )     -- Write the body to the file.
  41.             hFile:close()   -- Save the changes and close the file.
  42.     else
  43.         print( "Sorry, no body to process." )
  44.     end
  45.    
  46.     hBody.close()   -- Do not know for sure if this is really necessary, but just in case.
  47. end
  48.  
  49. local function checkOverwrite( filename )
  50.     term.setCursorBlink( false )
  51.     print("\nWarning: File already exists. Overwrite? (Y/N)")
  52.    
  53.     while true do
  54.             event, choice = os.pullEvent( "char" )  -- Only listen for "char" events.
  55.             if string.lower( choice ) == "y" then return true end
  56.             if string.lower( choice ) == "n" then return false end
  57.     end
  58. end
  59.  
  60. local function printUsage()
  61.     print("Usage:")
  62.     print("wget <URL> <OUTPUT-FILENAME>")
  63.     print("Example:")
  64.     print("wget http://www.google.com/ webout")
  65.     print("\nThe response body will then be output into the file 'webout', line for line.")
  66. end
  67.  
  68.  
  69. --[[ ===== Execution Entry ===== ]]
  70.  
  71. --[[ Processing arguments ]]
  72. if tArgs[1] then url = tArgs[1] end
  73. if tArgs[2] then filename = tArgs[2] end
  74.  
  75. --[[ Making sure all necessary arguments were provided by the user ]]
  76. if not url or not filename then
  77.     printUsage()
  78. elseif not fs.exists( filename ) or checkOverwrite( filename ) then
  79.     processHttpBody( getHttpBody( url ), filename )    --If getHttpBody successfully returns a body, we continue with processing the body and saving it to a file.
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement