Advertisement
BruceWplays

pastebin silent V1

Feb 14th, 2023 (edited)
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. function get(tArgs)
  2.     local function printUsage()
  3.         -- print("Usages:")
  4.         -- print("pastebin put <filename>")
  5.         -- print("pastebin get <code> <filename>")
  6.         -- print("pastebin run <code> <arguments>")
  7.     end
  8.  
  9.     --local tArgs = { ... }
  10.     local function extractId(paste)
  11.         local patterns = {
  12.             "^([%a%d]+)$",
  13.             "^https?://pastebin.com/([%a%d]+)$",
  14.             "^pastebin.com/([%a%d]+)$",
  15.             "^https?://pastebin.com/raw/([%a%d]+)$",
  16.             "^pastebin.com/raw/([%a%d]+)$",
  17.         }
  18.  
  19.         for i = 1, #patterns do
  20.             local code = paste:match(patterns[i])
  21.             if code then return code end
  22.         end
  23.  
  24.         return nil
  25.     end
  26.  
  27.     local function get(url)
  28.         local paste = extractId(url)
  29.         if not paste then
  30.             -- io.stderr:write("Invalid pastebin code.\n")
  31.             -- io.write("The code is the ID at the end of the pastebin.com URL.\n")
  32.             return
  33.         end
  34.  
  35.         -- write("Connecting to pastebin.com... ")
  36.         -- Add a cache buster so that spam protection is re-checked
  37.         local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
  38.         local response, err = http.get(
  39.             "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
  40.         )
  41.  
  42.         if response then
  43.             -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  44.             local headers = response.getResponseHeaders()
  45.             if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  46.                 -- io.stderr:write("Failed.\n")
  47.                 print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
  48.                 return
  49.             end
  50.  
  51.             -- print("Success.")
  52.  
  53.             local sResponse = response.readAll()
  54.             response.close()
  55.             return sResponse
  56.         else
  57.             -- io.stderr:write("Failed.\n")
  58.             -- print(err)
  59.         end
  60.     end
  61.  
  62.     --get--
  63.     -------
  64.     -------
  65.     -- Download a file from pastebin.com
  66.     if #tArgs < 3 then
  67.         --printUsage()
  68.         --return
  69.     end
  70.  
  71.     -- Determine file to download
  72.     local sCode = tArgs[1]
  73.     local sFile = tArgs[2]
  74.     local sjpath = tArgs[3]
  75.     --local sPath = --shell.resolve(sFile)
  76.     local sPath = sjpath.."/"..sFile
  77.     if fs.exists(sPath) then
  78.         -- print("File already exists")
  79.         return
  80.     end
  81.  
  82.     -- GET the contents from pastebin
  83.     local res = get(sCode)
  84.     if res then
  85.         local file = fs.open(sPath, "w")
  86.         file.write(res)
  87.         file.close()
  88.  
  89.         -- print("Downloaded as " .. sFile)
  90.     end
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement