PSanf2

pastebin_obj

May 16th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. --[[ This is a Lua object to handle interacting with the Pastebin API via HTTP.
  2.    
  3.     To use this in a program do the following (assuming this file is in the CraftOS root directory)
  4.         class = assert(loadfile("pastebin_obj"))
  5.         class()
  6.         Object = Pastebin:new()
  7.    
  8.     Member functions can now be called by doing Object:function(arg1,arg2,...)
  9.     Member variables can be set and accessed with Object.member_variable
  10.    
  11.     See http://www.computercraft.info/forums2/index.php?/topic/8393-oop-in-lua/ for more info on the OOP practices
  12.     See http://www.computercraft.info/forums2/index.php?/topic/10450-handling-errors-creating-controlled-errors-creating-a-bsod/
  13.         for information on how to handle errors.
  14. ]]
  15.  
  16. Pastebin = {}
  17. Pastebin.__index = Pastebin
  18.  
  19. -- Member variables being declared
  20.  
  21. Pastebin.api_dev_key = ""
  22. Pastebin.api_user_key = ""
  23.  
  24. -- Member functions
  25.  
  26. -- Constructor
  27. function Pastebin.new(self, dev_key)
  28.     if not http then
  29.         error("Error constructing Pastebin object. Pastebin requires http API. Set enableAPI_http to 1 in mod_ComputerCraft.cfg")
  30.     end
  31.     local PastebinObject = {}
  32.     setmetatable(PastebinObject, Pastebin)
  33.     self.api_dev_key = dev_key
  34.     return PastebinObject
  35. end
  36.  
  37. -- This attempts to get an API key for the user and returns true on success, false on failure.
  38. function Pastebin.login(self, user_name, user_password)
  39.     local response = http.post(
  40.         "http://pastebin.com/api/api_login.php",
  41.         "api_dev_key="..textutils.urlEncode(self.api_dev_key).."&"..
  42.         "api_user_name="..textutils.urlEncode(user_name).."&"..
  43.         "api_user_password="..textutils.urlEncode(user_password)
  44.     )
  45.     if response then
  46.         local sResponse = response.readAll()
  47.         response.close()
  48.         self.api_user_key = string.match(sResponse,"[^/]+$")
  49.         return true
  50.     else
  51.         return false
  52.     end
  53. end
  54.  
  55. -- gets a raw paste off Pastebin, and returns it.
  56. function Pastebin.get_raw(self, sCode)
  57.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(sCode))
  58.     if response then
  59.         local sResponse = response.readAll()
  60.         response.close()
  61.         return sResponse
  62.     else
  63.         error("Bad response from Pastebin.com. Unable to get data.")
  64.     end
  65. end
  66.  
  67. -- used to put stuff on pastebin
  68. --[[ argument format is Obj.put(paste_code, paste_name, paste_format, paste_private, paste_expire)
  69.     paste_code is the text of the paste
  70.     paste_name is the name of the paste
  71.     paste_format is the syntax highlighting, which is "text" by default (see Pastebin.com API documentation for other options)
  72.     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)
  73.     paste_expire is the expire for the paste (see Pastebin.com API documentation for other options)
  74.     http://pastebin.com/api
  75. ]]
  76. function Pastebin.put(...)
  77.     local args = { ... }
  78.     local self = args[1]
  79.     local paste_code = args[2]
  80.     local paste_name = args[3] or ""
  81.     local paste_format = args[4] or "text"
  82.     local paste_private = args[5] or "0"
  83.     local paste_expire_date = args[6] or "N"
  84.    
  85.     if #args < 2 or #args > 6 then
  86.         error("Invalid number of arguments provided to Pastebin.put()")
  87.     end
  88.    
  89.     if paste_code == "" then
  90.         error("No paste_code provided to Pastebin.put method.")
  91.     end
  92.    
  93.     local url = "http://pastebin.com/api/api_post.php"
  94.    
  95.     local params = "api_dev_key="..textutils.urlEncode(self.api_dev_key).."&"..
  96.                     "api_option=paste&"..
  97.                     "api_paste_code="..textutils.urlEncode(paste_code)
  98.    
  99.     if self.api_user_key ~= "" then
  100.         params = params.."&api_user_key="..textutils.urlEncode(self.api_user_key)
  101.     end
  102.    
  103.     if paste_name ~= "" then
  104.         params = params.."&api_paste_name="..textutils.urlEncode(paste_name)
  105.     end
  106.    
  107.     params = params.."&api_paste_format="..textutils.urlEncode(paste_format)
  108.    
  109.     if paste_private == "0" then
  110.         -- do nothing
  111.     elseif paste_private == "1" then
  112.         params = params.."&api_paste_private="..textutils.urlEncode(paste_private)
  113.     elseif paste_private == "2" then
  114.         if self.api_user_key ~= "" then
  115.             params = params.."&api_paste_private="..textutils.urlEncode(paste_private)
  116.         else
  117.             error("User must be logged in before they can create a private paste.")
  118.         end
  119.     else
  120.         error("Invalid value provided to Pastebin.put(). paste_private can be the string 0, 1, or 2.")
  121.     end
  122.    
  123.     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
  124.         error("Invalid argument provided to Pastebin.put(). paste_expire date can be N, 10M, 1H, 1D, 1W, 2W, or 1M.")
  125.     else
  126.         params = params.."&api_paste_expire="..textutils.urlEncode(paste_expire_date)
  127.     end
  128.    
  129.     local response = http.post(url,params)
  130.     if response then
  131.         local sResponse = response.readAll()
  132.         response.close()
  133.         return string.match(sResponse,"[^/]+$")
  134.     else
  135.         error("Bad response from Pastebin. Unable to put data on server.")
  136.     end
  137.    
  138. end
Advertisement
Add Comment
Please, Sign In to add comment