Advertisement
Lupus590

pastebin.lua

Dec 7th, 2020 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. --[[
  2. -- copy and run this to download
  3. local response = http.get("https://pastebin.com/raw/wTsdw2c0" , nil , true)
  4. local file = fs.open("newPastebin", "wb")
  5. file.write(response.readAll())
  6. file.close()
  7. response.close()
  8. ]]
  9.  
  10.  
  11. local function printUsage()
  12.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  13.     print("Usages:")
  14.     print(programName .. " put <filename>")
  15.     print(programName .. " get <code> <filename>")
  16.     print(programName .. " run <code> <arguments>")
  17. end
  18.  
  19. local tArgs = { ... }
  20. if #tArgs < 2 then
  21.     printUsage()
  22.     return
  23. end
  24.  
  25. if not http then
  26.     printError("Pastebin requires the http API")
  27.     printError("Set http.enabled to true in CC: Tweaked's config")
  28.     return
  29. end
  30.  
  31. --- Attempts to guess the pastebin ID from the given code or URL
  32. local function extractId(paste)
  33.     local patterns = {
  34.         "^([%a%d]+)$",
  35.         "^https?://pastebin.com/([%a%d]+)$",
  36.         "^pastebin.com/([%a%d]+)$",
  37.         "^https?://pastebin.com/raw/([%a%d]+)$",
  38.         "^pastebin.com/raw/([%a%d]+)$",
  39.     }
  40.  
  41.     for i = 1, #patterns do
  42.         local code = paste:match(patterns[i])
  43.         if code then return code end
  44.     end
  45.  
  46.     return nil
  47. end
  48.  
  49. local function get(url)
  50.     local paste = extractId(url)
  51.     if not paste then
  52.         io.stderr:write("Invalid pastebin code.\n")
  53.         io.write("The code is the ID at the end of the pastebin.com URL.\n")
  54.         return
  55.     end
  56.  
  57.     write("Connecting to pastebin.com... ")
  58.     -- Add a cache buster so that spam protection is re-checked
  59.     local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
  60.     local response, err = http.get(
  61.         "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
  62.     )
  63.  
  64.     if response then
  65.         -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  66.         local headers = response.getResponseHeaders()
  67.         if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  68.             io.stderr:write("Failed.\n")
  69.             print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
  70.             return
  71.         end
  72.  
  73.         print("Success.")
  74.  
  75.         local sResponse = response.readAll()
  76.         response.close()
  77.         return sResponse
  78.     else
  79.         io.stderr:write("Failed.\n")
  80.         print(err)
  81.     end
  82. end
  83.  
  84. local sCommand = tArgs[1]
  85. if sCommand == "put" then
  86.     -- Upload a file to pastebin.com
  87.     -- Determine file to upload
  88.     local sFile = tArgs[2]
  89.     local sPath = shell.resolve(sFile)
  90.     if not fs.exists(sPath) or fs.isDir(sPath) then
  91.         print("No such file")
  92.         return
  93.     end
  94.  
  95.     -- Read in the file
  96.     local sName = fs.getName(sPath)
  97.     local file = fs.open(sPath, "r")
  98.     local sText = file.readAll()
  99.     file.close()
  100.  
  101.     -- POST the contents to pastebin
  102.     write("Connecting to pastebin.com... ")
  103.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  104.     local response = http.post(
  105.         "https://pastebin.com/api/api_post.php",
  106.         "api_option=paste&" ..
  107.         "api_dev_key=" .. key .. "&" ..
  108.         "api_paste_format=lua&" ..
  109.         "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
  110.         "api_paste_code=" .. textutils.urlEncode(sText)
  111.     )
  112.  
  113.     if response then
  114.         print("Success.")
  115.  
  116.         local sResponse = response.readAll()
  117.         response.close()
  118.  
  119.         local sCode = string.match(sResponse, "[^/]+$")
  120.         print("Uploaded as " .. sResponse)
  121.         print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
  122.  
  123.     else
  124.         print("Failed.")
  125.     end
  126.  
  127. elseif sCommand == "get" then
  128.     -- Download a file from pastebin.com
  129.     if #tArgs < 3 then
  130.         printUsage()
  131.         return
  132.     end
  133.  
  134.     -- Determine file to download
  135.     local sCode = tArgs[2]
  136.     local sFile = tArgs[3]
  137.     local sPath = shell.resolve(sFile)
  138.     if fs.exists(sPath) then
  139.         print("File already exists")
  140.         return
  141.     end
  142.  
  143.     -- GET the contents from pastebin
  144.     local res = get(sCode)
  145.     if res then
  146.         local file = fs.open(sPath, "w")
  147.         file.write(res)
  148.         file.close()
  149.  
  150.         print("Downloaded as " .. sFile)
  151.     end
  152. elseif sCommand == "run" then
  153.     local sCode = tArgs[2]
  154.  
  155.     local res = get(sCode)
  156.     if res then
  157.         local func, err = load(res, sCode, "t", _ENV)
  158.         if not func then
  159.             printError(err)
  160.             return
  161.         end
  162.         local success, msg = pcall(func, select(3, ...))
  163.         if not success then
  164.             printError(msg)
  165.         end
  166.     end
  167. else
  168.     printUsage()
  169.     return
  170. end
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement