Advertisement
goldfiction

CC_Wget

Sep 28th, 2023
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. local function wget(option, url, ziel)
  2.   if type(url) ~= "string" and type(ziel) ~= "string" then
  3.         return
  4.   elseif type(option) == "string" and option ~= "-f" and type(url) == "string" then
  5.         ziel = url
  6.         url = option
  7.   end
  8.   if http.checkURL(url) then
  9.         if fs.exists(ziel) and option ~= "-f" then
  10.           printError("<Error> Target exists already")
  11.           return
  12.         else
  13.           term.write("Downloading ... ")
  14.           local timer = os.startTimer(60)
  15.           http.request(url)
  16.           while true do
  17.                 local event, id, data = os.pullEvent()
  18.                 if event == "http_success" then
  19.                   print("success")
  20.                   local f = io.open(ziel, "w")
  21.                   f:write(data.readAll())
  22.                   f:close()
  23.                   data:close()
  24.                   print("Saved as " .. ziel)
  25.                   return true
  26.                 elseif event == "timer" and timer == id then
  27.                   printError("<Error> Timeout")
  28.                   return
  29.                 elseif event == "http_failure" then
  30.                   printError("<Error> Download")
  31.                   os.cancelAlarm(timer)
  32.                   return
  33.                 end
  34.           end
  35.         end
  36.   else
  37.         printError("<Error> URL")
  38.         return
  39.   end
  40. end
  41.  
  42. -- wget("http://example.org/", "testing")
  43. -- wget("-f", "http://example.org/", "testing")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement