Belzebub

/lib/downloader.lua

Dec 7th, 2021 (edited)
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local internet = require("internet")
  3. local serialization = require("serialization")
  4.  
  5. local function GetPastebinID(url_or_id)
  6.     local id = url_or_id:match("https://pastebin%.com/(.+)")
  7.     if not id then
  8.         id = url_or_id:match("https://pastebin%.com/raw/(.+)")
  9.     end
  10.  
  11.     return id
  12. end
  13.  
  14. local function HttpFetch(url)
  15.     local handle, response = internet.request(url, nil, {
  16.         ["User-Agent"] = "OpenComputers"
  17.     }), ""
  18.  
  19.     for chunk in handle do
  20.         response = response .. chunk
  21.     end
  22.  
  23.     return response
  24. end
  25.  
  26. local function GetGithubBlob(user, repo, branch, path)
  27.     return string.format("https://github.com/%s/%s/blob/%s/%s", user, repo, branch, path)
  28. end
  29.  
  30. local download = {}
  31.  
  32. function download.download(path, url, alive_time)
  33.     alive_time = alive_time or 86400
  34.  
  35.     if not fs.exists(path) then
  36.         os.execute("wget ".. url .." ".. path)
  37.     elseif (fs.lastModified(path) / 1000 + alive_time) < (os.time() / 100) then
  38.         os.execute("rm ".. path)
  39.         os.execute("wget ".. url .." ".. path)
  40.     end
  41. end
  42.  
  43. function download.require(path, url, alive_time)
  44.     download.download(path, url, alive_time)
  45.     return require(path)
  46. end
  47.  
  48. function download.load(path, url, alive_time)
  49.     download.download(path, url, alive_time)
  50.  
  51.     local f = io.open(path, "r")
  52.     if not f then return "" end
  53.     local data = f:read("*a")
  54.     io.close(f)
  55.     return data
  56. end
  57.  
  58. download.pastebin = {}
  59.  
  60. function download.pastebin.require(path, url_or_id, alive_time)
  61.     local id = GetPastebinID(url_or_id) or url_or_id
  62.     return download.require(path, "https://pastebin.com/raw/".. id, alive_time)
  63. end
  64.  
  65. function download.pastebin.load(path, url_or_id, alive_time)
  66.     local id = GetPastebinID(url_or_id) or url_or_id
  67.     return download.load(path, "https://pastebin.com/raw/".. id, alive_time)
  68. end
  69.  
  70. download.github = {}
  71.  
  72. local is_file, is_dir = {blob = true}, {tree = true}
  73.  
  74. function download.github.listrepo(user, repo, branch, cback)
  75.     local url = string.format("https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1", user, repo, branch)
  76.     local json = HttpFetch(url)
  77.     local tree = serialization.unserialize(json)
  78.  
  79.     local function recursive(tree)
  80.         for i, data in ipairs(tree) do
  81.             if is_file[data.type] then
  82.                 cback(data.path, data.url)
  83.             elseif is_dir[data.type] then
  84.                 recursive(data)
  85.             end
  86.         end
  87.     end
  88.  
  89.     recursive(tree)
  90. end
  91.  
  92. function download.github.reqirerepo(dir_path, init_file, user, repo, branch, alive_time)
  93.     branch = branch or "master"
  94.  
  95.     download.github.listrepo(user, repo, branch, function(fpath, url)
  96.         download.download(dir_path .."/".. fpath, url, alive_time)
  97.     end)
  98.  
  99.     return require(dir_path .."/".. init_file)
  100. end
  101.  
  102. function download.github.reqire(path, user, repo, branch, fpath, alive_time)
  103.     local url = GetGithubBlob(user, repo, branch, fpath)
  104.     return download.require(path, url, alive_time)
  105. end
  106.  
  107. function download.github.load(path, user, repo, branch, fpath, alive_time)
  108.     local url = GetGithubBlob(user, repo, branch, fpath)
  109.     return download.load(path, url, alive_time)
  110. end
  111.  
  112. return download
Add Comment
Please, Sign In to add comment