PaffcioStudio

ComputerCraft GitHub Downloader

Feb 3rd, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. local async = true
  2. local silent = false
  3.  
  4. local preset = {
  5.     user = nil,
  6.     repo = nil,
  7.     branch = nil,
  8.     path = nil,
  9.     start = function()
  10.         if not silent then print("Pobieranie danych z GitHub...") end
  11.     end,
  12.     done = function()
  13.         if not silent then print("Gotowe") end
  14.     end
  15. }
  16.  
  17. local args = {...}
  18.  
  19. args[1] = preset.user or args[1]
  20. args[2] = preset.repo or args[2]
  21. args[3] = preset.branch or args[3] or "master"
  22. args[4] = preset.path or args[4] or ""
  23.  
  24. if #args < 2 then
  25.         print("Uzycie:\n"..((shell and shell.getRunningProgram()) or "git").." <uzytkownik> <repozytorium>") error()
  26. end
  27.  
  28. local function save(data,file)
  29.     local file = shell.resolve(file:gsub("%%20"," "))
  30.     if not (fs.exists(string.sub(file,1,#file - #fs.getName(file))) and fs.isDir(string.sub(file,1,#file - #fs.getName(file)))) then
  31.         if fs.exists(string.sub(file,1,#file - #fs.getName(file))) then fs.delete(string.sub(file,1,#file - #fs.getName(file))) end
  32.         fs.makeDir(string.sub(file,1,#file - #fs.getName(file)))
  33.     end
  34.     local f = fs.open(file,"w")
  35.     f.write(data)
  36.     f.close()
  37. end
  38.  
  39. local function download(url, file)
  40.     save(http.get(url).readAll(),file)
  41. end
  42.  
  43. if not json then
  44.     download("http://pastebin.com/raw.php?i=4nRg9CHU","json")
  45.     os.loadAPI("json")
  46. end
  47.  
  48. preset.start()
  49. local data = json.decode(http.get("https://api.github.com/repos/"..args[1].."/"..args[2].."/git/trees/"..args[3].."?recursive=1").readAll())
  50. if data.message and data.message:find("Polaczenie z API nie powiodlo sie.") then error("Blad w API, sprobuj pozniej.") end
  51. if data.message and data.message == "Not found" then error("Niepoprawne repozytorium",2) else
  52.     for k,v in pairs(data.tree) do
  53.         -- Make directories
  54.         if v.type == "tree" then
  55.             fs.makeDir(fs.combine(args[4],v.path))
  56.             if not hide_progress then
  57.             end
  58.         end
  59.     end
  60.     local drawProgress
  61.     if async and not silent then
  62.         local _, y = term.getCursorPos()
  63.         local wide, _ = term.getSize()
  64.         term.setCursorPos(1, y)
  65.         term.write("[")
  66.         term.setCursorPos(wide - 6, y)
  67.         term.write("]")
  68.         drawProgress = function(done, max)
  69.             local value = done / max
  70.             term.setCursorPos(2,y)
  71.             term.write(("="):rep(math.floor(value * (wide - 8))))
  72.             local percent = math.floor(value * 100) .. "%"
  73.             term.setCursorPos(wide - percent:len(),y)
  74.             term.write(percent)
  75.         end
  76.     end
  77.     local filecount = 0
  78.     local downloaded = 0
  79.     local paths = {}
  80.     local failed = {}
  81.     for k,v in pairs(data.tree) do
  82.         -- Send all HTTP requests (async)
  83.         if v.type == "blob" then
  84.             v.path = v.path:gsub("%s","%%20")
  85.             local url = "https://raw.github.com/"..args[1].."/"..args[2].."/"..args[3].."/"..v.path,fs.combine(args[4],v.path)
  86.             if async then
  87.                 http.request(url)
  88.                 paths[url] = fs.combine(args[4],v.path)
  89.                 filecount = filecount + 1
  90.             else
  91.                 download(url, fs.combine(args[4], v.path))
  92.                 if not silent then print(fs.combine(args[4], v.path)) end
  93.             end
  94.         end
  95.     end
  96.     while downloaded < filecount do
  97.         local e, a, b = os.pullEvent()
  98.         if e == "http_success" then
  99.             save(b.readAll(),paths[a])
  100.             downloaded = downloaded + 1
  101.             if not silent then drawProgress(downloaded,filecount) end
  102.         elseif e == "http_failure" then
  103.             -- Retry in 3 seconds
  104.             failed[os.startTimer(3)] = a
  105.         elseif e == "timer" and failed[a] then
  106.             http.request(failed[a])
  107.         end
  108.     end
  109. end
  110. preset.done()
  111. shell.run("delete README*.*")
  112. shell.run("delete json*")
Add Comment
Please, Sign In to add comment