warfar

get

Feb 8th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "get <code> <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.     print( "Pastebin requires http API" )
  14.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  15.     return
  16. end
  17. local sCode
  18. local l = #tArgs[1]
  19. if l == 28 then
  20.     sCode = string.sub(tArgs[1], -8)
  21.     elseif l == 8 then
  22.     sCode = tArgs[1]
  23.     else printUsage()
  24.     return
  25. end
  26.  
  27.  
  28. -- Determine file to download
  29.     local sFile = tArgs[2]
  30.     local sPath = shell.resolve( sFile )
  31.     if fs.exists( sPath ) then
  32.         fs.delete( sPath )
  33.     end
  34.    
  35.     -- GET the contents from pastebin
  36.     write( "Connecting" )
  37.     local response = http.get(
  38.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  39.         )
  40.        
  41.     if response then
  42.         term.clear()
  43.         term.setCursorPos(1,1)
  44.         print( "Success." )
  45.        
  46.         local sResponse = response.readAll()
  47.         response.close()
  48.        
  49.         local file = fs.open( sPath, "w" )
  50.         file.write( sResponse )
  51.         file.close()
  52.     else
  53.         print( "Failed." )
  54.     end
Advertisement
Add Comment
Please, Sign In to add comment