· 2 years ago · Nov 07, 2022, 01:10 PM
1---@diagnostic disable: undefined-global
2--[[
3 Original Code Pastebin: https://pastebin.com/Mc1CUUQn
4 Git Loader - PasteBin style GitHub Repo Downloader BY: CodingButter#8420
5 Added two functions, one to download with an API key, the other is to reset the API key in settings.
6 MYPASTEBIN: https://pastebin.com/6C6a4E11
7]]
8--#region My Functions
9local function downloadFile(uri, file, headers)
10 if type(uri) ~= "string" and type(file) ~= "string" then
11 error("Both uri and file need to be strings!")
12 end
13 local bool, err = http.checkURL(uri)
14 if not bool then
15 req.close()
16 return false, "Failed to download, err: " .. err
17 end
18 local req, err = http.get(uri, headers)
19 if not req then
20 req.close()
21 return false, "Failed to download, err: " .. err
22 end
23 -- if fs.exists(file) then
24 -- if not fs.delete(file) then
25 -- return false, "Failed to delete file"
26 -- end
27 -- end
28 local file = fs.open(file, "w")
29 if file then
30 file.write(req.readAll())
31 file.close()
32 else
33 req.close()
34 return false, "Failed to open file"
35 end
36 req.close()
37end
38
39local function resetToken()
40 if settings.get("GITHUB_TOKEN") ~= "" then
41 settings.set("GITHUB_TOKEN", "")
42 end
43 settings.save()
44end
45
46--#endregion
47
48local tArgs = { ... }
49local defs = { {
50 name = "username",
51 message = "Github Username:",
52 setting = "GITHUB_USERNAME"
53}, {
54 name = "repo",
55 message = "Github Repository:",
56 setting = "GITHUB_REPO"
57}, {
58 name = "branch",
59 message = "Github Repository Branch:",
60 setting = "GITHUB_BRANCH"
61}, {
62 name = "token",
63 message = "Github API Token(optional):",
64 setting = "GITHUB_TOKEN"
65} }
66local config = {}
67term.clear()
68for i = 1, #defs do
69 local def = defs[i]
70 config[def.name] = tArgs[i] or settings.get(def.setting)
71 if not config[def.name] then
72 term.setCursorPos(1, 1)
73 term.write(def.message)
74 config[def.name] = read()
75 settings.set(def.setting, config[def.name])
76 term.clear()
77 end
78end
79local apiurl = "https://api.github.com/repos/" .. config.username .. "/" .. config.repo .. "/branches/" .. config.branch
80local githubPath = "https://raw.githubusercontent.com/" .. config.username .. "/" .. config.repo .. "/" .. config.branch
81local headers = config.token ~= "" and {
82 Authorization = "token " .. config.token
83} or nil
84
85local getJSON = function(api)
86 local post, err = http.get(api, headers)
87 if err then
88 error(api .. " " .. tostring(err))
89 end
90 local response = post.readAll()
91 return textutils.unserializeJSON(response)
92end
93local WIDTH, HEIGHT = term.getSize()
94local base = getJSON(apiurl)
95if not base.commit then
96 error(base.message)
97end
98settings.save()
99local baseTreeUrl = base.commit.commit.tree.url
100term.setTextColor(colors.white)
101local installText = "--[[ Building File List ]]--"
102term.setCursorPos(WIDTH / 2 - #installText / 2, 1)
103term.write(installText)
104local function buildFiles(url)
105
106 local tree = getJSON(url).tree
107 local tmpTable = {}
108 for k, v in ipairs(tree) do
109 if config.token == "" then
110 sleep(1)
111 end
112 if (v.type == "tree") then
113 tmpTable[v.path] = buildFiles(v.url)
114 else
115 term.setCursorPos(WIDTH / 2 - #v.path / 2, 2)
116 term.clearLine()
117 term.write(v.path)
118 tmpTable[#tmpTable + 1] = v.path
119 end
120 end
121 return tmpTable
122end
123
124local files = buildFiles(baseTreeUrl)
125local totalFiles = 0
126local downloaded = 0
127local function calculateFiles(_tbl)
128 for k, v in pairs(_tbl) do
129 if type(v) == "table" then
130 calculateFiles(v)
131 else
132 totalFiles = totalFiles + 1
133 end
134 end
135end
136
137local fileColors = { colors.white, colors.lightGray, colors.gray }
138local fileNames = {}
139local function downloadFiles(_tbl, dir)
140 local WIDTH, HEIGHT = term.getSize()
141 for k, v in pairs(_tbl) do
142 if type(v) == "table" then
143 downloadFiles(v, dir .. "/" .. k)
144 else
145 local path = dir .. "/" .. v
146 if headers == nil then
147 shell.run("wget", githubPath .. path, path)
148 else
149 print(downloadFile(githubPath .. path, path, headers))
150 end
151 sleep(1)
152 table.insert(fileNames, 1, path)
153 downloaded = downloaded + 1
154 term.clear()
155
156 term.setTextColor(colors.white)
157 local installText = "--[[ Installing BeastCraft ]]--"
158 local progressText =
159 downloaded .. " of " .. totalFiles .. "(" .. math.ceil(100 * (downloaded / totalFiles)) .. "%)"
160 term.setCursorPos(WIDTH / 2 - #installText / 2, 2)
161 term.write(installText)
162 term.setCursorPos(WIDTH / 2 - #progressText / 2, 3)
163 term.write(progressText)
164 for i = 1, 3 do
165 if fileNames[i] then
166 term.setCursorPos(WIDTH / 2 - #fileNames[i] / 2, i + 3)
167 term.setTextColor(fileColors[i])
168 term.write(fileNames[i])
169 end
170 end
171 end
172 end
173 term.setCursorPos(WIDTH / 2 - 3, 7)
174end
175
176calculateFiles(files)
177downloadFiles(files, "")
178resetToken()