kaibochan

pull.lua

Feb 23rd, 2025 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. if not arg[1] then
  2.     print("Usage:\n"
  3.         .. "arg[1] should be a github repository link\n"
  4.         .. "arg[2] should be a destination folder")
  5.     return
  6. end
  7.  
  8. local repo_link = arg[1]
  9. local destination = arg[2] or "."
  10.  
  11. function pull(repo_link, destination)
  12.     local github_api_link = "https://api.github.com/repos"
  13.  
  14.     url_parts = {}
  15.     for part in repo_link:gmatch("[^/]+") do
  16.         table.insert(url_parts, part)
  17.     end
  18.  
  19.     local github_root = "github.com"
  20.     local github_root_index
  21.     for index, part in ipairs(url_parts) do
  22.         if part == github_root then
  23.             github_root_index = index
  24.         end
  25.     end
  26.  
  27.     local owner = url_parts[github_root_index + 1]
  28.     local repo_name = url_parts[github_root_index + 2]
  29.  
  30.     local request_link =
  31.         github_api_link .. "/"
  32.         .. owner .. "/"
  33.         .. repo_name .. "/contents"
  34.     if not http.checkURL(request_link) then
  35.         error("Malformed URL:\n" .. request_link)
  36.     end
  37.  
  38.     local git_repo_request = http.get(request_link)
  39.     local repo = textutils.unserialiseJSON(git_repo_request.readAll())
  40.  
  41.     local to_pull = {}
  42.     for _, file in ipairs(repo) do
  43.         table.insert(to_pull, file)
  44.     end
  45.  
  46.     local all_files = {}
  47.  
  48.     while #to_pull ~= 0 do
  49.         if to_pull[1].type == "dir" then
  50.             local folder_link = request_link .. "/" .. to_pull[1].name
  51.             folder_link = folder_link:gsub("%s", "%%20")
  52.             if not http.checkURL(folder_link) then
  53.                 error("Malformed URL:\n" .. folder_link)
  54.             end
  55.  
  56.             local folder_result = http.get(folder_link).readAll()
  57.             local folder = textutils.unserialiseJSON(folder_result)
  58.  
  59.             for _, file in ipairs(folder) do
  60.                 file.name = to_pull[1].name .. "/" .. file.name
  61.                 table.insert(to_pull, file)
  62.             end
  63.         elseif to_pull[1].type == "file" then
  64.             table.insert(all_files, to_pull[1])
  65.         end
  66.  
  67.         table.remove(to_pull, 1)
  68.     end
  69.  
  70.     for _, file in ipairs(all_files) do
  71.         print("Requesting "..file.name.."...")
  72.        
  73.         local download_request = http.get(file.download_url)
  74.         while not download_request do
  75.             sleep(1)
  76.             download_request = http.get(file.download_url)
  77.         end
  78.        
  79.         local file_download = download_request.readAll()
  80.  
  81.         local file_handle = fs.open(destination .. "/" .. file.name, "w")
  82.         file_handle.write(file_download)
  83.         file_handle.close()
  84.     end
  85. end
  86.  
  87. pull(repo_link, destination)
  88.  
Add Comment
Please, Sign In to add comment