Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function parse(str)
- local id = str:sub(str:find(":")+2)
- id = id:sub(1, id:len()-2)
- return id
- end
- local function main(args)
- local main_url="http://hastebin.com"
- if not (#args==2 or #args==3) then
- print("Usages:")
- print("hastebin put <filename>")
- print("hastebin get <id> <saveas>")
- return
- end
- if not http then
- print("Hastebin requires the HTTP API to be enabled.")
- return
- end
- local command = args[1]
- if command=="put" then
- local _file = args[2]
- local path = shell.resolve(_file)
- if not fs.exists(path) or fs.isDir(path) then
- print("File "..path.." not found.")
- return
- end
- local name = fs.getName(path)
- local f = fs.open(path, "r")
- local text = f.readAll()
- f.close()
- print("Connecting to hastebin.")
- local r = http.post(main_url.."/documents", text)
- if r then
- print("Successfully uploaded "..path)
- print("You can find it at "..main_url.."/"..parse(r.readAll()))
- r.close()
- else
- print("Could not upload "..path)
- end
- elseif command=="get" then
- local r = http.get(main_url.."/raw/"..textutils.urlEncode(args[2]))
- if r then
- local text = r.readAll()
- r.close()
- local file = fs.open(args[3], "w")
- file.write(text)
- file.close()
- print("Saved "..main_url.."/"..args[2].." to "..args[3])
- else
- print("Failed to retrieve the URL.")
- end
- end
- end
- main({...})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement