Advertisement
Hiranus

CCBitBucket

Jun 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. local function GetTextFromUrl(address)
  2.   local fileHandle = http.get(address);
  3.   if fileHandle == nil then
  4.     error("Could not execute  \"get\"  on address: "..address)
  5.   end
  6.   local text = fileHandle.readAll();
  7.   fileHandle.close();
  8.   return text;
  9. end
  10.  
  11. local function Save(name,data)
  12.   local file = fs.open(name,"w")
  13.   if not file then
  14.     error("File "..name.." could not be opened");
  15.   end
  16.   file.write(data)
  17.   file.close()
  18. end
  19.  
  20. local function GetLastName(str)
  21.   local lastsl = string.match(str,'^.*()/');
  22.   if string.len(str) == lastsl then
  23.     str = str:sub(1, -2)
  24.     lastsl = string.match(str,'^.*()/');
  25.   end
  26.   return string.sub(str,lastsl+1);
  27. end
  28.  
  29. function SaveFileFromURL(address,newName,path)
  30.   if not newName then
  31.     newName = GetLastName(address);
  32.   end
  33.   if path then
  34.     if string.len(path) ~= string.match(path,'^.*()/') then
  35.       path = path .. "/";
  36.     end
  37.     newName = path .. newName;
  38.   end
  39.   print("Saving file "..address.. " as ".. newName)
  40.   Save(newName,GetTextFromUrl(address));
  41.   return;
  42. end
  43.  
  44. function Walker(url,path)
  45.   print("");
  46.   print("Checking directory "..url)
  47.   local resp = GetTextFromUrl(url)
  48.   local jsonresp = Json.decode(resp);
  49.   local childs = jsonresp.values;
  50.   for i=1,#childs do
  51.     local child = childs[i];
  52.     if child.type == "commit_directory" then
  53.       Walker(child.links.self.href)
  54.     elseif child.type == "commit_file" then
  55.       SaveFileFromURL(child.links.self.href,path..child.path);
  56.     else
  57.       print("Unknown type "..tostring(child.type))
  58.     end
  59.   end
  60. end
  61.  
  62. local mode,address,newName = ...;
  63. if mode ~= nil then
  64.   if mode ~= "file" and mode ~= "dir" then
  65.     error("Invalid mode")
  66.   end
  67.  
  68.   if not http.checkURL(address) then
  69.     error("Invalid URL")
  70.   end
  71.  
  72.   if mode == "file" then
  73.     SaveFileFromURL(address,newName);
  74.   else
  75.     if Json == nil then
  76.       Save("Json",GetTextFromUrl("https://api.bitbucket.org/2.0/repositories/Hiranus0/mcrestapi/src/master/ComputerCraft/Lua/API/Json.lua"));
  77.       os.loadAPI("Json");
  78.     end
  79.     Walker(address,newName);
  80.   end
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement