Commandcracker

wget

Jan 24th, 2021 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.18 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usage:" )
  3.     print( "wget <url> <filename>" )
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs < 2 then
  8.     printUsage()
  9.     return
  10. end
  11.  
  12. if not http then
  13.     printError( "wget requires http API" )
  14.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  15.     return
  16. end
  17.  
  18. local function get( sUrl )
  19.     write( "Connecting to " .. sUrl .. "... " )
  20.  
  21.     local ok, err = http.checkURL( sUrl )
  22.     if not ok then
  23.         print( "Failed." )
  24.         if err then
  25.             printError( err )
  26.         end
  27.         return nil
  28.     end
  29.  
  30.     local response = http.get( sUrl )
  31.     if not response then
  32.         print( "Failed." )
  33.         return nil
  34.     end
  35.  
  36.     print( "Success." )
  37.  
  38.     local sResponse = response.readAll()
  39.     response.close()
  40.     return sResponse
  41. end
  42.  
  43. -- Determine file to download
  44. local sUrl = tArgs[1]
  45. local sFile = tArgs[2]
  46. local sPath = shell.resolve( sFile )
  47. if fs.exists( sPath ) then
  48.     print( "File already exists" )
  49.     return
  50. end
  51.  
  52. -- Do the get
  53. local res = get( sUrl )
  54. if res then
  55.     local file = fs.open( sPath, "w" )
  56.     file.write( res )
  57.     file.close()
  58.  
  59.     print( "Downloaded as "..sFile )
  60. end
  61.  
Add Comment
Please, Sign In to add comment