Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Created April 11, 2023 18:33
Show Gist options
  • Save appgurueu/083944e26709e35dbaf5042ceee3ef8b to your computer and use it in GitHub Desktop.
Save appgurueu/083944e26709e35dbaf5042ceee3ef8b to your computer and use it in GitHub Desktop.
Dump CDB into the current directory using `wget`
#!/usr/bin/env luajit
-- See https://content.minetest.net/help/api/
local http_request = require"http.request"
local lunajson = require"lunajson"
do
-- URI escaping utilities
-- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
local uri_unescaped_chars = {}
for char in ("-_.!~*'()"):gmatch(".") do
uri_unescaped_chars[char] = true
end
local function add_unescaped_range(from, to)
for byte = from:byte(), to:byte() do
uri_unescaped_chars[string.char(byte)] = true
end
end
add_unescaped_range("0", "9")
add_unescaped_range("a", "z")
add_unescaped_range("A", "Z")
local function encode(str, allowed_chars)
return str:gsub(".", function(char)
if allowed_chars[char] then
return char
end
return ("%%%02X"):format(char:byte())
end)
end
function string:encode_uri_component()
return encode(self, uri_unescaped_chars)
end
end
local function get_body(uri)
local headers, stream = assert(http_request.new_from_uri(uri):go())
local body, err = stream:get_body_as_string()
if not body then
print(err)
return
end
if headers:get":status" ~= "200" then
print(body)
return
end
return body
end
local packages = lunajson.decode(get_body"https://content.minetest.net/api/packages/")
for index, package in ipairs(packages) do
local path = package.author:encode_uri_component() .. "/" .. package.name
print("Downloading releases of " .. path .. ": " .. index .. " of " .. #packages)
local releases = lunajson.decode(get_body("https://content.minetest.net/api/packages/" .. path .. "/releases/"))
for _, release in ipairs(releases) do
assert(not (path:find"'" or release.url:find"'" or release.release_date:find"'"))
os.execute(("mkdir -p '%s'"):format(path))
os.execute(("wget --tries 10 -P '%s' -O '%s.zip' 'https://content.minetest.net%s'"):format(path, release.release_date, release.url))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment