Advertisement
Krutoy242

ComputerCraft wget

Feb 24th, 2015
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  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 enableAPI_http to true in ComputerCraft.cfg" )
  15.     return
  16. end
  17.  
  18. local function get(url)
  19.     write( "Connecting... " )
  20.     local response = http.get(url)
  21.        
  22.     if response then
  23.         print( "Success." )
  24.        
  25.         local sResponse = response.readAll()
  26.         response.close()
  27.         return sResponse
  28.     else
  29.         printError( "Failed." )
  30.     end
  31. end
  32.  
  33. local sCommand = tArgs[1]
  34. if type(tArgs[1]) == "string" and type(tArgs[2]) == "string" then
  35.     -- Determine file to download
  36.     local sUrl  = tArgs[1]
  37.     local sFile = tArgs[2]
  38.     local sPath = shell.resolve( sFile )
  39.     if fs.exists( sPath ) then
  40.         print( "File already exists" )
  41.         return
  42.     end
  43.    
  44.     -- GET the contents from pastebin
  45.     local res = get(sUrl)
  46.     if res then        
  47.         local file = fs.open( sPath, "w" )
  48.         file.write( res )
  49.         file.close()
  50.        
  51.         print( "Downloaded as "..sFile )
  52.     end
  53. else
  54.     printUsage()
  55.     return
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement