Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- You need to put your api_dev_key here
- local api_dev_key = "dfe49e216beea148e6acf8d5149d93f1"
- local api_user_key = "ece0976136bed4daa8e72e68dca7fd7f"
- -- Internal function that parses the pastebin XML response.
- local function parse(response)
- local objs = {}
- for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
- local obj = {}
- for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
- obj[key] = value
- end
- objs[#objs+1] = obj
- end
- return objs
- end
- -- Used to be a custom function, but I realized CC-Lua had one already.
- local url_encode = textutils.urlEncode
- -- Fetches a paste from Pastebin.
- -- Example: local rc4 = pastebin.get("Rxe673BJ")
- --
- -- Returns requested paste as a string or an error message.
- function get(api_paste_key)
- assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
- local response = http.get("https://pastebin.com/raw/"..url_encode(api_paste_key))
- return response and response.readAll()
- end
- function list(api_user_key, api_results_limit)
- assert(type(api_user_key) == "string", "Enter a valid user key as a string!")
- local urlstr = {
- "api_dev_key=", url_encode(api_dev_key),
- "&api_user_key=", url_encode(api_user_key),
- "&api_option=list"
- }
- if api_results_limit then
- if type(api_results_limit) == "number" then
- if api_results_limit > 1000 then
- api_results_limit = 1000
- elseif api_results_limit < 1 then
- api_results_limit = 1
- end
- local len = #urlstr
- urlstr[len+1] = "&api_results_limit="
- urlstr[len+2] = url_encode(api_results_limit)
- else
- io.stderr:write("Results limit must be a number!\n")
- end
- end
- local res = http.post("https://pastebin.com/api/api_post.php", table.concat(urlstr))
- local body = res and res.readAll()
- if body and string.find(body, "<") then
- return parse(body)
- else
- return {}
- end
- end
- local allPastes = list(api_user_key)
- local api_user_name = "legg0028"
- local folderName = api_user_name
- if peripheral.isPresent("bottom") and peripheral.getType("bottom") == "drive" then
- print("Disk drive found, updating to disk.")
- sleep(1)
- if fs.exists("/disk/" ..folderName.. "/updates") then --"remove the old updates folder"
- fs.delete("/disk/" ..folderName.. "/updates")
- end
- if fs.exists("/disk/" ..folderName) then --"remove the old folder"
- fs.delete("/disk/" ..folderName)
- end
- fs.makeDir("disk/" ..folderName) --"make a new updates folder"
- downloadDir = "disk/" ..folderName.. "/"
- else
- print("Updating computer.")
- sleep(1)
- if fs.exists("/" ..folderName.. "/updates") then --"remove the old updates folder"
- fs.delete("/" ..folderName.. "/updates")
- end
- fs.makeDir("/" ..folderName) --"make a new updates folder"
- downloadDir = folderName.. "/"
- end
- for i, paste in ipairs(allPastes) do
- local key = paste["key"]
- local title = paste["title"] or ("untitled_"..key)
- local isPrivate = paste["private"] == "2"
- if isPrivate then
- print("Skipping private paste: " .. title)
- else
- local code = get(key)
- if code then
- local h = fs.open(downloadDir..title, "w")
- h.write(code)
- h.close()
- term.setCursorPos(1,4)
- print("\""..title.."\" successfully downloaded")
- else
- print("Failed to download: " .. title .. " (key: " .. key .. ")")
- end
- end
- end
- term.setCursorPos(1,4)
- print("Successfully updated "..#allPastes.." programs.")
- if downloadDir == "disk/" ..folderName then
- fs.delete("/" ..folderName)
- fs.copy("disk/" ..folderName, folderName)
- print("Successfully updated computer.")
- end
- if downloadDir == "disk/" ..folderName.. "/" then
- print("Setting disk startup program.")
- if fs.exists("/disk/startup") then
- fs.delete("/disk/startup")
- end
- fs.copy("disk/" ..folderName.. "/refresh", "disk/startup")
- if fs.exists("/disk/username") then
- fs.delete("/disk/username")
- end
- fs.copy("username", "disk/username")
- print("Done.")
- end
Advertisement
Add Comment
Please, Sign In to add comment