Repository navigation
Expand file tree
/
Copy pathUpdate.lua
More file actions
82 lines (63 loc) · 1.57 KB
/
Copy pathUpdate.lua
File metadata and controls
82 lines (63 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
local OWNER = "Str8Anonymous"
local REPO = "CC-Tweaked-stuff"
local BRANCH = "main"
local FOLDER = "MiningScripts"
local function request(url, customHeaders)
local headers = {
["User-Agent"] = "CC-Tweaked",
["Cache-Control"] = "no-cache",
}
if customHeaders then
for k, v in pairs(customHeaders) do
headers[k] = v
end
end
local response, err = http.get(url, headers)
if not response then
error("HTTP failed " .. tostring(err), 0)
end
local body = response.readAll()
response.close()
return body
end
local function download(url, outputPath)
local cacheBuster = tostring(os.epoch("utc"))
local sep = "?"
if url:find("?") then
sep = "&"
end
local finalUrl = url .. sep .. "t=" .. cacheBuster
print("URL " .. finalUrl)
local headers = {
["Accept"] = "application/vnd.github.v3.raw",
}
local body = request(finalUrl, headers)
if fs.exists(outputPath) then
fs.delete(outputPath)
end
local file = fs.open(outputPath, "w")
file.write(body)
file.close()
end
local apiUrl = ("https://api.github.com/repos/%s/%s/contents/%s?ref=%s&t=%s"):format(
OWNER,
REPO,
FOLDER,
BRANCH,
tostring(os.epoch("utc"))
)
print("Checking GitHub...")
local body = request(apiUrl)
local entries = textutils.unserializeJSON(body)
if type(entries) ~= "table" then
error("GitHub did not return a file list", 0)
end
local count = 0
for _, entry in ipairs(entries) do
if entry.type == "file" and entry.name:match("%.lua$") then
print("Downloading " .. entry.name)
download(entry.url, entry.name)
count = count + 1
end
end
print("Done. Downloaded " .. count .. " file(s).")