Advertisement
DuckStrom

ComputerCraft wget

Oct 12th, 2022 (edited)
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | Gaming | 0 0
  1. -- pastebin get hkhu8kf6 wget
  2.  
  3. -- Slightly modified version of https://github.com/cc-tweaked/CC-Tweaked/blob/mc-1.16.x/src/main/resources/data/computercraft/lua/rom/programs/http/wget.lua
  4. -- which is included by default in 1.8.9+ CC versions
  5.  
  6. local function printUsage()
  7.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  8.     print("Usage:")
  9.     print(programName .. " <url> [filename]")
  10.     print(programName .. " run <url>")
  11. end
  12.  
  13. local tArgs = { ... }
  14.  
  15. local run = false
  16. if tArgs[1] == "run" then
  17.     table.remove(tArgs, 1)
  18.     run = true
  19. end
  20.  
  21. if #tArgs < 1 then
  22.     printUsage()
  23.     return
  24. end
  25.  
  26. local url = table.remove(tArgs, 1)
  27.  
  28. if not http then
  29.     printError("wget requires the http API, but it is not enabled")
  30.     printError("Set http_enable to true in ComputerCraft.cfg")
  31.     return
  32. end
  33.  
  34. local function getFilename(sUrl)
  35.     sUrl = sUrl:gsub("[#?].*" , ""):gsub("/+$" , "")
  36.     return sUrl:match("/([^/]+)$")
  37. end
  38.  
  39. local function get(sUrl)
  40.     -- Check if the URL is valid
  41.     local ok, err = http.checkURL(url)
  42.     if not ok then
  43.         printError(err or "Invalid URL.")
  44.         return
  45.     end
  46.  
  47.     write("Connecting to " .. sUrl .. "... ")
  48.  
  49.     local response = http.get(sUrl , nil , true)
  50.     if not response then
  51.         print("Failed.")
  52.         return nil
  53.     end
  54.  
  55.     print("Success.")
  56.  
  57.     local sResponse = response.readAll()
  58.     response.close()
  59.     return sResponse or ""
  60. end
  61.  
  62. if run then
  63.     local res = get(url)
  64.     if not res then return end
  65.  
  66.     local func, err = load(res, getFilename(url), "t", _ENV)
  67.     if not func then
  68.         printError(err)
  69.         return
  70.     end
  71.  
  72.     local ok, err = pcall(func, table.unpack(tArgs))
  73.     if not ok then
  74.         printError(err)
  75.     end
  76. else
  77.     local sFile = tArgs[1] or getFilename(url) or url
  78.     local sPath = shell.resolve(sFile)
  79.     if fs.exists(sPath) then
  80.         print("File already exists")
  81.         return
  82.     end
  83.  
  84.     local res = get(url)
  85.     if not res then return end
  86.  
  87.     local file, err = fs.open(sPath, "wb")
  88.     if not file then
  89.         printError("Cannot save file: " .. err)
  90.         return
  91.     end
  92.  
  93.     file.write(res)
  94.     file.close()
  95.  
  96.     print("Downloaded as " .. sFile)
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement