Bmorr

get_repo.lua

May 7th, 2025 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. -- get_repo.lua
  2. -- This script downloads all files from a specified GitHub repository branch
  3. --[[
  4.     pastebin get 48TNyyJZ get_repo.lua
  5. ]]
  6. -- Get arguments from the command line
  7. local args = { ... }
  8. if #args < 2 then
  9.     print("Usage: get_repo <repo_user> <repo_name> [branch] [target_dir]")
  10.     return
  11. end
  12.  
  13. local repo_user = args[1]          -- GitHub username
  14. local repo_name = args[2]          -- GitHub repository name
  15. local branch = args[3] or "main"   -- Branch name (default: "main")
  16. local target_dir = args[4] or repo_name -- Local output directory (default: "repo_files")
  17.  
  18. -- GitHub API URL to list files
  19. local api_url = "https://api.github.com/repos/" .. repo_user .. "/" .. repo_name .. "/git/trees/" .. branch .. "?recursive=1"
  20. print("Fetching GitHub tree from: " .. api_url)
  21.  
  22. -- Make request to GitHub API
  23. local response = http.get(api_url)
  24. if not response then
  25.     print("Failed to fetch GitHub API tree.")
  26.     return
  27. end
  28.  
  29. -- Read and decode JSON response
  30. local raw_data = response.readAll()
  31. response.close()
  32.  
  33. print("Raw response length: " .. #raw_data)
  34. local data = textutils.unserializeJSON(raw_data)
  35.  
  36. if not data then
  37.     print("Failed to decode JSON from API response.")
  38.     return
  39. end
  40.  
  41. if not data.tree then
  42.     print("No 'tree' field in API response.")
  43.     return
  44. end
  45.  
  46. -- Create local directory if needed
  47. if not fs.exists(target_dir) then
  48.     fs.makeDir(target_dir)
  49.     print("Created target directory: " .. target_dir)
  50. end
  51.  
  52. -- Download each file from the tree
  53. local count = 0
  54. for _, item in ipairs(data.tree) do
  55.     if item.type == "blob" then
  56.         local raw_url = "https://raw.githubusercontent.com/" .. repo_user .. "/" .. repo_name .. "/" .. branch .. "/" .. item.path
  57.         local local_path = fs.combine(target_dir, item.path)
  58.  
  59.         -- Create any necessary directories
  60.         local local_dir = fs.getDir(local_path)
  61.         if not fs.exists(local_dir) then
  62.             fs.makeDir(local_dir)
  63.             print("Created directory: " .. local_dir)
  64.         end
  65.  
  66.         print("Downloading: " .. item.path)
  67.         local file_res = http.get(raw_url)
  68.         if file_res then
  69.             local content = file_res.readAll()
  70.             file_res.close()
  71.  
  72.             local file = fs.open(local_path, "w")
  73.             file.write(content)
  74.             file.close()
  75.  
  76.             print("Saved: " .. local_path)
  77.             count = count + 1
  78.         else
  79.             print("Failed to download file: " .. raw_url)
  80.         end
  81.     end
  82. end
  83.  
  84. print("Download complete. Total files: " .. count)
  85.  
Advertisement
Add Comment
Please, Sign In to add comment