SHARE
TWEET

wget

a guest Jan 13th, 2016 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local function printUsage()
  3.     print( "Usage:" )
  4.     print( "wget <url> [filename]" )
  5. end
  6.  
  7. local tArgs = { ... }
  8. if #tArgs < 1 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 function split(str,delimiter)
  48.   local cur = ""
  49.   local res = {}
  50.   for i = 1,#str do
  51.     local char = str:sub(i,i)
  52.     if char == delimiter then
  53.       table.insert(res,cur)
  54.       cur = ""
  55.     else
  56.       cur = cur..char
  57.     end
  58.   end
  59.   table.insert(res,cur)
  60.   return res
  61. end
  62. if not sFile then
  63.   local parts = split(sUrl,"/")
  64.   local file = parts[#parts]
  65.   sFile = split(file,"?")[1]
  66. end
  67. local sPath = shell.resolve( sFile )
  68. if fs.exists( sPath ) then
  69.     print( "File already exists" )
  70.     return
  71. end
  72.  
  73. -- Do the get
  74. local res = get( sUrl )
  75. if res then
  76.     local file = fs.open( sPath, "w" )
  77.     file.write( res )
  78.     file.close()
  79.  
  80.     print( "Downloaded as "..sFile )
  81. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top