TrovadorUrbano

CC Simple Dropbox API

May 3rd, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "pastebin get <raw link> <filename>" )
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs < 2 then
  8.     printUsage()
  9.     return
  10. end
  11.  
  12. if not http then
  13.     printError( "Pastebin requires http API" )
  14.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  15.     return
  16. end
  17.  
  18. local function get(paste)
  19.     write( "Connecting to Dropbox.com... " )
  20.     print(textutils.urlEncode( paste ))
  21.     local response = http.get( paste )
  22.        
  23.     if response then
  24.         print( "Success." )
  25.        
  26.         local sResponse = response.readAll()
  27.         response.close()
  28.         return sResponse
  29.     else
  30.         printError( "Failed." )
  31.     end
  32. end
  33.  
  34. local sCommand = tArgs[1]
  35.  
  36. if sCommand == "get" then
  37.     -- Download a file from pastebin.com
  38.     if #tArgs < 3 then
  39.         printUsage()
  40.         return
  41.     end
  42.  
  43.     -- Determine file to download
  44.     local sCode = tArgs[2]
  45.     local sFile = tArgs[3]
  46.     local sPath = shell.resolve( sFile )
  47.     if fs.exists( sPath ) then
  48.         print( "File already exists" )
  49.         return
  50.     end
  51.    
  52.     -- GET the contents from pastebin
  53.     local res = get(sCode)
  54.     if res then        
  55.         local file = fs.open( sPath, "w" )
  56.         file.write( res )
  57.         file.close()
  58.        
  59.         print( "Downloaded as "..sFile )
  60.     end
  61. else
  62.     printUsage()
  63.     return
  64. end
Advertisement
Add Comment
Please, Sign In to add comment