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.
- WHAT THIS THING CAN DO
- Gets a user API key from Pastebin.com so the user can be logged in
- allows you to create pastes (anonymously, and under a username, some arguments require the user to be logged in)
- allows you to get a paste from Pastebin.com as a fs.open() handle
- allows you to get a raw XML list of a users posts off Pastebin.com as a fs.open() handle (must be logged in)
- allows you to get a list of a users posts in table form (must be logged in)
- allows you to delete a paste (must be logged in)
- ]]
- 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.
- -- Returns a http handle that uses handle.close(), handle.readLine() and handle.readAll() to work with.
- -- be sure to close the handle when you're done!
- function Pastebin.get_raw(self, sCode)
- local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(sCode))
- if response then
- -- the response should be saved in a member variable, and a bool returned
- return response
- else
- error("Bad response from Pastebin.com. Unable to get data.")
- end
- end
- -- same as put(), but paste_code is replaced by a file path.
- function Pastebin.put_file(...)
- local args = { ... }
- local self = args[1]
- local sPath = 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 not fs.exists( sPath ) or fs.isDir( sPath ) then
- error( "No such file" )
- end
- -- Read in the file
- local sName = fs.getName( sPath )
- local file = fs.open( sPath, "r" )
- local sText = file.readAll()
- file.close()
- return self:put(sText,paste_name,paste_format,paste_private,paste_expire_date)
- 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 (required)
- paste_name is the name of the paste (optional, use "" to leave blank)
- 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
- returns the code of the created paste
- ]]
- 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
- -- fetches and returns the list of posts for the logged in user. Returns a http handle that uses handle.close(), handle.readLine() and handle.readAll() to work with.
- -- be sure to close the handle when you're done!
- function Pastebin.list_raw(self,results_limit)
- if self.api_user_key == "" then
- error("User must be logged in before attempting to retrieve list of pastes.")
- end
- local sLimit = results_limit or "50"
- if sLimit ~= "50" then
- if tonumber(sLimit) < 1 or tonumber(sLimit) > 1000 then
- error("Invalid argument provided to Pastebin.list(). results_limit must be a string integer between 1 and 1000.")
- end
- end
- local params = "api_dev_key="..textutils.urlEncode(self.api_dev_key).."&api_user_key="..textutils.urlEncode(self.api_user_key).."&api_option=list&api_results_limit="..textutils.urlEncode(sLimit)
- local response = http.post("http://pastebin.com/api/api_post.php",params)
- if response then
- -- the response should be saved in a member variable, and a bool returned.
- return response
- else
- error("Bad response from Pastebin. Unable to retirieve list.")
- end
- end
- --gets a list of a users posts, and returns it in a 2-dimensional table.
- function Pastebin.list_table(self, results_limit)
- local tResult = {}
- local raw_list = self:list_raw(results_limit)
- local index = 0
- local sLine = raw_list.readLine()
- while sLine do
- tResult[index] = {}
- tResult[index]["paste_key"] = string.sub(raw_list.readLine(),12,-13)
- tResult[index]["paste_date"] = string.sub(raw_list.readLine(),13,-14)
- tResult[index]["paste_title"] = string.sub(raw_list.readLine(),14,-15)
- tResult[index]["paste_size"] = string.sub(raw_list.readLine(),13,-14)
- tResult[index]["paste_expire_date"] = string.sub(raw_list.readLine(),20,-21)
- tResult[index]["paste_private"] = string.sub(raw_list.readLine(),16,-17)
- tResult[index]["paste_format_long"] = string.sub(raw_list.readLine(),20,-21)
- tResult[index]["paste_format_short"] = string.sub(raw_list.readLine(),21,-22)
- tResult[index]["paste_url"] = string.sub(raw_list.readLine(),12,-13)
- tResult[index]["paste_hits"] = string.sub(raw_list.readLine(),13,-14)
- sLine = raw_list.readLine()
- sLine = raw_list.readLine()
- index = index + 1
- end
- raw_list.close()
- return tResult
- end
- -- deletes a paste off Pastebin.com (must be logged in)
- function Pastebin.delete(self,code)
- if self.api_user_key == "" then
- print("User must be logged in before attempting to delete a paste.")
- end
- local url = "http://pastebin.com/api/api_post.php"
- local params = "api_dev_key="..textutils.urlEncode(self.api_dev_key)..
- "&api_user_key="..textutils.urlEncode(self.api_user_key)..
- "&api_paste_key="..textutils.urlEncode(code)..
- "&api_option=delete"
- local response = http.post(url,params)
- if response then
- local sResponse = response.readAll()
- response.close()
- if sResponse == "Paste Removed" then
- return true
- else
- error("Unable to remove paste. Pastebin says \""..sResponse.."\"")
- end
- else
- error("Bad resposne from Pastebin. Unable to delete paste")
- end
- end
- -- allows you to download a paste into a file.
- function Pastebin.get_file(self,sCode,sPath)
- if fs.exists(sPath) then
- error("File already exists")
- end
- local response = self:get_raw(sCode)
- if response then
- local sResponse = response.readAll()
- response.close()
- local file = fs.open(sPath,"w")
- file.write(sResponse)
- file.close()
- return true
- else
- error("Failed to paste "..sPath)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment