Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #!/usr/bin/xcrun swift
  2.  
  3. // A script to take the results of "speedtest-cli" and convert it into csv data.
  4.  
  5. // This script requires "speedtest-cli" to be installed via `pip`. It will print
  6. // out the results in csv format like the following: "Date,Time,Ping,Download,Upload"
  7. //
  8. // e.g.:
  9. // 2/8/16,9:13:00 AM,24.456,55.72,24.03
  10.  
  11. // This script is intended to be run as a "shell script", i.e.:
  12. // $> chmod +x speedtest.swift
  13. // $> ./speedtest.swift
  14.  
  15. import Foundation
  16.  
  17. func exec(command: String) -> (output: String, exitStatus: Int) {
  18. let tokens = command.componentsSeparatedByString(" ")
  19. let launchPath = tokens[0]
  20. let arguments = tokens.dropFirst(1)
  21.  
  22. let task = NSTask()
  23. task.launchPath = launchPath
  24. task.arguments = Array(arguments)
  25. let stdout = NSPipe()
  26. task.standardOutput = stdout
  27.  
  28. task.launch()
  29. task.waitUntilExit()
  30.  
  31. let outData = stdout.fileHandleForReading.readDataToEndOfFile()
  32. let outStr = String(data: outData, encoding: NSUTF8StringEncoding)!
  33. return (outStr, Int(task.terminationStatus))
  34. }
  35.  
  36. let result = exec("/usr/local/bin/speedtest --simple")
  37.  
  38. let results = result
  39. .output
  40. .componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
  41. .prefix(3)
  42. .flatMap { string -> String? in
  43. let components = string.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
  44. return components.count > 1 ? components[1] : nil
  45. }
  46.  
  47. let date = NSDate()
  48. let dateFormatter = NSDateFormatter()
  49.  
  50. dateFormatter.dateStyle = .ShortStyle
  51. dateFormatter.timeStyle = .NoStyle
  52.  
  53. let dateString = dateFormatter.stringFromDate(date)
  54.  
  55. dateFormatter.dateStyle = .NoStyle
  56. dateFormatter.timeStyle = .MediumStyle
  57.  
  58. let timeString = dateFormatter.stringFromDate(date)
  59.  
  60. let printables = [dateString, timeString] + results
  61. print(printables.joinWithSeparator(","))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement