Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- (C) PROMETHEUS TECHNLOGIES 2022
- pastebinlib.lua v1.0
- Lua pastebin interface library
- ]]
- local function extractId(paste)
- if not paste then return nil end
- local patterns = {
- "^([%a%d]+)$",
- "^https?://pastebin.com/([%a%d]+)$",
- "^pastebin.com/([%a%d]+)$",
- "^https?://pastebin.com/raw/([%a%d]+)$",
- "^pastebin.com/raw/([%a%d]+)$",
- }
- for i = 1, #patterns do
- local code = paste:match(patterns[i])
- if code then return code end
- end
- return nil
- end
- local function get(url)
- local paste = extractId(url)
- if not paste then
- io.stderr:write("Invalid pastebin code.\n")
- io.write("The code is the ID at the end of the pastebin.com URL.\n")
- return
- end
- -- write("Connecting to pastebin.com... ")
- -- Add a cache buster so that spam protection is re-checked
- local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
- local response, err = http.get(
- "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
- )
- if response then
- -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
- local headers = response.getResponseHeaders()
- if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
- -- io.stderr:write("Failed.\n")
- -- print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
- error("BLOCKED")
- return
- end
- -- print("Success.")
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- -- io.stderr:write("Failed.\n")
- -- print(err)
- error(err)
- end
- end
- local function put(sName,sText)
- -- POST the contents to pastebin
- -- write("Connecting to pastebin.com... ")
- local key = "0ec2eb25b6166c0c27a394ae118ad829"
- local response = http.post(
- "https://pastebin.com/api/api_post.php",
- "api_option=paste&" ..
- "api_dev_key=" .. key .. "&" ..
- "api_paste_format=lua&" ..
- "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
- "api_paste_code=" .. textutils.urlEncode(sText)
- )
- if response then
- -- print("Success.")
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match(sResponse, "[^/]+$")
- -- print("Uploaded as " .. sResponse)
- -- print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
- return sCode
- else
- -- print("Failed.")
- error("FAILED")
- end
- end
- local t={}
- t.put = put
- t.get = get
- return t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement