Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function printUsage()
- local programName = arg[0] or fs.getName(shell.getRunningProgram())
- print("Usages:")
- print(programName .. " put <filename>")
- print(programName .. " get <code> <filename>")
- print(programName .. " run <code> <arguments>")
- end
- local tArgs = { ... }
- if #tArgs < 2 then
- printUsage()
- return
- end
- if not http then
- printError("Spacebin requires the http API")
- printError("Set http.enabled to true in CC's config")
- return
- end
- --- Attempts to guess the spacebin ID from the given code or URL
- local function extractId(paste)
- local patterns = {
- "^([%a%d]+)$",
- "^https?://spaceb.in/([%a%d]+)$",
- "^spaceb.in/([%a%d]+)$",
- "^https?://spaceb.in/([%a%d]+)/raw$",
- }
- for i = 1, #patterns do
- local code = paste:match(patterns[i])
- if code then return code end
- end
- return nil
- end
- local function get(url)
- local paste = extractId(url)
- if not paste then
- io.stderr:write("Invalid spacebin code.\n")
- io.write("The code is the ID at the end of the spaceb.in URL.\n")
- return
- end
- write("Connecting to spaceb.in... ")
- local response, err = http.get(
- "https://spaceb.in/api/v1/documents/"..textutils.urlEncode(paste).."/raw"
- )
- if response then
- local headers = response.getResponseHeaders()
- if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
- io.stderr:write("Failed.\n")
- print("Failed to download paste.")
- return
- end
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- io.stderr:write("Failed.\n")
- print(err)
- end
- end
- local sCommand = tArgs[1]
- if sCommand == "put" then
- -- Upload a file to spaceb.in
- -- Determine file to upload
- local sFile = tArgs[2]
- local sPath = shell.resolve(sFile)
- if not fs.exists(sPath) or fs.isDir(sPath) then
- print("No such file")
- return
- end
- -- Read in the file
- local sName = fs.getName(sPath)
- local file = fs.open(sPath, "r")
- local sText = file.readAll()
- file.close()
- -- POST the contents to spacebin
- write("Connecting to spaceb.in... ")
- local response, err = http.post(
- "https://spaceb.in/api/v1/documents",
- textutils.serialiseJSON(
- {
- ["content"] = sText;
- ["extension"] = "txt";
- }
- ),
- {
- ["Content-Type"] = "application/json";
- }
- )
- if response then
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match(sResponse, "\"id\":[\s]*\"([a-zA-Z]+)\"")
- print("Uploaded as " .. sCode)
- print("Run \"spacebin get " .. sCode .. "\" to download anywhere")
- else
- print("Failed.")
- error(err)
- end
- elseif sCommand == "get" then
- -- Download a file from spaceb.in
- if #tArgs < 3 then
- printUsage()
- return
- end
- -- Determine file to download
- local sCode = tArgs[2]
- local sFile = tArgs[3]
- local sPath = shell.resolve(sFile)
- if fs.exists(sPath) then
- print("File already exists")
- return
- end
- -- GET the contents from spacebin
- local res = get(sCode)
- if res then
- local file = fs.open(sPath, "w")
- file.write(res)
- file.close()
- print("Downloaded as " .. sFile)
- end
- elseif sCommand == "run" then
- local sCode = tArgs[2]
- local res = get(sCode)
- if res then
- local func, err = load(res, sCode, "t", _ENV)
- if not func then
- printError(err)
- return
- end
- local success, msg = pcall(func, select(3, ...))
- if not success then
- printError(msg)
- end
- end
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement