Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not http then
- return error"Must enable HTTP for this program to work!"
- end
- --[[
- This is Install Utils by Geforce Fan. It's found @ BvL9rr0N , please get it from there to be sure it's up to date.
- Just paste it into your installer!
- ]]
- local function pasteRun(id,aa)
- aa=aa or {}
- if not type(id)=="string" then
- return "Must has string!"
- end
- --get program off pastebin
- local a=http.get("http://www.pastebin.com/raw.php?i="..id)
- if not a then return false end
- a=a.readAll()
- if not a then return false end
- local env= setmetatable({},{__index=_G})
- if not aa then
- env = _G
- end
- a=load(a,"userPasteRun","t",env)
- if not a then return false end
- local ok,err=pcall(a,unpack(aa))
- --a()
- if (not ok) and err then
- printError("Error running "..id..": "..err)
- return false
- end
- return err,env
- end
- local function pastePI(id,tab)
- local returned,env = pasteRun(id)
- _G[tab] = env
- end
- local function pasteGet(id)
- if not type(id)=="string" then
- return "Must has string!"
- end
- --get program off pastebin
- local a=http.get("http://www.pastebin.com/raw.php?i="..id)
- if not a then return false end
- a=a.readAll()
- if not a then return false end
- return a
- end
- --This is the end of installutils. The rest is this person's installer.
- local function cWrite(txt)
- local _,y=term.getCursorPos()
- term.setCursorPos(math.floor((term.getSize()-#txt)/2),y)
- term.write(txt)
- end
- --[[ /gitget
- GitHub downloading utility for CC.
- Developed by apemanzilla.
- This requires ElvishJerricco's JSON parsing API.
- Direct link: http://pastebin.com/raw.php?i=4nRg9CHU
- ]]--
- -- Edit these variables to use preset mode.
- -- Whether to download the files asynchronously (huge speed benefits, will also retry failed files)
- -- 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
- local async = true
- -- Whether to write to the terminal as files are downloaded
- -- Note that unless checked for this will not affect pre-set start/done code below
- local silent = false
- local iColors
- if term.isColor() then
- iColors = {
- loadBar = colors.cyan,
- loadBarBack = colors.blue,
- background = colors.lightBlue,
- text = colors.black,
- }
- else
- iColors = {
- loadBar = colors.black,
- loadBarBack = colors.gray,
- background = colors.white,
- text = colors.black,
- }
- end
- --temp
- term.setBackgroundColor(iColors.background)
- term.clear()
- local installingName = "Testing..."
- local preset = {
- -- The GitHub account name
- user = nil,
- -- The GitHub repository name
- repo = nil,
- -- The branch or commit tree to download (defaults to 'master')
- branch = nil,
- -- The local folder to save all the files to (defaults to '/')
- path = nil,
- -- Function to run before starting the download
- start = function()
- end,
- -- Function to run when the download completes
- done = function()
- if not silent then cWrite"Done" end
- end
- }
- -- Leave the rest of the program alone.
- local args = {...}
- args[1] = preset.user or args[1]
- args[2] = preset.repo or args[2]
- args[3] = preset.branch or args[3] or "master"
- args[4] = preset.path or args[4] or ""
- if #args < 2 then
- print("Usage:\n"..((shell and shell.getRunningProgram()) or "gitget").." <user> <repo> [branch/tree] [path]") error()
- end
- local lastFileLength = 0
- local function printReadOnly(file)
- local str = ""
- for i=1,lastFileLength-#file do
- str=str.." "
- end
- term.setCursorPos(1,5)
- printError(file.." was read-only."..str)
- term.setTextColor(iColors.text)
- lastFileLength = #file
- end
- local function save(data,file)
- local file = shell.resolve(file:gsub("%%20"," "))
- 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
- 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
- fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
- end
- if fs.isReadOnly(file) then
- printReadOnly(file)
- return
- end
- local f = fs.open(file,"w")
- f.write(data)
- f.close()
- end
- local function download(url, file)
- save(http.get(url).readAll(),file)
- end
- if not json then
- --download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
- --os.loadAPI("json")
- --^ 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.
- pastePI("4nRg9CHU","json")
- end
- preset.start()
- local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
- if data.message and data.message:find("API rate limit exceeded") then error("Out of API calls, try again later") end
- if data.message and data.message == "Not found" then error("Invalid repository",2) else
- for k,v in pairs(data.tree) do
- -- Make directories
- if v.type == "tree" then
- if fs.isReadOnly(fs.combine(args[4],v.path)) then
- printReadOnly(fs.combine(args[4],v.path))
- else
- fs.makeDir(fs.combine(args[4],v.path))
- end
- end
- end
- local drawProgress
- if async and not silent then
- local wide, _ = term.getSize()
- local y = _-3
- term.setTextColor(iColors.text)
- term.setCursorPos(1,1)
- cWrite("Installing "..installingName)
- print("\n")
- cWrite"Errors:"
- paintutils.drawLine(3,y,wide-2,y,iColors.loadBarBack)
- drawProgress = function(done, max)
- local value = done / max
- term.setCursorPos(3,y)
- term.setBackgroundColor(iColors.loadBar)
- term.write((" "):rep(math.floor(value * (wide - 4))))
- term.setBackgroundColor(iColors.background)
- local percent = math.floor(value * 100) .. "%"
- term.setCursorPos(1,y+2)
- cWrite(percent)
- end
- end
- local filecount = 0
- local downloaded = 0
- local paths = {}
- local failed = {}
- for k,v in pairs(data.tree) do
- -- Send all HTTP requests (async)
- if v.type == "blob" then
- v.path = v.path:gsub("%s","%%20")
- local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
- if async then
- http.request(url)
- paths[url] = fs.combine(args[4],v.path)
- filecount = filecount + 1
- else
- download(url, fs.combine(args[4], v.path))
- if not silent then print(fs.combine(args[4], v.path)) end
- end
- end
- end
- while downloaded < filecount do
- local e, a, b = os.pullEvent()
- if e == "http_success" then
- save(b.readAll(),paths[a])
- downloaded = downloaded + 1
- if not silent then drawProgress(downloaded,filecount) end
- elseif e == "http_failure" then
- -- Retry in 3 seconds
- failed[os.startTimer(3)] = a
- elseif e == "timer" and failed[a] then
- http.request(failed[a])
- end
- end
- end
- preset.done()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment