Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- As of Feb 9, 2020 & ComputerCraft version 1.58: the pastebin api does not work because of the outdated base URL
- local baseurl = "https://pastebin.com/raw/"
- --[[
- 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)
- However, the http api doesn't deal with redirects, and so while it thinks it succeeded, it returns no text every time.
- Since you can't edit the built-in apis, you'll have to use this program until you use a newer version
- Obviously the problem is you can't use pastebin to download this
- Luckily you can copy & paste the following lines, (one line at a time!), in the starting terminal
- It will create 'newpastebin' in the root directory, as you can't put it in 'programs/'
- Copy&paste the following lines into the starting terminal:
- lua
- t = http.get("https://pastebin.com/raw/FuYXbdtv"):readAll()
- f = fs.open("newpastebin", "w")
- f.write(t)
- f.close()
- exit()
- ]]--
- local function printUsage()
- print("Usages:")
- print("newpastebin get <id> <filename>")
- print("newpastebin put <filename>")
- print("newpastebin run <code> [arguments] [...]")
- end
- -- Connects to pastebin and gets the paste with the id `id`
- -- Returns nil on failure, otherwise returns the text of the paste
- local function getPaste (id)
- -- Get http response handle, (aka. connect to internet)
- local response = http.get(baseurl..id)
- if response == nil then
- printError("Failed to get http response.")
- printError("(Either that pastebin doesn't exist or you're offline)")
- return nil
- end
- -- Make sure http get succeeded
- local rcode = response.getResponseCode()
- if rcode ~= 200 then
- printError("OK not recieved, code: ", rcode)
- printError("(This error only originally appeared with the current bug, how'd you get this?)")
- return nil
- end
- -- Actually get our text
- local text = response:readAll()
- if text == nil or text == "" then
- printError("No text in response")
- printError("(Demand a refund, I guess)")
- return nil
- end
- return text
- end
- -- Gets the text from the pastebin with id `id` and copies it to the file appointed to by `path`
- -- Returns true on success or false on failure
- local function get (id, path)
- -- Get the text
- local text = getPaste(id)
- if text == nil then
- return false
- end
- -- Make our new file
- local fout = fs.open(path, "w")
- if fout == nil then
- printError("Failed to create new file.")
- printError("(Either it already exists or that path is off-limits)")
- return false
- end
- -- Copy the text over
- fout.write(text)
- fout.close()
- return true
- end
- -- Puts a file from this device to pastebin
- -- Returns the ID of the paste on success, nil on failure
- local function put (fileName)
- -- Make sure file exists
- local path = shell.resolve(fileName)
- if not fs.exists(path) or fs.isDir(path) then
- printError("File not found.")
- return -1
- end
- -- Open the file at `path`
- local fin = fs.open(path, "r")
- if fin == nil then
- printError("Failed to open file for reading.")
- return nil
- end
- -- Read it in! *SNIFF*
- local text = fin.readAll()
- fin.close()
- -- Format and send our post request
- local key = "0ec2eb25b6166c0c27a394ae118ad829"
- local pasteName = textutils.urlEncode(fileName)
- local pasteCode = textutils.urlEncode(text)
- local response = http.post(
- "https://pastebin.com/api/api_post.php",
- "api_option=paste&api_dev_key="..key..
- "&api_paste_format=lua&api_paste_name="..pasteName..
- "&api_paste_code="..pasteCode
- )
- -- Make sure it went through
- if response == nil then
- printError("Failed to get http post response.")
- return nil
- end
- local rcode = response.getResponseCode()
- if rcode ~= 200 then
- printError("OK not recieved, code: ", rcode)
- return nil
- end
- -- Parse out the return paste id
- local msg = response.readAll()
- response.close()
- local id = string.match(msg, "[^/]+$")
- -- Print out our answer
- if id then
- print("Successfully uploaded...")
- print("ID: ", id)
- else
- printError("Failed to parse out id, msg: ", msg)
- return nil
- end
- return id
- end
- -- Runs the program with code in the paste with id `id`
- -- Returns true on success, false on failure
- local function run (id, largs)
- -- Get the text of the paste
- local text = getPaste(id)
- if text == nil then
- printError("Error occured.")
- return false
- end
- -- Load text into function
- local func, err = loadstring(text)
- if not func then
- printError(err)
- return false
- end
- -- Set up environment and call our new function
- setfenv(func, getfenv())
- local success, msg = pcall(func, unpack(largs, 3))
- if not success then
- printError(msg)
- return false
- end
- return true
- end
- local function main (args)
- -- Make sure http is enabled
- if http==nil then
- printError("You need to enable the http api, it's disabled by default.")
- printError("(Enable it through the ComputerCraft config file)")
- return -1
- end
- -- Check first arg is supplied
- if #args <= 1 then
- printUsage()
- return 0
- end
- if args[1] == "get" then -- User wants to get
- -- Make sure arguments are supplied
- if #args ~= 3 then
- printUsage()
- return 0
- end
- -- Make sure file doesn't already exist
- local path = shell.resolve(args[3])
- if fs.exists(path) then
- printError("File already exists")
- return 0
- end
- -- Call our Get function
- if get(args[2], path) then
- print("Done :)")
- else
- return -1
- end
- elseif args[1] == "put" then -- User wants to put
- -- Check that arguments are supplied
- if #args ~= 2 then
- printUsage()
- return 0
- end
- -- call our function
- if put(args[2]) == nil then
- return -1
- end
- elseif args[1] == "run" then -- User wants to run
- -- Make sure arguments are supplied
- if #args <= 1 then
- printUsage()
- return 0
- end
- -- Call our function
- run(args[2], args)
- else
- printUsage()
- end
- return 0
- end
- local args = {...}
- main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement