fatboychummy

pget

Aug 5th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | None | 0 0
  1. --[[
  2. In progress.
  3. Will be a sort of easy pastebin updater thing once I'm done
  4. not really much to it
  5. ]]
  6. --[[
  7. local filesToGet = {
  8.   {
  9.     name = "startup",
  10.     loc = "blabla",
  11.   }
  12. }
  13. ]]
  14. local filesToGet = dofile("filesToGet")
  15.  
  16.  
  17. local nFiles = #filesToGet
  18. local sFiles = tostring(#filesToGet)
  19.  
  20.  
  21. local function PE(i,err)
  22.   local oldC = term.getTextColor()
  23.   term.setTextColor(colors.red)
  24.   print("Failed to download "..filesToGet[i].name.." due to: "..err)
  25.   term.setTextColor(oldC)
  26. end
  27.  
  28. print("Deleting required files...")
  29. for i = 1,nFiles do
  30.   print("Deleting "..filesToGet[i].name.."...")
  31.   fs.delete(filesToGet[i].name)
  32. end
  33. print("Done.")
  34. print("Deleted "..sFiles.." files.")
  35. print("Attempting to download "..sFiles.." files.")
  36.  
  37. for i = 1,nFiles do
  38.   print()
  39.   print("Attempting to download file from Pastebin: " .. filesToGet[i].name)
  40.   print("Connecting to Pastebin...")
  41.   local h1 = http.get("https://pastebin.com/raw/" .. textutils.urlEncode(filesToGet[i].loc) .. "?cb=" .. tostring(math.random(0, 2^30)))
  42.   if h1 then
  43.     print("Connected!")
  44.     print("Opening file: "..filesToGet[i].name)
  45.     local h2 = fs.open(filesToGet[i].name,"w")
  46.     if h2 then
  47.       print("File successfully opened!")
  48.       h2.write(h1.readAll())
  49.       h2.close()
  50.       print("Download success!")
  51.     else
  52.       PE(i,"Failed to open file.")
  53.     end
  54.     h1.close()
  55.   else
  56.     PE(i,"Failed to connect to pastebin.")
  57.   end
  58. end
Add Comment
Please, Sign In to add comment