warfar

DiskGet

Mar 6th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.55 KB | None | 0 0
  1. --Program made by Warfar
  2. --http://pastebin.com/u/warfar
  3.  
  4.  
  5. local function printUsage()
  6.     print( "Usages:" )
  7.     print( "get <code> <filename>" )
  8. end
  9.  
  10. local tArgs = { ... }
  11. if #tArgs ~= 2 then
  12.     printUsage()
  13.     return
  14. end
  15.  
  16. if not http then
  17.     print( "Pastebin requires http API" )
  18.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  19.     return
  20. end
  21. local sCode
  22. local l = #tArgs[1]
  23. if l == 28 then
  24.     sCode = string.sub(tArgs[1], -8)
  25. elseif l == 8 then
  26.     sCode = tArgs[1]
  27. else printUsage()
  28. return
  29. end
  30.  
  31. for _,v in ipairs(redstone.getSides()) do
  32.     if peripheral.getType(v) == "disk"
  33.         diskFound = true
  34.         break
  35.     else
  36.         diskFound = false
  37.     end
  38. end
  39.  
  40. if not diskFound then
  41.     print("No disk drive found")
  42. end
  43.  
  44. -- Determine file to download
  45. local sFile = tArgs[2]
  46. local sPath = shell.resolve( sFile )
  47.  
  48. -- GET the contents from pastebin
  49. write( "Connecting" )
  50. local response = http.get(
  51.     "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  52. )
  53.  
  54. if response then
  55.     term.clear()
  56.     term.setCursorPos(1,1)
  57.     print( "Success." )
  58.  
  59.     local sResponse = response.readAll()
  60.     response.close()
  61.     if fs.exists("disk/"..sPath) then
  62.         write("Override? y/n")
  63.         local choice = io.read()
  64.         if choice == "y" then
  65.             fs.delete("disk/"..sPath)
  66.         else
  67.             print("File Exists")
  68.             return
  69.         end
  70.     end
  71.     local file = fs.open("disk/"..sPath, "w" )
  72.     file.write( sResponse )
  73.     file.close()
  74. else
  75.     print( "Failed." )
  76. end
Advertisement
Add Comment
Please, Sign In to add comment