Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local loginURL = 'http://pastebin.com/api/api_login.php'
- local pasteURL = 'http://pastebin.com/api/api_post.php'
- local getURL = 'http://pastebin.com/raw.php'
- local saveFile = fs.path(shell.running()) .. "pastebin.cfg"
- pastebin = {
- --These three are required
- api_dev_key = 'ec318e63f2bde4100bec7a66d5e1b950',
- api_option = 'paste',
- api_paste_code = '', --This will contain our paste
- --These are optional, but it's better if they're set
- api_user_key = '', --This is retrieved by using the login function
- api_paste_name = nil,
- api_paste_format = 'Lua', --Syntax highlighting
- api_paste_private = 0, --0: Global, 1: Unlisted, 2: Private
- api_paste_expire_date = 'N', --'N' = Never
- }
- local function makeRequest(url, postData)
- local response = http.request(url, postData)
- local result = ''
- if response then
- for line in response do
- result = result .. line
- end
- end
- return result
- end
- local function saveData()
- local file = fs.open(saveFile, "w")
- if not file then
- print("Unable to save data! You will not be able to login.")
- end
- file:write(text.serialize(pastebin))
- file:close()
- end
- local function readData()
- if fs.exists(saveFile) then
- local file = fs.open(saveFile, "r")
- local content = file:read(fs.size(saveFile))
- file:close()
- pastebin = text.unserialize(content)
- end
- end
- local function login(username, password)
- loginData = {api_dev_key=pastebin.api_dev_key, api_user_name=username, api_user_password=password}
- local response = makeRequest(loginURL, loginData)
- if not response or response:find("Bad API request") then
- return response
- end
- pastebin.api_user_key = response
- saveData()
- return true
- end
- local options = {
- login = function(...)
- local arg = {...}
- if not arg[3] then
- print("Missing required arguments: username and password")
- print("Usage: pastebin login <username> <password>")
- return false
- end
- if not login(arg[2], arg[3]) then
- print("Unable to login!")
- else print("Logged in successfully: User key set to -> " .. pastebin.api_user_key) end
- end;
- logout = function(...)
- local arg = {...}
- pastebin.api_user_key = ''
- saveData()
- print("Successfully logged out.")
- end;
- get = function(...)
- local arg = {...}
- if not arg[3] then
- print("Missing required arguments: paste_key and file_name")
- print("Usage: pastebin get <paste key> <intended file name>")
- return false
- end
- local response = makeRequest(getURL.."?i="..arg[2])
- if response then
- local file = fs.open(arg[3], "w")
- file:write(response)
- file:close()
- print("Paste successfully downloaded as " .. arg[3])
- return
- end
- print("Oops! Something went wrong...")
- end;
- paste = function(...)
- local arg = {...}
- if not arg[2] then
- print("Missing required arguments: file_name")
- print("Usage: pastebin paste <file name> [paste name] [syntax format]")
- return
- elseif not fs.exists(arg[2]) then
- print("Invalid file name: Please specify a valid file to upload.")
- print("Usage: pastebin paste <file name> [paste name] [syntax format]")
- end
- local file = fs.open(arg[2],"r")
- local content = file:read(fs.size(arg[2]))
- file:close()
- readData()
- pastebin.api_paste_code = content
- pastebin.api_paste_name = arg[3]
- pastebin.api_paste_format = arg[4] or pastebin.api_paste_format
- print(makeRequest(pasteURL, pastebin))
- end;
- }
- local tArgs = {...}
- if not tArgs[1] or not options[tArgs[1]] then
- print("Usage:")
- print("pastebin login <username> <password>")
- print("pastebin logout")
- print("pastebin get <paste key> <intended file name>")
- print("pastebin paste <file name> [paste name] [syntax format]")
- else
- options[tArgs[1]](table.unpack(tArgs))
- end
Advertisement
Add Comment
Please, Sign In to add comment