Advertisement
tiin57

Hastebin

Aug 24th, 2013
2,872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. local function parse(str)
  2.   local id = str:sub(str:find(":")+2)
  3.   id = id:sub(1, id:len()-2)
  4.   return id
  5. end
  6. local function main(args)
  7.   local main_url="http://hastebin.com"
  8.   if not (#args==2 or #args==3) then
  9.     print("Usages:")
  10.     print("hastebin put <filename>")
  11.     print("hastebin get <id> <saveas>")
  12.     return
  13.   end
  14.   if not http then
  15.     print("Hastebin requires the HTTP API to be enabled.")
  16.     return
  17.   end
  18.   local command = args[1]
  19.   if command=="put" then
  20.     local _file = args[2]
  21.     local path = shell.resolve(_file)
  22.     if not fs.exists(path) or fs.isDir(path) then
  23.       print("File "..path.." not found.")
  24.       return
  25.     end
  26.     local name = fs.getName(path)
  27.     local f = fs.open(path, "r")
  28.     local text = f.readAll()
  29.     f.close()
  30.    
  31.     print("Connecting to hastebin.")
  32.     local r = http.post(main_url.."/documents", text)
  33.     if r then
  34.       print("Successfully uploaded "..path)
  35.       print("You can find it at "..main_url.."/"..parse(r.readAll()))
  36.       r.close()
  37.     else
  38.       print("Could not upload "..path)
  39.     end
  40.   elseif command=="get" then
  41.     local r = http.get(main_url.."/raw/"..textutils.urlEncode(args[2]))
  42.     if r then
  43.       local text = r.readAll()
  44.       r.close()
  45.       local file = fs.open(args[3], "w")
  46.       file.write(text)
  47.       file.close()
  48.       print("Saved "..main_url.."/"..args[2].." to "..args[3])
  49.     else
  50.       print("Failed to retrieve the URL.")
  51.     end
  52.   end
  53. end
  54. main({...})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement