OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
2.8 KiB

local shell = require("shell")
local fs = require("filesystem")
local internet = require("internet")
-- local ll = require("liblua")
local args = {...}
-- Error checking
if args[1] == nil then
print("URL must exist")
return
end
-- Stolen from Lua users because I'm bad with RegEx
function trim(s, p)
-- Set to whitespace trim otherwise
if p == nil then
p = "%s"
end
return (s:gsub(string.format("^%s*(.-)%s*$", p, p), "%1"))
end
local function split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
-- Slice table
function slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
local function fixURL(url)
if not string.match(url, "/$") and not string.match(url, "\\.") then
url = url.."/"
end
return url
end
local function makeRequest(url)
local handle = internet.request(url)
local result = ""
for chunk in handle do result = result..chunk end
-- Grab the metatable for the handle. This contains the
-- internal HTTPRequest object.
local mt = getmetatable(handle)
-- The response method grabs the information for
-- the HTTP response code, the response message, and the
-- response headers.
local code, message, headers = mt.__index.response()
if code ~=200 then
return nil, "Bad code received"
end
return url, result
end
local function getFiles(url, result, workDir)
-- Set first initial working directory to current directory
if workDir == nil then
workDir = shell.getWorkingDirectory()
end
-- Loop through results
for i in string.gmatch(result, "href=\".-\"") do
local dir = split(i, "\"")[2]
local cwd = ""
if string.match(dir, ".lua$") then
local subdir = table.concat(slice(split(url), 4), "/")
print(subdir)
shell.setWorkingDirectory(workDir.."/"..subdir)
shell.execute("wget -f "..url..dir)
goto continue
end
cwd = workDir.."/"..trim(dir,"/")
if string.match(dir, "/$")
and not string.match(dir, ".git")
and not string.match(dir, ".vscode")
then
-- Remove any weird files
if fs.exists(cwd) and not fs.isDirectory(cwd) then
fs.remove(cwd)
end
-- Make missing dir
if not fs.exists(cwd) then
fs.makeDirectory(cwd)
end
-- Recurse
shell.setWorkingDirectory(cwd)
getFiles(makeRequest(url..dir))
end
::continue::
end
end
-- Grab all files on the server
local path = ""
if args[2] == nil then
path = shell.getWorkingDirectory()
else
path = args[2]
end
getFiles(makeRequest(fixURL(args[1])))
shell.setWorkingDirectory(path)
-- wget -f http://mc.bashed.rocks:13699/get-all.lua