FFGFlash

api/pastebin

Sep 21st, 2021 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local devKey = "ByTl7aSZ0CDz83Qqzgqr8-LzuUPy1sUG"
  2.  
  3. function getPaste(code)
  4.   local res = request.get("http://pastebin.com/raw.php", { i = code })
  5.   if not res then return false, "Failed to get paste." end
  6.   local data = res.readAll()
  7.   res.close()
  8.   return data
  9. end
  10.  
  11. function get(code, path, force)
  12.   path = path or code
  13.   if force then fs.delete(path)
  14.   elseif fs.exists(path) then return false, "File already exists."
  15.   end
  16.   local data,err = getPaste(code)
  17.   if not data then return false, err end
  18.   file = fs.open(path, "w")
  19.   file.write(data)
  20.   file.close()
  21.   return true
  22. end
  23.  
  24. function put(path)
  25.   if not fs.exists(path) or fs.isDir(path) then return false, "No such file." end
  26.   local name = fs.getName(path)
  27.   local file = fs.open(path, "r")
  28.   local data = file.readAll()
  29.   file.close()
  30.   local res = request.post("http://pastebin.com/api/api_post.php", {
  31.     api_option = "paste",
  32.     api_dev_key = devKey,
  33.     api_paste_format = "lua",
  34.     api_paste_name = name,
  35.     api_paste_code = data
  36.   })
  37.   if not res then return false, "Failed." end
  38.   local code = string.match(res.readAll(), "[^/]+$")
  39.   res.close()
  40.   return code
  41. end
  42.  
  43. function run(code, ...)
  44.   local data,err = getPaste(code)
  45.   if not data then return false, err end
  46.   local func,err = loadstring(data)
  47.   if not func then return false, err end
  48.   setfenv(func,getfenv())
  49.   local success,err = pcall(func, ...)
  50.   if not success then return false, err end
  51.   return true
  52. end
  53.  
  54. function login(username, password)
  55.   local res = request.post("https://pastebin.com/api/api_login.php", { api_dev_key = devKey, api_user_name = username, api_user_password = password })
  56.   if not res then return false, "Failed to login." end
  57.  
  58.   local pb = { UserKey = res.readAll() }
  59.   res.close()
  60.  
  61.   function pb.getPaste(code)
  62.     local res = request.post("https://pastebin.com/api/api_raw.php", { api_dev_key = devKey, api_user_key = pb.UserKey, api_paste_key = code, api_option = "show_paste" })
  63.     if not res then return false, "Failed to get paste." end
  64.     local data = res.readAll()
  65.     res.close()
  66.     return data
  67.   end
  68.  
  69.   function pb.get(code, path, force)
  70.     path = path or code
  71.     if force then fs.delete(path)
  72.     elseif fs.exists(path) then return false, "File already exists."
  73.     end
  74.     local data,err = pb.getPaste(code)
  75.     if not data then return false, err end
  76.     file = fs.open(path, "w")
  77.     file.write(data)
  78.     file.close()
  79.     return true
  80.   end
  81.  
  82.   function pb.delete(code)
  83.     local res = request.post("https://pastebin.com/api/api_post.php", {
  84.       api_option = "delete",
  85.       api_dev_key = devKey,
  86.       api_user_key = pb.UserKey,
  87.       api_paste_key = code
  88.     })
  89.     if not res then return false, "Failed." end
  90.     res.close()
  91.     return true
  92.   end
  93.  
  94.   function pb.put(path)
  95.     if not fs.exists(path) or fs.isDir(path) then return false, "No such file." end
  96.     local name = fs.getName(path)
  97.     local file = fs.open(path, "r")
  98.     local data = file.readAll()
  99.     file.close()
  100.     local res = request.post("https://pastebin.com/api/api_post.php", {
  101.       api_option = "paste",
  102.       api_dev_key = devKey,
  103.       api_paste_format = "lua",
  104.       api_paste_name = name,
  105.       api_paste_code = data,
  106.       api_paste_private = "0",
  107.       api_user_key = pb.UserKey
  108.     })
  109.     if not res then return false, "Failed." end
  110.     local code = string.match(res.readAll(), "[^/]+$")
  111.     res.close()
  112.     return code
  113.   end
  114.  
  115.   function pb.list()
  116.     local res = request.post("https://pastebin.com/api/api_post.php", {
  117.       api_option = "list",
  118.       api_dev_key = devKey,
  119.       api_user_key = pb.UserKey,
  120.       api_results_limit = "1000"
  121.     })
  122.     if not res then return false, "Failed." end
  123.     local line = res.readLine()
  124.     local data,paste = {},{}
  125.     while line do
  126.       if line == "<paste>" then paste = {}
  127.       elseif line == "</paste>" then table.insert(data, paste)
  128.       else
  129.         local key, value = string.match(line, "^.+<(.+)>(.+)</.+>$")
  130.         paste[key] = value
  131.       end
  132.       line = res.readLine()
  133.     end
  134.     res.close()
  135.     return data
  136.   end
  137.  
  138.   function pb.run(code, ...)
  139.     local data,err = pb.getPaste(code)
  140.     if not data then return false, err end
  141.     local func,err = loadstring(data)
  142.     if not func then return false, err end
  143.     setfenv(func,getfenv())
  144.     local success,err = pcall(func, ...)
  145.     if not success then return false, err end
  146.     return true
  147.   end
  148.  
  149.   return pb
  150. end
Add Comment
Please, Sign In to add comment