Advertisement
ComputerMan123

Lopa Setup

Feb 6th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ /gitget
  2. GitHub downloading utility for CC.
  3. Developed by apemanzilla.
  4.  
  5. This requires ElvishJerricco's JSON parsing API.
  6. Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
  7. ]]--
  8.  
  9. -- Edit these variables to use preset mode.
  10. -- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
  11. -- If false will download the files one by one and use the old output (List each file name as it's downloaded) instead of the progress bar
  12. local async = true
  13.  
  14. -- Whether to write to the terminal as files are downloaded
  15. -- Note that unless checked for this will not affect pre-set start/done code below
  16. local silent = false
  17.  
  18. local preset = {
  19.     -- The GitHub account name
  20.     user = "CoolMan119",
  21.     -- The GitHub repository name
  22.     repo = "Lopa",
  23.    
  24.     -- The branch or commit tree to download (defaults to 'master')
  25.     branch = "testing",
  26.    
  27.     -- The local folder to save all the files to (defaults to '/')
  28.     path = nil,
  29.    
  30.     -- Function to run before starting the download
  31.     start = function()
  32.         if not silent then print("Downloading files from GitHub...") end
  33.     end,
  34.    
  35.     -- Function to run when the download completes
  36.     done = function()
  37.         if not silent then
  38.           print("Done!")
  39.           print("Rebooting...")
  40.           os.sleep(1)
  41.           os.reboot()
  42.         end
  43.     end
  44. }
  45.  
  46. -- Leave the rest of the program alone.
  47. local args = {...}
  48.  
  49. args[1] = preset.user or args[1]
  50. args[2] = preset.repo or args[2]
  51. args[3] = preset.branch or args[3] or "master"
  52. args[4] = preset.path or args[4] or ""
  53.  
  54. if #args < 2 then
  55.         print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
  56. end
  57.  
  58. local function save(data,file)
  59.     local file = shell.resolve(file:gsub("%%20"," "))
  60.     if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
  61.         if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  62.         fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  63.     end
  64.     local f = fs.open(file,"w")
  65.     f.write(data)
  66.     f.close()
  67. end
  68.  
  69. local function download(url, file)
  70.     save(http.get(url).readAll(),file)
  71. end
  72.  
  73. if not json then
  74.     download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  75.     os.loadAPI("json")
  76. end
  77.  
  78. preset.start()
  79. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  80. if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
  81. if data.message and data.message == "Not found" then error("Invalid repository",2) else
  82.     for k,v in pairs(data.tree) do
  83.         -- Make directories
  84.         if v.type == "tree" then
  85.             fs.makeDir(fs.combine(args[4],v.path))
  86.             if not hide_progress then
  87.             end
  88.         end
  89.     end
  90.     local drawProgress
  91.     if async and not silent then
  92.         local _, y = term.getCursorPos()
  93.         local wide, _ = term.getSize()
  94.         term.setCursorPos(1, y)
  95.         term.write("[")
  96.         term.setCursorPos(wide - 6, y)
  97.         term.write("]")
  98.         drawProgress = function(done, max)
  99.             local value = done / max
  100.             term.setCursorPos(2,y)
  101.             term.write(("="):rep(math.floor(value * (wide - 8))))
  102.             local percent = math.floor(value * 100) .. "%"
  103.             term.setCursorPos(wide - percent:len(),y)
  104.             term.write(percent)
  105.         end
  106.     end
  107.     local filecount = 0
  108.     local downloaded = 0
  109.     local paths = {}
  110.     local failed = {}
  111.     for k,v in pairs(data.tree) do
  112.         -- Send all HTTP requests (async)
  113.         if v.type == "blob" then
  114.             v.path = v.path:gsub("%s","%%20")
  115.             local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
  116.             if async then
  117.                 http.request(url)
  118.                 paths[url] = fs.combine(args[4],v.path)
  119.                 filecount = filecount + 1
  120.             else
  121.                 download(url, fs.combine(args[4], v.path))
  122.                 if not silent then print(fs.combine(args[4], v.path)) end
  123.             end
  124.         end
  125.     end
  126.     while downloaded < filecount do
  127.         local e, a, b = os.pullEvent()
  128.         if e == "http_success" then
  129.             save(b.readAll(),paths[a])
  130.             downloaded = downloaded + 1
  131.             if not silent then drawProgress(downloaded,filecount) end
  132.         elseif e == "http_failure" then
  133.             -- Retry in 3 seconds
  134.             failed[os.startTimer(3)] = a
  135.         elseif e == "timer" and failed[a] then
  136.             http.request(failed[a])
  137.         end
  138.     end
  139. end
  140. preset.done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement