Advertisement
massacring

LoadFilesLib

Jun 18th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local function extractId(paste)
  2.     local patterns = {
  3.         "^([%a%d]+)$",
  4.         "^https?://pastebin.com/([%a%d]+)$",
  5.         "^pastebin.com/([%a%d]+)$",
  6.         "^https?://pastebin.com/raw/([%a%d]+)$",
  7.         "^pastebin.com/raw/([%a%d]+)$",
  8.     }
  9.  
  10.     for i = 1, #patterns do
  11.         local code = paste:match(patterns[i])
  12.         if code then return code end
  13.     end
  14.  
  15.     return nil
  16. end
  17.  
  18. local function getPastebin(url)
  19.     local paste = extractId(url)
  20.     if not paste then
  21.         io.stderr:write("Invalid pastebin code.\n")
  22.         io.write("The code is the ID at the end of the pastebin.com URL.\n")
  23.         return
  24.     end
  25.  
  26.     write("Connecting to pastebin.com... ")
  27.     -- Add a cache buster so that spam protection is re-checked
  28.     local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
  29.     local response, err = http.get(
  30.         "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
  31.     )
  32.  
  33.     if response then
  34.         -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  35.         local headers = response.getResponseHeaders()
  36.         if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  37.             io.stderr:write("Failed.\n")
  38.             print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
  39.             return
  40.         end
  41.  
  42.         print("Success.")
  43.  
  44.         local sResponse = response.readAll()
  45.         response.close()
  46.         return sResponse
  47.     else
  48.         io.stderr:write("Failed.\n")
  49.         print(err)
  50.     end
  51. end
  52.  
  53. local function loadFile(id, name)
  54.     sleep(0.3)
  55.     local sPath = shell.resolve(name)
  56.     local res = getPastebin(id)
  57.     if res then
  58.         local file = fs.open(sPath, "w")
  59.         file.write(res)
  60.         file.close()
  61.  
  62.         print("Downloaded as " .. name)
  63.     end
  64. end
  65.  
  66. return { loadFile = loadFile }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement