Advertisement
DYankee

git get

Apr 28th, 2023 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. -- Set the repository owner, name, and branch
  2. local owner = "DYankee"
  3. local repo = "LapOs"
  4. local path = "main"
  5. local baseUrl = "https://api.github.com/repos/" .. owner .. "/" .. repo .. "/contents/"
  6. -- Set the directory where you want to download the files
  7. local directory = "/LapOs/"
  8.  
  9. -- Define a function to get all folders in folder
  10. local function get_folder_list(url)
  11.     local url = "https://api.github.com/repos/" .. owner .. "/" .. repo .. "/contents/"
  12.     local response = http.get(url)
  13.     local contents = textutils.unserialiseJSON(response.readAll())
  14.     local folders = {}
  15.  
  16.     --check for folders
  17.     for i = 1, #contents, 1 do
  18.         if contents[i].type == "dir" then
  19.             folders[i] = {url = contents[i].url}
  20.             folders[i]= {name = contents[i].name}
  21.         end
  22.     end
  23.     return folders
  24. end
  25.  
  26. -- Define a function to get all files in folder
  27. local function get_file_list(dir)
  28.     local url = baseUrl .. dir
  29.     local response = http.get(url)
  30.     local contents = textutils.unserialiseJSON(response.readAll())
  31.     local files = {}
  32.  
  33.     --check for folders
  34.     for i = 1, #contents, 1 do
  35.         if contents[i].type =="file" then
  36.             files[i] = {url = contents[i].url}
  37.             files[i] = {name = contents[i].name}
  38.         end
  39.     end
  40.     return files
  41. end
  42.  
  43. -- Define a function to download a file
  44. local function download_File(url, path)
  45.     local content = http.get(url).readAll()
  46.     local file = fs.open(path, "w")
  47.     file.write(content)
  48.     file.close()
  49. end
  50.  
  51. -- Define a function to download folder contents
  52. local function download_folder(folder)
  53.     local files = get_file_list(folder.name)
  54.     for i = 1, #files, 1 do
  55.         download_File(files[i].url, files[i].path)
  56.     end
  57. end
  58.  
  59. local folders = get_folder_list()
  60.  
  61. for i = 1, #folders, 1 do
  62.     download_folder(folders[i])
  63. end
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement