local function GetTextFromUrl(address)
local fileHandle = http.get(address);
if fileHandle == nil then
error("Could not execute \"get\" on address: "..address)
end
local text = fileHandle.readAll();
fileHandle.close();
return text;
end
local function Save(name,data)
local file = fs.open(name,"w")
if not file then
error("File "..name.." could not be opened");
end
file.write(data)
file.close()
end
local function GetLastName(str)
local lastsl = string.match(str,'^.*()/');
if string.len(str) == lastsl then
str = str:sub(1, -2)
lastsl = string.match(str,'^.*()/');
end
return string.sub(str,lastsl+1);
end
function SaveFileFromURL(address,newName,path)
if not newName then
newName = GetLastName(address);
end
if path then
if string.len(path) ~= string.match(path,'^.*()/') then
path = path .. "/";
end
newName = path .. newName;
end
print("Saving file "..address.. " as ".. newName)
Save(newName,GetTextFromUrl(address));
return;
end
function Walker(url,path)
print("");
print("Checking directory "..url)
local resp = GetTextFromUrl(url)
local jsonresp = Json.decode(resp);
local childs = jsonresp.values;
for i=1,#childs do
local child = childs[i];
if child.type == "commit_directory" then
Walker(child.links.self.href)
elseif child.type == "commit_file" then
SaveFileFromURL(child.links.self.href,path..child.path);
else
print("Unknown type "..tostring(child.type))
end
end
end
local mode,address,newName = ...;
if mode ~= nil then
if mode ~= "file" and mode ~= "dir" then
error("Invalid mode")
end
if not http.checkURL(address) then
error("Invalid URL")
end
if mode == "file" then
SaveFileFromURL(address,newName);
else
if Json == nil then
Save("Json",GetTextFromUrl("https://api.bitbucket.org/2.0/repositories/Hiranus0/mcrestapi/src/master/ComputerCraft/Lua/API/Json.lua"));
os.loadAPI("Json");
end
Walker(address,newName);
end
end
Comments
0 B
|👍
/👎