Advertisement
FaceInCake

Turtle Pastebin Fix

Feb 9th, 2021 (edited)
2,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1. -- As of Feb 9, 2020 & ComputerCraft version 1.58: the pastebin api does not work because of the outdated base URL
  2. local baseurl = "https://pastebin.com/raw/"
  3. --[[
  4. It used to be 'http://pastebin.com/raw.php?i=', which now redirects the user and returns the non-fatal http code 301, (permanently moved)
  5. However, the http api doesn't deal with redirects, and so while it thinks it succeeded, it returns no text every time.
  6.  
  7. Since you can't edit the built-in apis, you'll have to use this program until you use a newer version
  8. Obviously the problem is you can't use pastebin to download this
  9. Luckily you can copy & paste the following lines, (one line at a time!), in the starting terminal
  10. It will create 'newpastebin' in the root directory, as you can't put it in 'programs/'
  11.  
  12. Copy&paste the following lines into the starting terminal:
  13.  
  14. lua
  15. t = http.get("https://pastebin.com/raw/FuYXbdtv"):readAll()
  16. f = fs.open("newpastebin", "w")
  17. f.write(t)
  18. f.close()
  19. exit()
  20.  
  21. ]]--
  22.  
  23. local function printUsage()
  24.     print("Usages:")
  25.     print("newpastebin get <id> <filename>")
  26.     print("newpastebin put <filename>")
  27.     print("newpastebin run <code> [arguments] [...]")
  28. end
  29.  
  30. -- Connects to pastebin and gets the paste with the id `id`
  31. -- Returns nil on failure, otherwise returns the text of the paste
  32. local function getPaste (id)
  33.     -- Get http response handle, (aka. connect to internet)
  34.     local response = http.get(baseurl..id)
  35.     if response == nil then
  36.         printError("Failed to get http response.")
  37.         printError("(Either that pastebin doesn't exist or you're offline)")
  38.         return nil
  39.     end
  40.     -- Make sure http get succeeded
  41.     local rcode = response.getResponseCode()
  42.     if rcode ~= 200 then
  43.         printError("OK not recieved, code: ", rcode)
  44.         printError("(This error only originally appeared with the current bug, how'd you get this?)")
  45.         return nil
  46.     end
  47.     -- Actually get our text
  48.     local text = response:readAll()
  49.     if text == nil or text == "" then
  50.         printError("No text in response")
  51.         printError("(Demand a refund, I guess)")
  52.         return nil
  53.     end
  54.     return text
  55. end
  56.  
  57. -- Gets the text from the pastebin with id `id` and copies it to the file appointed to by `path`
  58. -- Returns true on success or false on failure
  59. local function get (id, path)
  60.     -- Get the text
  61.     local text = getPaste(id)
  62.     if text == nil then
  63.         return false
  64.     end
  65.     -- Make our new file
  66.     local fout = fs.open(path, "w")
  67.     if fout == nil then
  68.         printError("Failed to create new file.")
  69.         printError("(Either it already exists or that path is off-limits)")
  70.         return false
  71.     end
  72.     -- Copy the text over
  73.     fout.write(text)
  74.     fout.close()
  75.     return true
  76. end
  77.  
  78. -- Puts a file from this device to pastebin
  79. -- Returns the ID of the paste on success, nil on failure
  80. local function put (fileName)
  81.     -- Make sure file exists
  82.     local path = shell.resolve(fileName)
  83.     if not fs.exists(path) or fs.isDir(path) then
  84.         printError("File not found.")
  85.         return -1
  86.     end
  87.     -- Open the file at `path`
  88.     local  fin = fs.open(path, "r")
  89.     if fin == nil then
  90.         printError("Failed to open file for reading.")
  91.         return nil
  92.     end
  93.     -- Read it in! *SNIFF*
  94.     local text = fin.readAll()
  95.     fin.close()
  96.     -- Format and send our post request
  97.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  98.     local pasteName = textutils.urlEncode(fileName)
  99.     local pasteCode = textutils.urlEncode(text)
  100.     local response = http.post(
  101.         "https://pastebin.com/api/api_post.php",
  102.         "api_option=paste&api_dev_key="..key..
  103.         "&api_paste_format=lua&api_paste_name="..pasteName..
  104.         "&api_paste_code="..pasteCode
  105.     )
  106.     -- Make sure it went through
  107.     if response == nil then
  108.         printError("Failed to get http post response.")
  109.         return nil
  110.     end
  111.     local rcode = response.getResponseCode()
  112.     if rcode ~= 200 then
  113.         printError("OK not recieved, code: ", rcode)
  114.         return nil
  115.     end
  116.     -- Parse out the return paste id
  117.     local msg = response.readAll()
  118.     response.close()
  119.     local id = string.match(msg, "[^/]+$")
  120.     -- Print out our answer
  121.     if id then
  122.         print("Successfully uploaded...")
  123.         print("ID: ", id)
  124.     else
  125.         printError("Failed to parse out id, msg: ", msg)
  126.         return nil
  127.     end
  128.     return id
  129. end
  130.  
  131. -- Runs the program with code in the paste with id `id`
  132. -- Returns true on success, false on failure
  133. local function run (id, largs)
  134.     -- Get the text of the paste
  135.     local text = getPaste(id)
  136.     if text == nil then
  137.         printError("Error occured.")
  138.         return false
  139.     end
  140.     -- Load text into function
  141.     local func, err = loadstring(text)
  142.     if not func then
  143.         printError(err)
  144.         return false
  145.     end
  146.     -- Set up environment and call our new function
  147.     setfenv(func, getfenv())
  148.     local success, msg = pcall(func, unpack(largs, 3))
  149.     if not success then
  150.         printError(msg)
  151.         return false
  152.     end
  153.     return true
  154. end
  155.  
  156. local function main (args)
  157.     -- Make sure http is enabled
  158.     if http==nil then
  159.         printError("You need to enable the http api, it's disabled by default.")
  160.         printError("(Enable it through the ComputerCraft config file)")
  161.         return -1
  162.     end
  163.     -- Check first arg is supplied
  164.     if #args <= 1 then
  165.         printUsage()
  166.         return 0
  167.     end
  168.     if args[1] == "get" then -- User wants to get
  169.         -- Make sure arguments are supplied
  170.         if #args ~= 3 then
  171.             printUsage()
  172.             return 0
  173.         end
  174.         -- Make sure file doesn't already exist
  175.         local path = shell.resolve(args[3])
  176.         if fs.exists(path) then
  177.             printError("File already exists")
  178.             return 0
  179.         end
  180.         -- Call our Get function
  181.         if get(args[2], path) then
  182.             print("Done :)")
  183.         else
  184.             return -1
  185.         end
  186.     elseif args[1] == "put" then -- User wants to put
  187.         -- Check that arguments are supplied
  188.         if #args ~= 2 then
  189.             printUsage()
  190.             return 0
  191.         end
  192.         -- call our function
  193.         if put(args[2]) == nil then
  194.             return -1
  195.         end
  196.     elseif args[1] == "run" then -- User wants to run
  197.         -- Make sure arguments are supplied
  198.         if #args <= 1 then
  199.             printUsage()
  200.             return 0
  201.         end
  202.         -- Call our function
  203.         run(args[2], args)
  204.     else
  205.         printUsage()
  206.     end
  207.     return 0
  208. end
  209.  
  210. local args = {...}
  211. main(args)
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement