Advertisement
Adskaya_Dro4ila

pastebin.lua

Jan 31st, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[ This program allows downloading and uploading from and to pastebin.com.
  2.      Authors: Sangar, Vexatos ]]
  3. local component = require("component")
  4. local fs = require("filesystem")
  5. local internet = require("internet")
  6. local shell = require("shell")
  7.  
  8. if not component.isAvailable("internet") then
  9.   io.stderr:write("This program requires an internet card to run.")
  10.   return
  11. end
  12.  
  13. local args, options = shell.parse(...)
  14.  
  15. -- This gets code from the website and stores it in the specified file.
  16. local function get(pasteId, filename)
  17.   local f, reason = io.open(filename, "w")
  18.   if not f then
  19.     io.stderr:write("Failed opening file for writing: " .. reason)
  20.     return
  21.   end
  22.  
  23.   io.write("Downloading from pastebin.com... ")
  24.   local url = "https://pastebin.com/raw/" .. pasteId
  25.   local result, response = pcall(internet.request, url)
  26.   if result then
  27.     io.write("success.\n")
  28.     for chunk in response do
  29.       if not options.k then
  30.         string.gsub(chunk, "\r\n", "\n")
  31.       end
  32.       f:write(chunk)
  33.     end
  34.  
  35.     f:close()
  36.     io.write("Saved data to " .. filename .. "\n")
  37.   else
  38.     io.write("failed.\n")
  39.     f:close()
  40.     fs.remove(filename)
  41.     io.stderr:write("HTTP request failed: " .. response .. "\n")
  42.   end
  43. end
  44.  
  45. -- This makes a string safe for being used in a URL.
  46. local function encode(code)
  47.   if code then
  48.     code = string.gsub(code, "([^%w ])", function (c)
  49.       return string.format("%%%02X", string.byte(c))
  50.     end)
  51.     code = string.gsub(code, " ", "+")
  52.   end
  53.   return code
  54. end
  55.  
  56. -- This stores the program in a temporary file, which it will
  57. -- delete after the program was executed.
  58. local function run(pasteId, ...)
  59.   local tmpFile = os.tmpname()
  60.   get(pasteId, tmpFile)
  61.   io.write("Running...\n")
  62.  
  63.   local success, reason = shell.execute(tmpFile, nil, ...)
  64.   if not success then
  65.     io.stderr:write(reason)
  66.   end
  67.   fs.remove(tmpFile)
  68. end
  69.  
  70. -- Uploads the specified file as a new paste to pastebin.com.
  71. local function put(path)
  72.   local config = {}
  73.   local configFile = loadfile("/etc/pastebin.conf", "t", config)
  74.   if configFile then
  75.     local result, reason = pcall(configFile)
  76.     if not result then
  77.       io.stderr:write("Failed loading config: " .. reason)
  78.     end
  79.   end
  80.  
  81.   -- Убедитесь, что у вас есть ключ API и ключ пользователя
  82.   config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
  83.   config.user_key = config.user_key or "c89ce247658af126f8afe2ec4293daab"  -- Замените на ваш ключ пользователя
  84.  
  85.   local file, reason = io.open(path, "r")
  86.  
  87.   if not file then
  88.     io.stderr:write("Failed opening file for reading: " .. reason)
  89.     return
  90.   end
  91.  
  92.   local data = file:read("*a")
  93.   file:close()
  94.  
  95.   io.write("Uploading to pastebin.com... ")
  96.   local result, response = pcall(internet.request,
  97.         "https://pastebin.com/api/api_post.php",
  98.         "api_option=paste&" ..
  99.         "api_dev_key=" .. config.key .. "&" ..
  100.         "api_user_key=" .. config.user_key .. "&" ..  -- Добавьте ключ пользователя
  101.         "api_paste_format=lua&" ..
  102.         "api_paste_expire_date=N&" ..
  103.         "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
  104.         "api_paste_code=" .. encode(data))
  105.  
  106.   if result then
  107.     local info = ""
  108.     for chunk in response do
  109.       info = info .. chunk
  110.     end
  111.     if string.match(info, "^Bad API request, ") then
  112.       io.write("failed.\n")
  113.       io.write(info)
  114.     else
  115.       io.write("success.\n")
  116.       local pasteId = string.match(info, "[^/]+$")
  117.       io.write("Uploaded as " .. info .. "\n")
  118.       io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
  119.     end
  120.   else
  121.     io.write("failed.\n")
  122.     io.stderr:write(response)
  123.   end
  124. end
  125.  
  126. local command = args[1]
  127. if command == "put" then
  128.   if #args == 2 then
  129.     put(shell.resolve(args[2]))
  130.     return
  131.   end
  132. elseif command == "get" then
  133.   if #args == 3 then
  134.     local path = shell.resolve(args[3])
  135.     if fs.exists(path) then
  136.       if not options.f or not os.remove(path) then
  137.         io.stderr:write("file already exists")
  138.         return
  139.       end
  140.     end
  141.     get(args[2], path)
  142.     return
  143.   end
  144. elseif command == "run" then
  145.   if #args >= 2 then
  146.     run(args[2], table.unpack(args, 3))
  147.     return
  148.   end
  149. end
  150.  
  151. -- If we come here there was some invalid input.
  152. io.write("Usages:\n")
  153. io.write("pastebin put [-f] <file>\n")
  154. io.write("pastebin get [-f] <id> <file>\n")
  155. io.write("pastebin run [-f] <id> [<arguments...>]\n")
  156. io.write(" -f: Force overwriting existing files.\n")
  157. io.write(" -k: keep line endings as-is (will convert\n")
  158. io.write("     Windows line endings to Unix otherwise).")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement