5bitesofcookies

guiGitGet

Oct 5th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.99 KB | None | 0 0
  1. if not http then
  2.     return error"Must enable HTTP for this program to work!"
  3. end
  4. --[[
  5. This is Install Utils by Geforce Fan. It's found @ BvL9rr0N , please get it from there to be sure it's up to date.
  6. Just paste it into your installer!
  7. ]]
  8. local function pasteRun(id,aa)
  9.   aa=aa or {}
  10.   if not type(id)=="string" then
  11.     return "Must has string!"
  12.   end
  13.   --get program off pastebin
  14.   local a=http.get("http://www.pastebin.com/raw.php?i="..id)
  15.   if not a then return false end
  16.   a=a.readAll()
  17.   if not a then return false end
  18.   local env= setmetatable({},{__index=_G})
  19.   if not aa then
  20.     env = _G
  21.   end
  22.   a=load(a,"userPasteRun","t",env)
  23.   if not a then return false end
  24.   local ok,err=pcall(a,unpack(aa))
  25.   --a()
  26.   if (not ok) and err then
  27.     printError("Error running "..id..": "..err)
  28.     return false
  29.   end
  30.   return err,env
  31. end
  32. local function pastePI(id,tab)
  33.   local returned,env = pasteRun(id)
  34.   _G[tab] = env
  35. end
  36. local function pasteGet(id)
  37.   if not type(id)=="string" then
  38.     return "Must has string!"
  39.   end
  40.   --get program off pastebin
  41.   local a=http.get("http://www.pastebin.com/raw.php?i="..id)
  42.   if not a then return false end
  43.   a=a.readAll()
  44.   if not a then return false end
  45.   return a
  46. end
  47.  
  48. --This is the end of installutils. The rest is this person's installer.
  49. local function cWrite(txt)
  50.     local _,y=term.getCursorPos()
  51.     term.setCursorPos(math.floor((term.getSize()-#txt)/2),y)
  52.     term.write(txt)
  53. end
  54.  
  55.  
  56.  
  57. --[[ /gitget
  58. GitHub downloading utility for CC.
  59. Developed by apemanzilla.
  60.  
  61. This requires ElvishJerricco's JSON parsing API.
  62. Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
  63. ]]--
  64.  
  65. -- Edit these variables to use preset mode.
  66. -- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
  67. -- 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
  68. local async = true
  69.  
  70. -- Whether to write to the terminal as files are downloaded
  71. -- Note that unless checked for this will not affect pre-set start/done code below
  72. local silent = false
  73. local iColors
  74. if term.isColor() then
  75.     iColors = {
  76.         loadBar = colors.cyan,
  77.         loadBarBack = colors.blue,
  78.         background = colors.lightBlue,
  79.         text = colors.black,
  80.  
  81.     }
  82. else
  83.     iColors = {
  84.         loadBar = colors.black,
  85.         loadBarBack = colors.gray,
  86.         background = colors.white,
  87.         text = colors.black,
  88.  
  89.     }
  90. end
  91.  
  92. --temp
  93. term.setBackgroundColor(iColors.background)
  94. term.clear()
  95. local installingName = "Testing..."
  96. local preset = {
  97.     -- The GitHub account name
  98.     user = nil,
  99.     -- The GitHub repository name
  100.     repo = nil,
  101.    
  102.     -- The branch or commit tree to download (defaults to 'master')
  103.     branch = nil,
  104.    
  105.     -- The local folder to save all the files to (defaults to '/')
  106.     path = nil,
  107.    
  108.     -- Function to run before starting the download
  109.     start = function()
  110.     end,
  111.    
  112.     -- Function to run when the download completes
  113.     done = function()
  114.         if not silent then cWrite"Done" end
  115.     end
  116. }
  117.  
  118. -- Leave the rest of the program alone.
  119. local args = {...}
  120.  
  121. args[1] = preset.user or args[1]
  122. args[2] = preset.repo or args[2]
  123. args[3] = preset.branch or args[3] or "master"
  124. args[4] = preset.path or args[4] or ""
  125.  
  126. if #args < 2 then
  127.         print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
  128. end
  129. local lastFileLength = 0
  130. local function printReadOnly(file)
  131.     local str = ""
  132.     for i=1,lastFileLength-#file do
  133.         str=str.." "
  134.     end
  135.     term.setCursorPos(1,5)
  136.     printError(file.." was read-only."..str)
  137.     term.setTextColor(iColors.text)
  138.     lastFileLength = #file
  139. end
  140. local function save(data,file)
  141.     local file = shell.resolve(file:gsub("%%20"," "))
  142.     if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) and (not fs.isReadOnly(string.sub(file,1,#file - #fs.getName(file)))) then
  143.         if fs.exists(string.sub(file,1,#file - #fs.getName(file))) and (not fs.isReadOnly(string.sub(file,1,#file - #fs.getName(file)))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  144.         fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  145.     end
  146.     if fs.isReadOnly(file) then
  147.         printReadOnly(file)
  148.         return
  149.     end
  150.     local f = fs.open(file,"w")
  151.     f.write(data)
  152.     f.close()
  153. end
  154.  
  155. local function download(url, file)
  156.     save(http.get(url).readAll(),file)
  157. end
  158.  
  159. if not json then
  160.     --download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  161.     --os.loadAPI("json")
  162.     --^ No. We can just load the API off pastebin to avoid the risk of the user running out of space and save them save, aswell as prevent a useless file from going on their computer.
  163.     pastePI("4nRg9CHU","json")
  164. end
  165.  
  166. preset.start()
  167. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  168. if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
  169. if data.message and data.message == "Not found" then error("Invalid repository",2) else
  170.     for k,v in pairs(data.tree) do
  171.         -- Make directories
  172.         if v.type == "tree" then
  173.             if fs.isReadOnly(fs.combine(args[4],v.path)) then
  174.                 printReadOnly(fs.combine(args[4],v.path))
  175.             else
  176.                 fs.makeDir(fs.combine(args[4],v.path))
  177.             end
  178.         end
  179.     end
  180.     local drawProgress
  181.     if async and not silent then
  182.        
  183.         local wide, _ = term.getSize()
  184.         local y = _-3
  185.         term.setTextColor(iColors.text)
  186.         term.setCursorPos(1,1)
  187.         cWrite("Installing "..installingName)
  188.         print("\n")
  189.         cWrite"Errors:"
  190.         paintutils.drawLine(3,y,wide-2,y,iColors.loadBarBack)
  191.         drawProgress = function(done, max)
  192.             local value = done / max
  193.             term.setCursorPos(3,y)
  194.             term.setBackgroundColor(iColors.loadBar)
  195.             term.write((" "):rep(math.floor(value * (wide - 4))))
  196.             term.setBackgroundColor(iColors.background)
  197.             local percent = math.floor(value * 100) .. "%"
  198.             term.setCursorPos(1,y+2)
  199.             cWrite(percent)
  200.         end
  201.        
  202.     end
  203.     local filecount = 0
  204.     local downloaded = 0
  205.     local paths = {}
  206.     local failed = {}
  207.     for k,v in pairs(data.tree) do
  208.         -- Send all HTTP requests (async)
  209.         if v.type == "blob" then
  210.             v.path = v.path:gsub("%s","%%20")
  211.             local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
  212.             if async then
  213.                 http.request(url)
  214.                 paths[url] = fs.combine(args[4],v.path)
  215.                 filecount = filecount + 1
  216.             else
  217.                 download(url, fs.combine(args[4], v.path))
  218.                 if not silent then print(fs.combine(args[4], v.path)) end
  219.             end
  220.         end
  221.     end
  222.     while downloaded < filecount do
  223.         local e, a, b = os.pullEvent()
  224.         if e == "http_success" then
  225.             save(b.readAll(),paths[a])
  226.             downloaded = downloaded + 1
  227.             if not silent then drawProgress(downloaded,filecount) end
  228.         elseif e == "http_failure" then
  229.             -- Retry in 3 seconds
  230.             failed[os.startTimer(3)] = a
  231.         elseif e == "timer" and failed[a] then
  232.             http.request(failed[a])
  233.         end
  234.     end
  235. end
  236. preset.done()
  237. term.setBackgroundColor(colors.black)
  238. term.clear()
  239. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment