Last active
July 1, 2021 23:50
-
-
Save BryanHaley/f0c741e30eed522ad0d602c7e24eb129 to your computer and use it in GitHub Desktop.
File Updater Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FILES = { | |
["foofile1"] = "url1", | |
["foofile2"] = "url2" | |
} | |
function update_file(filename, url) | |
local cacheBreak = tostring(math.random(0, 99999)) | |
res, err = http.get(url .. "?breaker=" .. cacheBreak) | |
if not res then error(err) end | |
local code = res.readAll() | |
if not(fs.exists(filename)) | |
then | |
local newFile = fs.open(filename, 'w') | |
newFile.close() | |
end | |
local readFile = fs.open(filename, 'r') | |
local oldCode = readFile.readAll() | |
readFile.close() | |
local file = fs.open(filename, 'w') | |
if oldCode == code | |
then | |
file.write(oldCode) | |
print(filename .. ': NO CHANGES MADE - Same Code') | |
else | |
file.write(code) | |
print(filename .. ': WRITING UPDATE') | |
byteDiff = string.len(code) - string.len(oldCode) | |
if byteDiff >= 0 | |
then | |
print(filename .. ': ' .. tostring(math.abs(byteDiff)) .. ' bytes added') | |
else | |
print(filename .. ': ' .. tostring(math.abs(byteDiff)) .. ' bytes removed') | |
end | |
end | |
file.close() | |
res.close() | |
end | |
math.randomseed(os.time()) | |
for file, url in pairs(FILES) do | |
update_file(file, url) | |
end | |
-- Pastebin: https://pastebin.com/qMpy7hiB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment