Advertisement
DYankee

etu

Oct 2nd, 2022 (edited)
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. --[[
  2.     Begin - Download repository
  3.         get folders
  4.         get files
  5.         IF files
  6.             download files
  7.         IF folders
  8.             WHILE (#folders > 0)
  9.                 get folders
  10.                 get files
  11.                 IF files
  12.                     download files
  13.             END WHILE
  14. ]]
  15.  
  16. local folders = {}
  17. local files = {}
  18. local args = {
  19.     Authorization =  "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"
  20. }
  21.  
  22. local function get_folders(url)
  23.     print("geting folders")
  24.     local response = http.get(url, args)
  25.     local contents = textutils.unserialiseJSON(response.readAll())
  26.     --check for folders
  27.     for i = 1, #contents, 1 do
  28.         if contents[i].type == "dir" then
  29.             folders[#folders + 1] = contents[i]
  30.         end
  31.     end
  32. end
  33.  
  34. local function get_files(url)
  35.     print("geting files")
  36.     local response = http.get(url, args)
  37.     local contents = textutils.unserialiseJSON(response.readAll())
  38.  
  39.     --check for folders
  40.     for i = 1, #contents, 1 do
  41.         if contents[i].type =="file" then
  42.             files[#files + 1] = contents[i]
  43.         end
  44.     end
  45. end
  46.  
  47. local function download_File(url, path)
  48.     print("downloading: " .. path)
  49.     local content = http.get(url, args).readAll()
  50.     local file = fs.open(path, "w")
  51.     file.write(content)
  52.     file.close()
  53. end
  54.  
  55.  
  56. local function Main()
  57.  
  58.     --const
  59.     local owner = "DYankee"
  60.     local repo = "LapOs"
  61.     local baseUrl = "https://api.github.com/repos/" .. owner .. "/" .. repo .. "/contents/"
  62.     local basepath = "Lapos/"
  63.  
  64.     get_folders(baseUrl)
  65.     get_files(baseUrl)
  66.  
  67.     if #files > 0 then
  68.         while #files > 0 do
  69.             local index = #files
  70.             download_File(files[#files].url, basepath .. files[#files].path)
  71.             table.remove(files, index)
  72.         end
  73.         print("no files")
  74.     end
  75.  
  76.     if #folders > 0 then
  77.         while #folders > 0 do
  78.             get_files(baseUrl .. folders[#folders].path)
  79.             if #files > 0 then
  80.                 while #files > 0 do
  81.                     download_File(files[#files].url, basepath .. files[#files].path)
  82.                     table.remove(files, #files)
  83.                 end
  84.             end
  85.             local index = #folders
  86.             get_folders(baseUrl .. folders[#folders].path)
  87.             table.remove(folders, index)
  88.         end
  89.     end
  90. print("finished")
  91. end
  92.  
  93. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement