EnterYourName

Github repo downloader fork

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