Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fs = require("filesystem")
- local internet = require("internet")
- local serialization = require("serialization")
- local function GetPastebinID(url_or_id)
- local id = url_or_id:match("https://pastebin%.com/(.+)")
- if not id then
- id = url_or_id:match("https://pastebin%.com/raw/(.+)")
- end
- return id
- end
- local function HttpFetch(url)
- local handle, response = internet.request(url, nil, {
- ["User-Agent"] = "OpenComputers"
- }), ""
- for chunk in handle do
- response = response .. chunk
- end
- return response
- end
- local function GetGithubBlob(user, repo, branch, path)
- return string.format("https://github.com/%s/%s/blob/%s/%s", user, repo, branch, path)
- end
- local download = {}
- function download.download(path, url, alive_time)
- alive_time = alive_time or 86400
- if not fs.exists(path) then
- os.execute("wget ".. url .." ".. path)
- elseif (fs.lastModified(path) / 1000 + alive_time) < (os.time() / 100) then
- os.execute("rm ".. path)
- os.execute("wget ".. url .." ".. path)
- end
- end
- function download.require(path, url, alive_time)
- download.download(path, url, alive_time)
- return require(path)
- end
- function download.load(path, url, alive_time)
- download.download(path, url, alive_time)
- local f = io.open(path, "r")
- if not f then return "" end
- local data = f:read("*a")
- io.close(f)
- return data
- end
- download.pastebin = {}
- function download.pastebin.require(path, url_or_id, alive_time)
- local id = GetPastebinID(url_or_id) or url_or_id
- return download.require(path, "https://pastebin.com/raw/".. id, alive_time)
- end
- function download.pastebin.load(path, url_or_id, alive_time)
- local id = GetPastebinID(url_or_id) or url_or_id
- return download.load(path, "https://pastebin.com/raw/".. id, alive_time)
- end
- download.github = {}
- local is_file, is_dir = {blob = true}, {tree = true}
- function download.github.listrepo(user, repo, branch, cback)
- local url = string.format("https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", user, repo, branch)
- local json = HttpFetch(url)
- local tree = serialization.unserialize(json)
- local function recursive(tree)
- for i, data in ipairs(tree) do
- if is_file[data.type] then
- cback(data.path, data.url)
- elseif is_dir[data.type] then
- recursive(data)
- end
- end
- end
- recursive(tree)
- end
- function download.github.reqirerepo(dir_path, init_file, user, repo, branch, alive_time)
- branch = branch or "master"
- download.github.listrepo(user, repo, branch, function(fpath, url)
- download.download(dir_path .."/".. fpath, url, alive_time)
- end)
- return require(dir_path .."/".. init_file)
- end
- function download.github.reqire(path, user, repo, branch, fpath, alive_time)
- local url = GetGithubBlob(user, repo, branch, fpath)
- return download.require(path, url, alive_time)
- end
- function download.github.load(path, user, repo, branch, fpath, alive_time)
- local url = GetGithubBlob(user, repo, branch, fpath)
- return download.load(path, url, alive_time)
- end
- return download
Add Comment
Please, Sign In to add comment