fugitiveplatypus

get

Feb 4th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. hostname="http://www.example.com"
  2. page="/path/to/paste.php"
  3.  
  4. local function printUsage()
  5.     print( "Usages:" )
  6.     print( "get [file,proj] <filename>" )
  7. end
  8.  
  9. local tArgs = { ... }
  10. if #tArgs < 2 then
  11.     printUsage()
  12.     return
  13. end
  14.  
  15.  
  16. if not http then
  17.     print( "get requires http API" )
  18.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  19.     return
  20. end
  21.  
  22. -- Download a file from www.example.com
  23. print()
  24.  
  25. if tArgs[1] == "file" then
  26.     -- Determine file to download
  27.     local sFile = tArgs[2]
  28.  
  29.     -- GET the contents
  30.     print( "-Get from "..hostname.."-" )
  31.     write("Connecting.. ")
  32.     local response = http.get( hostname..page.."?f="..sFile )
  33.  
  34.     if response then
  35.         local sResponse = response.readAll()
  36.  
  37.         print("Success.")
  38.         write("Getting \""..sFile.."\".. ")  
  39.         if sResponse and (sResponse ~= "") then  
  40.             print( "Success." )
  41.  
  42.             response.close()
  43.  
  44.             write( "Saving as \""..sFile.."\".. " )
  45.  
  46.             local sPath = shell.resolve( sFile )
  47.             if fs.exists( sPath ) then
  48.                 fs.delete( sPath )
  49.             end
  50.  
  51.             local file = fs.open( sPath, "w" )
  52.             file.write( sResponse )
  53.             file.close()
  54.  
  55.             print("Success.")
  56.  
  57.  
  58.         else
  59.             error("No file exists.")
  60.         end
  61.     else
  62.         error( "Connection failure." )
  63.         exit(1)
  64.     end
  65.    
  66. elseif tArgs[1] == "proj" then
  67.  
  68.  
  69.     if not fs.exists("/split") then
  70.         print("---Getting get dependencies---")
  71.         ret, err = pcall( shell.run, "/get", "file", "/split")
  72.  
  73.         if not ret then
  74.             error("Unable to get dependency \"split\":\n"..err)
  75.         else
  76.             print()
  77.         end
  78.     end
  79.  
  80.     dofile("/split")
  81.  
  82.     -- Determine proj to download
  83.     local sFile = tArgs[2]
  84.     print( "---Get proj from "..hostname.."---" )
  85.     write("Connecting.. ")
  86.     local response = http.get( hostname..page.."?p="..sFile )
  87.  
  88.     if response then
  89.         local sResponse = response.readAll()
  90.         print("Success.")
  91.         write("Extracting project info.. ")  
  92.  
  93.         if sResponse and sResponse ~= "" then  
  94.             projArr = split(sResponse, '\n')
  95.             print( "Success." )
  96.             print("\n---Project Contents---")
  97.  
  98.             local projNum = table.getn(projArr)
  99.  
  100.             for i=1,projNum do
  101.                 print("> "..projArr[i])
  102.             end
  103.  
  104.             local dir = shell.dir()
  105.             if not dir or dir == "" then
  106.                 dir = "/"
  107.             end
  108.  
  109.             filePath = fs.combine(dir, sFile)
  110.             write("\nInstall project to \""..filePath.."\"? [Y/n] ")
  111.             input = read()
  112.  
  113.             if not input
  114.                 or (input == "n")
  115.                 or (input == "N")
  116.                 or (input == "no")
  117.                 or (input == "No")
  118.                 or (input == "NO")
  119.                 then
  120.                
  121.                 print("Installation cancelled by user.")
  122.                 return
  123.             end
  124.  
  125.             print("\n---Downloading files to "..filePath.."---")
  126.            
  127.             if fs.exists(filePath) then
  128.                 fs.delete(filePath)
  129.             end
  130.             fs.makeDir(filePath)
  131.  
  132.             shell.setDir(filePath)
  133.             for i=1,projNum do
  134.                 write("\n["..i.."/"..projNum.."]")
  135.                 ret, err = pcall( shell.run, "/get", "file", projArr[i])
  136.  
  137.                 if not ret then
  138.                     print("Error: One or more files failed to download:")
  139.                     print(err)
  140.                     break
  141.                 end
  142.             end
  143.             shell.setDir(dir)
  144.             if not ret then
  145.                 fs.delete(filePath)
  146.             end
  147.  
  148.         end
  149.     end
  150. else
  151.     printUsage()
  152. end
Advertisement
Add Comment
Please, Sign In to add comment