Advertisement
FFGFlash

api/pastebin.lua

Sep 28th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. local api = { LoadOrder = 0 }
  2. function api.Build()
  3.   local Pastebin = { DevKey = "ByTl7aSZ0CDz83Qqzgqr8-LzuUPy1sUG", Base = "https://pastebin.com/api/", UserKey = false }
  4.   Pastebin.__index = Pastebin
  5.   function Pastebin:__call(u,p)
  6.     local r=request:post(self.Base.."api_login.php",{api_dev_key=self.DevKey,api_user_name=u,api_user_password=p})
  7.     if not r then return false,"Failed to login." end
  8.     local inst=setmetatable({UserKey=r.readAll()},self)
  9.     r.close()
  10.     function inst:delete(c)
  11.       local r=request:post(self.Base.."api_post.php",{api_dev_key=self.DevKey,api_user_key=self.UserKey,api_option="delete",api_paste_key=c})
  12.       if not r then return false,"Failed." end
  13.       r.close()
  14.       return true
  15.     end
  16.     function inst:list(c)
  17.       local r=request:post(self.Base.."api_post.php",{api_dev_key=self.DevKey,api_user_key=self.UserKey,api_option="list",api_results_limit=c})
  18.       if not r then return false,"Failed." end
  19.       local l=r.readLine()
  20.       local d,p={},{}
  21.       while l do
  22.         if l =="<paste>" then p = {}
  23.         elseif l == "</paste>" then d[#d+1]=p
  24.         else
  25.           local k,v=string.match(l,"^.+<(.+)>(.+)</.+>$")
  26.           p[k]=v
  27.         end
  28.         l=r.readLine()
  29.       end
  30.       r.close()
  31.       return d
  32.     end
  33.     return inst
  34.   end
  35.   function Pastebin:getPaste(c)
  36.     local r=not self.UserKey and request:get("https://pastebin.com/raw.php",{i=c}) or request:post(self.Base.."api_raw.php",{api_dev_key=self.DevKey,api_user_key=self.UserKey,api_option="show_paste",api_paste_key=c})
  37.     if not r then return false, "Failed to get paste." end
  38.     local d=r.readAll()
  39.     r.close()
  40.     return d
  41.   end
  42.   function Pastebin:get(c,p,f)
  43.     p=p or c
  44.     if f then fs.delete(p) elseif fs.exists(p) then return false,"File already exists." end
  45.     local d,e=self:getPaste(c)
  46.     if not d then return false,e end
  47.     local a=fs.open(p,"w")
  48.     a.write(d)
  49.     a.close()
  50.     return true
  51.   end
  52.   function Pastebin:run(c,...)
  53.     local d,e=self:getPaste(c)
  54.     if not d then return false,e end
  55.     local f,e=loadstring(d)
  56.     if not f then return false,e end
  57.     setfenv(f,getfenv())
  58.     local s,e=pcall(f,...)
  59.     if not s then return false,e end
  60.     return true
  61.   end
  62.   function Pastebin:put(p,n)
  63.     if not fs.exists(p) or fs.isDir(p) then return false,"No such file." end
  64.     local f=fs.open(p,"r")
  65.     local r=request:post(self.Base.."api_post.php",{api_dev_key=self.DevKey,api_option="paste",api_paste_format="lua",api_paste_name=n or fs.getName(p),api_paste_code=f.readAll(),api_user_key=self.UserKey or nil})
  66.     f.close()
  67.     if not r then return false, "Failed." end
  68.     local c=string.match(r.readAll(),"[^/]+$")
  69.     r.close()
  70.     return c
  71.   end
  72.   return setmetatable(Pastebin, Pastebin)
  73. end
  74. return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement