Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --check for HTTP API
- if not http then
- print("Please enable the HTTP API within the ComputerCraft configuration.")
- return
- end
- --variables
- local devKey = "3575e50cc6635311078014a550bc3f9f"
- local userKey = nil
- local logged = false
- local tArgs = {...}
- local badResponses = {
- "Bad API request, IP blocked",
- "Bad API request, api_paste_code was empty",
- "Bad API request, maximum paste file size exceeded",
- "Error, this is a private paste. If this is your private paste, please login to Pastebin first."
- }
- local badRequests = {
- "Bad API request, invalid login",
- "Bad API request, account not active"
- }
- --check for arguments
- local function usage()
- print("Usage:")
- print(shell.getRunningProgram().." <get> <pastebin code> <save file>")
- print(shell.getRunningProgram().." <put> <save file> [username]")
- print("Required: < >")
- print("Optional: [ ]")
- end
- if #tArgs >= 2 and #tArgs <= 4 then
- if tArgs[1] == "get" then
- print("Retrieving from url: http://pastebin.com/"..tArgs[2])
- print("Saving to file: "..tArgs[3])
- if #tArgs[2] > 8 or #tArgs[2] < 8 then
- print("Invalid Pastebin code!")
- else
- print("Connecting to Pastebin...")
- local site = http.get("http://pastebin.com/raw.php?i="..tArgs[2])
- if not site then
- print("Unable to connect to Pastebin!")
- else
- print("Connected! Reading url contents...")
- local siteContent = site.readAll()
- if siteContent == badResponses[4] then
- print("\nERROR - Private paste!\nUnable to download.")
- return
- else
- if not fs.exists(tArgs[3]) then
- local file = fs.open(tArgs[3], "w")
- file.write(siteContent)
- file.close()
- print("Download complete! Saved as '"..tArgs[3].."'")
- else
- print("File already exists!")
- end
- end
- end
- end
- elseif tArgs[1] == "put" then
- if not fs.exists(tArgs[2]) then
- print("No file exists!")
- else
- if not tArgs[3] then
- print("Reading file...")
- local file = fs.open(tArgs[2], "r")
- local postText = file.readAll()
- file.close()
- local postName = tArgs[2]
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&"..
- "api_dev_key="..devKey.."&"..
- "api_paste_format=lua&"..
- "api_paste_name="..textutils.urlEncode(postName).."&"..
- "api_paste_code="..textutils.urlEncode(postText)
- ).readAll()
- if response then
- for i = 1, #badResponses do
- if response == badResponses[i] then
- print(badResponses[i])
- break
- else
- print("Save successful! File stored at:\n"..response)
- break
- end
- end
- else
- print("Unable to connect to Pastebin! Please try again.")
- end
- else
- print("Using private authentication. Password required!")
- write("Password: ")
- local password = read("*")
- local response = http.post(
- "http://pastebin.com/api/api_login.php",
- "api_dev_key="..devKey.."&"..
- "api_user_name="..textutils.urlEncode(tArgs[3]).."&"..
- "api_user_password="..textutils.urlEncode(password)
- ).readAll()
- if response == badRequests[1] then
- print("Invalid login!")
- elseif response == badRequests[2] then
- print("Inactive account!")
- else
- local userKey = response
- print("Reading file...")
- local file = fs.open(tArgs[2], "r")
- local postText = file.readAll()
- file.close()
- local postName = tArgs[2]
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&"..
- "api_dev_key="..devKey.."&"..
- "api_user_key="..userKey.."&"..
- "api_paste_format=lua&"..
- "api_paste_private=0&"..
- "api_paste_name="..textutils.urlEncode(postName).."&"..
- "api_paste_code="..textutils.urlEncode(postText)
- ).readAll()
- if response then
- for i = 1, #badResponses do
- if response == badResponses[i] then
- print(badResponses[i])
- break
- else
- print("Save successful! File stored at:\n"..response)
- break
- end
- end
- else
- print("Unable to connect to Pastebin! Please try again.")
- end
- end
- end
- end
- else
- usage()
- end
- else
- usage()
- end
Advertisement
Add Comment
Please, Sign In to add comment