Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ This is a Lua object to handle interacting with the Pastebin API via HTTP.
- To use this in a program do the following (assuming this file is in the CraftOS root directory)
- class = assert(loadfile("pastebin_obj"))
- class()
- Object = Pastebin:new()
- Member functions can now be called by doing Object:function(arg1,arg2,...)
- Member variables can be set and accessed with Object.member_variable
- See http://www.computercraft.info/forums2/index.php?/topic/8393-oop-in-lua/ for more info on the OOP practices
- See http://www.computercraft.info/forums2/index.php?/topic/10450-handling-errors-creating-controlled-errors-creating-a-bsod/
- for information on how to handle errors.
- ]]
- Pastebin = {}
- Pastebin.__index = Pastebin
- -- Member variables being declared
- Pastebin.api_dev_key = ""
- Pastebin.api_user_key = ""
- -- Member functions
- -- Constructor
- function Pastebin.new(self, dev_key)
- if not http then
- error("Error constructing Pastebin object. Pastebin requires http API. Set enableAPI_http to 1 in mod_ComputerCraft.cfg")
- end
- local PastebinObject = {}
- setmetatable(PastebinObject, Pastebin)
- self.api_dev_key = dev_key
- return PastebinObject
- end
- -- This attempts to get an API key for the user and returns true on success, false on failure.
- function Pastebin.login(self, user_name, user_password)
- local response = http.post(
- "http://pastebin.com/api/api_login.php",
- "api_dev_key="..textutils.urlEncode(self.api_dev_key).."&"..
- "api_user_name="..textutils.urlEncode(user_name).."&"..
- "api_user_password="..textutils.urlEncode(user_password)
- )
- if response then
- local sResponse = response.readAll()
- response.close()
- self.api_user_key = string.match(sResponse,"[^/]+$")
- return true
- else
- return false
- end
- end
- -- gets a raw paste off Pastebin, and returns it.
- function Pastebin.get_raw(self, sCode)
- local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(sCode))
- if response then
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- error("Bad response from Pastebin.com. Unable to get data.")
- end
- end
- -- used to put stuff on pastebin
- --[[ argument format is Obj.put(paste_code, paste_name, paste_format, paste_private, paste_expire)
- paste_code is the text of the paste
- paste_name is the name of the paste
- paste_format is the syntax highlighting, which is "text" by default (see Pastebin.com API documentation for other options)
- paste_private is the privacy code, which is "0" by default (0=public, 1=unlisted, 2=private, if using 2 then you must be logged in before calling this function)
- paste_expire is the expire for the paste (see Pastebin.com API documentation for other options)
- http://pastebin.com/api
- ]]
- function Pastebin.put(...)
- local args = { ... }
- local self = args[1]
- local paste_code = args[2]
- local paste_name = args[3] or ""
- local paste_format = args[4] or "text"
- local paste_private = args[5] or "0"
- local paste_expire_date = args[6] or "N"
- if #args < 2 or #args > 6 then
- error("Invalid number of arguments provided to Pastebin.put()")
- end
- if paste_code == "" then
- error("No paste_code provided to Pastebin.put method.")
- end
- local url = "http://pastebin.com/api/api_post.php"
- local params = "api_dev_key="..textutils.urlEncode(self.api_dev_key).."&"..
- "api_option=paste&"..
- "api_paste_code="..textutils.urlEncode(paste_code)
- if self.api_user_key ~= "" then
- params = params.."&api_user_key="..textutils.urlEncode(self.api_user_key)
- end
- if paste_name ~= "" then
- params = params.."&api_paste_name="..textutils.urlEncode(paste_name)
- end
- params = params.."&api_paste_format="..textutils.urlEncode(paste_format)
- if paste_private == "0" then
- -- do nothing
- elseif paste_private == "1" then
- params = params.."&api_paste_private="..textutils.urlEncode(paste_private)
- elseif paste_private == "2" then
- if self.api_user_key ~= "" then
- params = params.."&api_paste_private="..textutils.urlEncode(paste_private)
- else
- error("User must be logged in before they can create a private paste.")
- end
- else
- error("Invalid value provided to Pastebin.put(). paste_private can be the string 0, 1, or 2.")
- end
- if paste_expire_date ~= "N" and paste_expire_date ~= "10M" and paste_expire_date ~= "1H" and paste_expire_date ~= "1D" and paste_expire_date ~= "1W" and paste_expire_date ~= "2W" and paste_expire_date ~= "1M" then
- error("Invalid argument provided to Pastebin.put(). paste_expire date can be N, 10M, 1H, 1D, 1W, 2W, or 1M.")
- else
- params = params.."&api_paste_expire="..textutils.urlEncode(paste_expire_date)
- end
- local response = http.post(url,params)
- if response then
- local sResponse = response.readAll()
- response.close()
- return string.match(sResponse,"[^/]+$")
- else
- error("Bad response from Pastebin. Unable to put data on server.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment