Add support for --csv and --json arguments as output types
This commit is contained in:
parent
3b29d5701d
commit
79c3a19792
|
@ -417,6 +417,15 @@ class SpeedtestResults(object):
|
||||||
|
|
||||||
return self._share
|
return self._share
|
||||||
|
|
||||||
|
def simple(self, units):
|
||||||
|
return """Ping: %s ms
|
||||||
|
Download: %0.2f M%s/s
|
||||||
|
Upload: %0.2f M%s/s""" % (self.ping,
|
||||||
|
(self.download / 1000 / 1000) * units[1],
|
||||||
|
units[0],
|
||||||
|
(self.upload / 1000 / 1000) * units[1],
|
||||||
|
units[0])
|
||||||
|
|
||||||
|
|
||||||
class Speedtest(object):
|
class Speedtest(object):
|
||||||
"""Class for performing standard speedtest.net testing operations"""
|
"""Class for performing standard speedtest.net testing operations"""
|
||||||
|
@ -801,9 +810,16 @@ def parse_args():
|
||||||
parser.add_argument('--share', action='store_true',
|
parser.add_argument('--share', action='store_true',
|
||||||
help='Generate and provide a URL to the speedtest.net '
|
help='Generate and provide a URL to the speedtest.net '
|
||||||
'share results image')
|
'share results image')
|
||||||
parser.add_argument('--simple', action='store_true',
|
parser.add_argument('--simple', action='store_true', default=False,
|
||||||
help='Suppress verbose output, only show basic '
|
help='Suppress verbose output, only show basic '
|
||||||
'information')
|
'information')
|
||||||
|
parser.add_argument('--csv', action='store_true', default=False,
|
||||||
|
help='Suppress verbose output, only show basic '
|
||||||
|
'information in CSV format of: '
|
||||||
|
'ServerId,Latency,Upload,Download')
|
||||||
|
parser.add_argument('--json', action='store_true', default=False,
|
||||||
|
help='Suppress verbose output, only show basic '
|
||||||
|
'information in JSON format')
|
||||||
parser.add_argument('--list', action='store_true',
|
parser.add_argument('--list', action='store_true',
|
||||||
help='Display a list of speedtest.net servers '
|
help='Display a list of speedtest.net servers '
|
||||||
'sorted by distance')
|
'sorted by distance')
|
||||||
|
@ -822,6 +838,11 @@ def parse_args():
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def printer(string, quiet=False, **kwargs):
|
||||||
|
if not quiet:
|
||||||
|
print_(string, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def shell():
|
def shell():
|
||||||
"""Run the full speedtest.net test"""
|
"""Run the full speedtest.net test"""
|
||||||
|
|
||||||
|
@ -841,17 +862,23 @@ def shell():
|
||||||
source = args.source
|
source = args.source
|
||||||
socket.socket = bound_socket
|
socket.socket = bound_socket
|
||||||
|
|
||||||
# Don't set a callback if we are running in simple mode
|
if args.simple or args.csv or args.json:
|
||||||
if args.simple:
|
quiet = True
|
||||||
|
else:
|
||||||
|
quiet = False
|
||||||
|
|
||||||
|
# Don't set a callback if we are running quietly
|
||||||
|
if quiet:
|
||||||
callback = None
|
callback = None
|
||||||
else:
|
else:
|
||||||
callback = print_dots
|
callback = print_dots
|
||||||
|
|
||||||
print_('Retrieving speedtest.net configuration...')
|
printer('Retrieving speedtest.net configuration...', quiet)
|
||||||
try:
|
try:
|
||||||
speedtest = Speedtest()
|
speedtest = Speedtest()
|
||||||
except ConfigRetrievalError:
|
except ConfigRetrievalError:
|
||||||
print_('Cannot retrieve speedtest configuration')
|
printer('Cannot retrieve speedtest configuration')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
if args.list:
|
if args.list:
|
||||||
try:
|
try:
|
||||||
|
@ -883,10 +910,11 @@ def shell():
|
||||||
if args.server:
|
if args.server:
|
||||||
servers.append(args.server)
|
servers.append(args.server)
|
||||||
|
|
||||||
if not args.simple:
|
printer('Testing from %(isp)s (%(ip)s)...' % speedtest.config['client'],
|
||||||
print_('Testing from %(isp)s (%(ip)s)...' % speedtest.config['client'])
|
quiet)
|
||||||
|
|
||||||
if not args.mini:
|
if not args.mini:
|
||||||
print_('Retrieving speedtest.net server list...')
|
printer('Retrieving speedtest.net server list...', quiet)
|
||||||
try:
|
try:
|
||||||
speedtest.get_servers(servers)
|
speedtest.get_servers(servers)
|
||||||
except NoMatchedServers:
|
except NoMatchedServers:
|
||||||
|
@ -899,41 +927,43 @@ def shell():
|
||||||
print_('%s is an invalid server type, must be int' % args.server)
|
print_('%s is an invalid server type, must be int' % args.server)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not args.simple:
|
printer('Selecting best server based on ping...', quiet)
|
||||||
print_('Selecting best server based on ping...')
|
|
||||||
speedtest.get_best_server(speedtest.get_closest())
|
speedtest.get_best_server(speedtest.get_closest())
|
||||||
elif args.mini:
|
elif args.mini:
|
||||||
speedtest.get_best_server(speedtest.set_mini_server(args.mini))
|
speedtest.get_best_server(speedtest.set_mini_server(args.mini))
|
||||||
|
|
||||||
results = speedtest.results()
|
results = speedtest.results()
|
||||||
|
|
||||||
if args.simple:
|
|
||||||
print_('Ping: %(latency)s ms' % results.server)
|
|
||||||
else:
|
|
||||||
# Python 2.7 and newer seem to be ok with the resultant encoding
|
# Python 2.7 and newer seem to be ok with the resultant encoding
|
||||||
# from parsing the XML, but older versions have some issues.
|
# from parsing the XML, but older versions have some issues.
|
||||||
# This block should detect whether we need to encode or not
|
# This block should detect whether we need to encode or not
|
||||||
try:
|
try:
|
||||||
unicode()
|
unicode()
|
||||||
print_(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
printer(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' %
|
'%(latency)s ms' %
|
||||||
results.server).encode('utf-8', 'ignore'))
|
results.server).encode('utf-8', 'ignore'), quiet)
|
||||||
except NameError:
|
except NameError:
|
||||||
print_('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % results.server)
|
'%(latency)s ms' % results.server, quiet)
|
||||||
|
|
||||||
if not args.simple:
|
printer('Testing upload speed', quiet, end='')
|
||||||
print_('Testing upload speed', end='')
|
|
||||||
speedtest.download(callback=callback)
|
speedtest.download(callback=callback)
|
||||||
print_('Download: %0.2f M%s/s' %
|
printer('Download: %0.2f M%s/s' %
|
||||||
((results.download / 1000 / 1000) * args.units[1], args.units[0]))
|
((results.download / 1000 / 1000) * args.units[1], args.units[0]),
|
||||||
|
quiet)
|
||||||
|
|
||||||
if not args.simple:
|
printer('Testing download speed', quiet, end='')
|
||||||
print_('Testing download speed', end='')
|
|
||||||
speedtest.upload(callback=callback)
|
speedtest.upload(callback=callback)
|
||||||
print_('Upload: %0.2f M%s/s' %
|
printer('Upload: %0.2f M%s/s' %
|
||||||
((results.upload / 1000 / 1000) * args.units[1], args.units[0]))
|
((results.upload / 1000 / 1000) * args.units[1], args.units[0]),
|
||||||
|
quiet)
|
||||||
|
|
||||||
|
if args.simple:
|
||||||
|
print_(results.simple(args.units))
|
||||||
|
elif args.csv:
|
||||||
|
print_(results.csv())
|
||||||
|
elif args.json:
|
||||||
|
print_(repr(results.dict()).replace("'", '"'))
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue