Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 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 response = http.get( sUrl )
  22.     if not response then
  23.         print( "Failed." )
  24.         return nil
  25.     end
  26.  
  27.     print( "Success." )
  28.  
  29.     local sResponse = response.readAll()
  30.     response.close()
  31.     return sResponse
  32. end
  33.  
  34. -- Determine file to download
  35. local sUrl = tArgs[1]
  36. local sFile = tArgs[2]
  37. local force = false
  38.  
  39. if string.sub(sFile, 1, 1) == "!" then
  40.     force = true
  41.     sFile = string.sub(sFile, 2)
  42. end
  43.  
  44. local sPath = shell.resolve( sFile )
  45. if fs.exists( sPath ) then
  46.     if force then
  47.         print("Warning: overwriting existing file")
  48.     else
  49.         print( "File already exists" )
  50.         return
  51.     end
  52. end
  53.  
  54. -- Do the get
  55. local res = get( sUrl )
  56. if res then
  57.     local file = fs.open( sPath, "w" )
  58.     file.write( res )
  59.     file.close()
  60.  
  61.     print( "Downloaded as "..sFile )
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement