Ranger15

ccscript-install

Dec 13th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs, gUser, gRepo, gPath, gBranch = {...}, nil, "usr/apps/", "scripts/", "master"
  2. local usage = [[
  3.  github, rangerdude, CC-scripts, usr/apps/,scripts/, master  ]]
  4. local blackList = [[
  5. @blacklistedfile
  6. ]]
  7.  
  8. local title = "Github Repo Downloader"
  9. local fileList = {dirs={},files={}}
  10. local x , y = term.getSize()
  11.  
  12. -- GUI
  13. function printTitle()
  14.     local line = 2
  15.     term.setCursorPos(1,line)
  16.     for i = 2, x, 1 do write("-") end
  17.     term.setCursorPos((x-title:len())/2,line+1)
  18.     print(title)
  19.     for i = 2, x, 1 do write("-") end
  20. end
  21.  
  22. function writeCenter( str )
  23.     term.clear()
  24.     printTitle()
  25.     term.setCursorPos((x-str:len())/2-1,y/2-1)
  26.     for i = -1, str:len(), 1 do write("-") end
  27.     term.setCursorPos((x-str:len())/2-1,y/2)
  28.     print("|"..str.."|")
  29.     term.setCursorPos((x-str:len())/2-1,y/2+1)
  30.     for i = -1, str:len(), 1 do write("-") end
  31. end
  32.  
  33. function printUsage()
  34.     local str = "Press space key to continue"
  35.     term.clear()
  36.     printTitle()
  37.     term.setCursorPos(1,y/2-4)
  38.     print(usage)
  39.     term.setCursorPos((x-str:len())/2,y/2+7)
  40.     print(str)
  41.     while true do
  42.         local event, param1 = os.pullEvent("key")
  43.         if param1 == 57 then
  44.             sleep(0)
  45.             break
  46.         end
  47.     end
  48.     term.clear()
  49.     term.setCursorPos(1,1)
  50. end
  51.  
  52. -- Download File
  53. function downloadFile( path, url, name )
  54.     writeCenter("Downloading File: "..name)
  55.     dirPath = path:gmatch('([%w%_%.% %-%+%,%;%:%*%#%=%/]+)/'..name..'$')()
  56.     if dirPath ~= nil and not fs.isDir(dirPath) then fs.makeDir(dirPath) end
  57.     local content = http.get(url)
  58.     local file = fs.open(path,"w")
  59.     file.write(content.readAll())
  60.     file.close()
  61. end
  62.  
  63. -- Get Directory Contents
  64. function getGithubContents( path )
  65.     local pType, pPath, pName, checkPath = {}, {}, {}, {}
  66.     local response = http.get("https://api.github.com/repos/"..gUser.."/"..gRepo.."/contents/"..path.."/?ref="..gBranch)
  67.     if response then
  68.         response = response.readAll()
  69.         if response ~= nil then
  70.             for str in response:gmatch('"type":"(%w+)"') do table.insert(pType, str) end
  71.             for str in response:gmatch('"path":"([^\"]+)"') do table.insert(pPath, str) end
  72.             for str in response:gmatch('"name":"([^\"]+)"') do table.insert(pName, str) end
  73.         end
  74.     else
  75.         writeCenter( "Error: Can't resolve URL" )
  76.         sleep(2)
  77.         term.clear()
  78.         term.setCursorPos(1,1)
  79.         error()
  80.     end
  81.     return pType, pPath, pName
  82. end
  83.  
  84. -- Blacklist Function
  85. function isBlackListed( path )
  86.     if blackList:gmatch("@"..path)() ~= nil then
  87.         return true
  88.     end
  89. end
  90.  
  91. -- Download Manager
  92. function downloadManager( path )
  93.     local fType, fPath, fName = getGithubContents( path )
  94.     for i,data in pairs(fType) do
  95.         if data == "file" then
  96.             checkPath = http.get("https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i])
  97.             if checkPath == nil then
  98.                
  99.                 fPath[i] = fPath[i].."/"..fName[i]
  100.             end
  101.             local path = "usr/apps/"..gRepo.."/"..fPath[i]
  102.             if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end
  103.             if not fileList.files[path] and not isBlackListed(fPath[i]) then
  104.                 fileList.files[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]}
  105.             end
  106.         end
  107.     end
  108.     for i, data in pairs(fType) do
  109.         if data == "dir" then
  110.             local path = "usr/apps/"..gRepo.."/"..fPath[i]
  111.             if gPath ~= "" then path = gPath.."/"..gRepo.."/"..fPath[i] end
  112.             if not fileList.dirs[path] then
  113.                 writeCenter("Listing directory: "..fName[i])
  114.                 fileList.dirs[path] = {"https://raw.github.com/"..gUser.."/"..gRepo.."/"..gBranch.."/"..fPath[i],fName[i]}
  115.                 downloadManager( fPath[i] )
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. -- Main Function
  122. function main( path )
  123.     writeCenter("Connecting to Github")
  124.     downloadManager(path)
  125.     for i, data in pairs(fileList.files) do
  126.         downloadFile( i, data[1], data[2] )
  127.     end
  128.     writeCenter("Download completed")
  129.     sleep(2,5)
  130.     term.clear()
  131.     term.setCursorPos(1,1)
  132. end
  133.  
  134. -- Parse User Input
  135. function parseInput( user, repo , dldir, path, branch )
  136.     if path == nil then path = "" end
  137.     if branch ~= nil then gBranch = branch end
  138.     if repo == nil then printUsage()
  139.     else
  140.         gUser = user
  141.         gRepo = repo
  142.         if dldir ~= nil then gPath = dldir end
  143.         main( path )
  144.     end
  145. end
  146.  
  147. if not http then
  148.     writeCenter("You need to enable the HTTP API!")
  149.     sleep(3)
  150.     term.clear()
  151.     term.setCursorPos(1,1)
  152. else
  153.     for i=1, 5, 1 do
  154.         if tArgs[i] == "." then tArgs[i] = nil end
  155.     end
  156.     parseInput( tArgs[1], tArgs[2], tArgs[3], tArgs[4], tArgs[5] )
  157. end
Add Comment
Please, Sign In to add comment