Advertisement
Commandcracker

Install YouCube

Oct 26th, 2022 (edited)
9,963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. --[[
  2. _ _  _ ____ ___ ____ _    _    ____ ____
  3. | |\ | [__   |  |__| |    |    |___ |__/
  4. | | \| ___]  |  |  | |___ |___ |___ |  \
  5.  
  6. Github Repository: https://github.com/CC-YouCube
  7. Homepage: https://youcube.madefor.cc/
  8. License: GPL-3.0
  9. ]]
  10. -- OpenPastebinInstaller v1.0.0 (based on wget)
  11.  
  12. local url_to_installer = "https://raw.githubusercontent.com/CC-YouCube/installer/main/src/installer.lua"
  13.  
  14. if not http then
  15.     printError("OpenPastebinInstaller requires the http API")
  16.     printError("Set http.enabled to true in ComputerCraft config")
  17.     return
  18. end
  19.  
  20. local function getFilename(url_to_file)
  21.     return url_to_file:gsub("[#?].*", ""):gsub("/+$", ""):match("/([^/]+)$")
  22. end
  23.  
  24. local function printColoured(text, colour)
  25.     term.setTextColour(colour)
  26.     print(text)
  27. end
  28.  
  29. local function http_get(url)
  30.     -- Check if the URL is valid
  31.     local valid_url, error_message = http.checkURL(url)
  32.     if not valid_url then
  33.         printError(('"%s" %s.'):format(url, error_message or "Invalid URL"))
  34.         return
  35.     end
  36.  
  37.     printColoured(('Connecting to "%s" ... '):format(url), colors.lightGray)
  38.  
  39.     local response, http_error_message = http.get(url, nil, true)
  40.     if not response then
  41.         printError(('Failed to download "%s" (%s).'):format(url, http_error_message or "Unknown error"))
  42.         return nil
  43.     end
  44.  
  45.     local previous_colour = term.getTextColour()
  46.  
  47.     printColoured(('Runnig "%s".'):format(getFilename(url)), colors.lime)
  48.  
  49.     -- Reset colour
  50.     term.setTextColour(previous_colour)
  51.  
  52.     local response_body = response.readAll()
  53.     response.close()
  54.  
  55.     if not response_body then
  56.         printError(('Failed to download "%s" (Empty response).'):format(url))
  57.     end
  58.  
  59.     return response_body
  60. end
  61.  
  62. local response_body = http_get(url_to_installer)
  63.  
  64. local loaded_function, load_error_message = load(response_body, getFilename(url_to_installer), "t", _ENV)
  65. if not loaded_function then
  66.     printError(load_error_message)
  67.     return
  68. end
  69.  
  70. local pcall_status, function_error_message = pcall(loaded_function)
  71. if not pcall_status then
  72.     printError(function_error_message)
  73. end
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement