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.

124 lines
2.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. local shell = require("shell")
  2. local fs = require("filesystem")
  3. local internet = require("internet")
  4. -- local ll = require("liblua")
  5. local args = {...}
  6. -- Error checking
  7. if args[1] == nil then
  8. print("URL must exist")
  9. return
  10. end
  11. -- Stolen from Lua users because I'm bad with RegEx
  12. function trim(s, p)
  13. -- Set to whitespace trim otherwise
  14. if p == nil then
  15. p = "%s"
  16. end
  17. return (s:gsub(string.format("^%s*(.-)%s*$", p, p), "%1"))
  18. end
  19. local function split(s, sep)
  20. if sep == nil then
  21. sep = "%s"
  22. end
  23. local t = {}
  24. for str in string.gmatch(s, "([^"..sep.."]+)") do
  25. table.insert(t, str)
  26. end
  27. return t
  28. end
  29. -- Slice table
  30. function slice(tbl, first, last, step)
  31. local sliced = {}
  32. for i = first or 1, last or #tbl, step or 1 do
  33. sliced[#sliced+1] = tbl[i]
  34. end
  35. return sliced
  36. end
  37. local function fixURL(url)
  38. if not string.match(url, "/$") and not string.match(url, "\\.") then
  39. url = url.."/"
  40. end
  41. return url
  42. end
  43. local function makeRequest(url)
  44. local handle = internet.request(url)
  45. local result = ""
  46. for chunk in handle do result = result..chunk end
  47. -- Grab the metatable for the handle. This contains the
  48. -- internal HTTPRequest object.
  49. local mt = getmetatable(handle)
  50. -- The response method grabs the information for
  51. -- the HTTP response code, the response message, and the
  52. -- response headers.
  53. local code, message, headers = mt.__index.response()
  54. if code ~=200 then
  55. return nil, "Bad code received"
  56. end
  57. return url, result
  58. end
  59. local function getFiles(url, result, workDir)
  60. -- Set first initial working directory to current directory
  61. if workDir == nil then
  62. workDir = shell.getWorkingDirectory()
  63. end
  64. -- Loop through results
  65. for i in string.gmatch(result, "href=\".-\"") do
  66. local dir = split(i, "\"")[2]
  67. local cwd = ""
  68. if string.match(dir, ".lua$") then
  69. local subdir = table.concat(slice(split(url), 4), "/")
  70. print(subdir)
  71. shell.setWorkingDirectory(workDir.."/"..subdir)
  72. shell.execute("wget -f "..url..dir)
  73. goto continue
  74. end
  75. cwd = workDir.."/"..trim(dir,"/")
  76. if string.match(dir, "/$")
  77. and not string.match(dir, ".git")
  78. and not string.match(dir, ".vscode")
  79. then
  80. -- Remove any weird files
  81. if fs.exists(cwd) and not fs.isDirectory(cwd) then
  82. fs.remove(cwd)
  83. end
  84. -- Make missing dir
  85. if not fs.exists(cwd) then
  86. fs.makeDirectory(cwd)
  87. end
  88. -- Recurse
  89. shell.setWorkingDirectory(cwd)
  90. getFiles(makeRequest(url..dir))
  91. end
  92. ::continue::
  93. end
  94. end
  95. -- Grab all files on the server
  96. local path = ""
  97. if args[2] == nil then
  98. path = shell.getWorkingDirectory()
  99. else
  100. path = args[2]
  101. end
  102. getFiles(makeRequest(fixURL(args[1])))
  103. shell.setWorkingDirectory(path)
  104. -- wget -f http://mc.bashed.rocks:13699/get-all.lua