Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- get_repo.lua
- -- This script downloads all files from a specified GitHub repository branch
- --[[
- pastebin get 48TNyyJZ get_repo.lua
- ]]
- -- Get arguments from the command line
- local args = { ... }
- if #args < 2 then
- print("Usage: get_repo <repo_user> <repo_name> [branch] [target_dir]")
- return
- end
- local repo_user = args[1] -- GitHub username
- local repo_name = args[2] -- GitHub repository name
- local branch = args[3] or "main" -- Branch name (default: "main")
- local target_dir = args[4] or repo_name -- Local output directory (default: "repo_files")
- -- GitHub API URL to list files
- local api_url = "https://api.github.com/repos/" .. repo_user .. "/" .. repo_name .. "/git/trees/" .. branch .. "?recursive=1"
- print("Fetching GitHub tree from: " .. api_url)
- -- Make request to GitHub API
- local response = http.get(api_url)
- if not response then
- print("Failed to fetch GitHub API tree.")
- return
- end
- -- Read and decode JSON response
- local raw_data = response.readAll()
- response.close()
- print("Raw response length: " .. #raw_data)
- local data = textutils.unserializeJSON(raw_data)
- if not data then
- print("Failed to decode JSON from API response.")
- return
- end
- if not data.tree then
- print("No 'tree' field in API response.")
- return
- end
- -- Create local directory if needed
- if not fs.exists(target_dir) then
- fs.makeDir(target_dir)
- print("Created target directory: " .. target_dir)
- end
- -- Download each file from the tree
- local count = 0
- for _, item in ipairs(data.tree) do
- if item.type == "blob" then
- local raw_url = "https://raw.githubusercontent.com/" .. repo_user .. "/" .. repo_name .. "/" .. branch .. "/" .. item.path
- local local_path = fs.combine(target_dir, item.path)
- -- Create any necessary directories
- local local_dir = fs.getDir(local_path)
- if not fs.exists(local_dir) then
- fs.makeDir(local_dir)
- print("Created directory: " .. local_dir)
- end
- print("Downloading: " .. item.path)
- local file_res = http.get(raw_url)
- if file_res then
- local content = file_res.readAll()
- file_res.close()
- local file = fs.open(local_path, "w")
- file.write(content)
- file.close()
- print("Saved: " .. local_path)
- count = count + 1
- else
- print("Failed to download file: " .. raw_url)
- end
- end
- end
- print("Download complete. Total files: " .. count)
Advertisement
Add Comment
Please, Sign In to add comment