Guest User

Untitled

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