BananeMan

wget

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