Advertisement
promitheus_sal

pastebinlib.lua

Dec 6th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. --[[
  2. (C) PROMETHEUS TECHNLOGIES 2022
  3.  
  4. pastebinlib.lua v1.0
  5. Lua pastebin interface library
  6. ]]
  7.  
  8. local function extractId(paste)
  9.     if not paste then return nil end
  10.     local patterns = {
  11.         "^([%a%d]+)$",
  12.         "^https?://pastebin.com/([%a%d]+)$",
  13.         "^pastebin.com/([%a%d]+)$",
  14.         "^https?://pastebin.com/raw/([%a%d]+)$",
  15.         "^pastebin.com/raw/([%a%d]+)$",
  16.     }
  17.  
  18.     for i = 1, #patterns do
  19.         local code = paste:match(patterns[i])
  20.         if code then return code end
  21.     end
  22.  
  23.     return nil
  24. end
  25.  
  26. local function get(url)
  27.     local paste = extractId(url)
  28.     if not paste then
  29.         io.stderr:write("Invalid pastebin code.\n")
  30.         io.write("The code is the ID at the end of the pastebin.com URL.\n")
  31.         return
  32.     end
  33.  
  34. --    write("Connecting to pastebin.com... ")
  35.     -- Add a cache buster so that spam protection is re-checked
  36.     local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
  37.     local response, err = http.get(
  38.         "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
  39.     )
  40.  
  41.     if response then
  42.         -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  43.         local headers = response.getResponseHeaders()
  44.         if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  45. --            io.stderr:write("Failed.\n")
  46. --            print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
  47.             error("BLOCKED")
  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.         error(err)
  60.     end
  61. end
  62.  
  63. local function put(sName,sText)
  64.     -- POST the contents to pastebin
  65. --    write("Connecting to pastebin.com... ")
  66.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  67.     local response = http.post(
  68.         "https://pastebin.com/api/api_post.php",
  69.         "api_option=paste&" ..
  70.         "api_dev_key=" .. key .. "&" ..
  71.         "api_paste_format=lua&" ..
  72.         "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
  73.         "api_paste_code=" .. textutils.urlEncode(sText)
  74.     )
  75.  
  76.     if response then
  77. --        print("Success.")
  78.  
  79.         local sResponse = response.readAll()
  80.         response.close()
  81.  
  82.         local sCode = string.match(sResponse, "[^/]+$")
  83. --        print("Uploaded as " .. sResponse)
  84. --        print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
  85.         return sCode
  86.     else
  87. --        print("Failed.")
  88.         error("FAILED")
  89.     end
  90. end
  91.  
  92. local t={}
  93. t.put = put
  94. t.get = get
  95.  
  96. return t
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement