legg0028

update

Sep 20th, 2018 (edited)
1,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. -- You need to put your api_dev_key here
  2. local api_dev_key = "dfe49e216beea148e6acf8d5149d93f1"
  3. local api_user_key = "ece0976136bed4daa8e72e68dca7fd7f"
  4.  
  5. -- Internal function that parses the pastebin XML response.
  6. local function parse(response)
  7.     local objs = {}
  8.     for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
  9.         local obj = {}
  10.         for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
  11.             obj[key] = value
  12.         end
  13.         objs[#objs+1] = obj
  14.     end
  15.     return objs
  16. end
  17.  
  18. -- Used to be a custom function, but I realized CC-Lua had one already.
  19. local url_encode = textutils.urlEncode
  20.  
  21. -- Fetches a paste from Pastebin.
  22. -- Example: local rc4 = pastebin.get("Rxe673BJ")
  23. --
  24. -- Returns requested paste as a string or an error message.
  25. function get(api_paste_key)
  26.     assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
  27.     local response = http.get("https://pastebin.com/raw/"..url_encode(api_paste_key))
  28.     return response and response.readAll()
  29. end
  30.  
  31. function list(api_user_key, api_results_limit)
  32.     assert(type(api_user_key) == "string", "Enter a valid user key as a string!")
  33.     local urlstr = {
  34.         "api_dev_key=", url_encode(api_dev_key),
  35.         "&api_user_key=", url_encode(api_user_key),
  36.         "&api_option=list"
  37.     }
  38.     if api_results_limit then
  39.         if type(api_results_limit) == "number" then
  40.             if api_results_limit > 1000 then
  41.                 api_results_limit = 1000
  42.             elseif api_results_limit < 1 then
  43.                 api_results_limit = 1
  44.             end
  45.             local len = #urlstr
  46.             urlstr[len+1] = "&api_results_limit="
  47.             urlstr[len+2] = url_encode(api_results_limit)
  48.         else
  49.             io.stderr:write("Results limit must be a number!\n")
  50.         end
  51.     end
  52.     local res = http.post("https://pastebin.com/api/api_post.php", table.concat(urlstr))
  53.     local body = res and res.readAll()
  54.     if body and string.find(body, "<") then
  55.         return parse(body)
  56.     else
  57.         return {}
  58.     end
  59. end
  60.  
  61.  
  62. local allPastes = list(api_user_key)
  63. local api_user_name = "legg0028"
  64. local folderName = api_user_name
  65.  
  66. if peripheral.isPresent("bottom") and peripheral.getType("bottom") == "drive" then
  67.   print("Disk drive found, updating to disk.")
  68.   sleep(1)
  69.   if fs.exists("/disk/" ..folderName.. "/updates") then --"remove the old updates folder"
  70.     fs.delete("/disk/" ..folderName.. "/updates")
  71.   end
  72.   if fs.exists("/disk/" ..folderName) then --"remove the old folder"
  73.     fs.delete("/disk/" ..folderName)
  74.   end
  75.   fs.makeDir("disk/" ..folderName) --"make a new updates folder"
  76.   downloadDir = "disk/" ..folderName.. "/"
  77. else
  78.   print("Updating computer.")
  79.   sleep(1)
  80.   if fs.exists("/" ..folderName.. "/updates") then --"remove the old updates folder"
  81.     fs.delete("/" ..folderName.. "/updates")
  82.   end
  83.   fs.makeDir("/" ..folderName) --"make a new updates folder"
  84.   downloadDir = folderName.. "/"
  85. end
  86.  
  87. for i, paste in ipairs(allPastes) do
  88.   local key = paste["key"]
  89.   local title = paste["title"] or ("untitled_"..key)
  90.   local isPrivate = paste["private"] == "2"
  91.  
  92.   if isPrivate then
  93.     print("Skipping private paste: " .. title)
  94.   else
  95.     local code = get(key)
  96.     if code then
  97.       local h = fs.open(downloadDir..title, "w")
  98.       h.write(code)
  99.       h.close()
  100.       term.setCursorPos(1,4)
  101.       print("\""..title.."\" successfully downloaded")
  102.     else
  103.       print("Failed to download: " .. title .. " (key: " .. key .. ")")
  104.     end
  105.   end
  106. end
  107.  
  108. term.setCursorPos(1,4)
  109. print("Successfully updated "..#allPastes.." programs.")
  110. if downloadDir == "disk/" ..folderName then
  111.   fs.delete("/" ..folderName)
  112.   fs.copy("disk/" ..folderName, folderName)
  113.   print("Successfully updated computer.")
  114. end
  115. if downloadDir == "disk/" ..folderName.. "/" then
  116.   print("Setting disk startup program.")
  117.   if fs.exists("/disk/startup") then
  118.     fs.delete("/disk/startup")
  119.   end
  120.   fs.copy("disk/" ..folderName.. "/refresh", "disk/startup")
  121.   if fs.exists("/disk/username") then
  122.     fs.delete("/disk/username")
  123.   end
  124.   fs.copy("username", "disk/username")
  125.   print("Done.")
  126. end
Advertisement
Add Comment
Please, Sign In to add comment