Add ability to list speedtest.net servers and specify a server to test against. Fixes #4

This commit is contained in:
Matt Martz 2013-01-30 10:49:32 -06:00
parent e67ee1663c
commit f3040c534f
2 changed files with 86 additions and 29 deletions

View File

@ -192,7 +192,7 @@ def getConfig():
return config return config
def closestServers(client): def closestServers(client, all=False):
"""Determine the 5 closest speedtest.net servers based on geographic """Determine the 5 closest speedtest.net servers based on geographic
distance distance
""" """
@ -208,6 +208,7 @@ def closestServers(client):
attrib = dict(server.attributes.items()) attrib = dict(server.attributes.items())
d = distance([float(client['lat']), float(client['lon'])], d = distance([float(client['lat']), float(client['lon'])],
[float(attrib.get('lat')), float(attrib.get('lon'))]) [float(attrib.get('lat')), float(attrib.get('lon'))])
attrib['d'] = d
if d not in servers: if d not in servers:
servers[d] = [attrib] servers[d] = [attrib]
else: else:
@ -217,7 +218,7 @@ def closestServers(client):
for d in sorted(servers.keys()): for d in sorted(servers.keys()):
for s in servers[d]: for s in servers[d]:
closest.append(s) closest.append(s)
if(len(closest) == 5): if len(closest) == 5 and not all:
break break
else: else:
continue continue
@ -278,6 +279,10 @@ def speedtest():
parser.add_argument('--simple', action='store_true', parser.add_argument('--simple', action='store_true',
help='Suppress verbose output, only show basic ' help='Suppress verbose output, only show basic '
'information') 'information')
parser.add_argument('--list', action='store_true',
help='Display a list of speedtest.net servers '
'sorted by distance')
parser.add_argument('--server', help='Specify a server ID to test against')
options = parser.parse_args() options = parser.parse_args()
if isinstance(options, tuple): if isinstance(options, tuple):
@ -292,17 +297,40 @@ def speedtest():
if not args.simple: if not args.simple:
print 'Retrieving speedtest.net server list...' print 'Retrieving speedtest.net server list...'
if args.list or args.server:
servers = closestServers(config['client'], True)
if args.list:
serverList = []
for server in servers:
line = ('%(id)4s) %(sponsor)s (%(name)s, %(country)s) '
'[%(d)0.2f km]' % server)
serverList.append(line)
try:
print '\n'.join(serverList).encode('utf-8', 'ignore')
except IOError:
pass
sys.exit(0)
else:
servers = closestServers(config['client']) servers = closestServers(config['client'])
if not args.simple: if not args.simple:
client = dict(config['client'].items()) print 'Testing from %(isp)s (%(ip)s)...' % config['client']
print 'Testing From: %(isp)s - %(ip)s' % client
if args.server:
try:
best = getBestServer(filter(lambda x: x['id'] == args.server,
servers))
except IndexError:
print 'Invalid server ID'
sys.exit(1)
else:
if not args.simple: if not args.simple:
print 'Selecting best server based on ping...' print 'Selecting best server based on ping...'
best = getBestServer(servers) best = getBestServer(servers)
if not args.simple: if not args.simple:
print 'Hosted by %(sponsor)s (%(name)s): %(latency)sms' % best print ('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best)
else: else:
print 'Ping: %(latency)s ms' % best print 'Ping: %(latency)s ms' % best

View File

@ -181,7 +181,7 @@ def getConfig():
return config return config
def closestServers(client): def closestServers(client, all=False):
"""Determine the 5 closest speedtest.net servers based on geographic """Determine the 5 closest speedtest.net servers based on geographic
distance distance
""" """
@ -196,6 +196,7 @@ def closestServers(client):
for server in root[0]: for server in root[0]:
d = distance([float(client['lat']), float(client['lon'])], d = distance([float(client['lat']), float(client['lon'])],
[float(server.get('lat')), float(server.get('lon'))]) [float(server.get('lat')), float(server.get('lon'))])
server.attrib['d'] = d
if d not in servers: if d not in servers:
servers[d] = [server.attrib] servers[d] = [server.attrib]
else: else:
@ -205,7 +206,7 @@ def closestServers(client):
for d in sorted(servers.keys()): for d in sorted(servers.keys()):
for s in servers[d]: for s in servers[d]:
closest.append(s) closest.append(s)
if(len(closest) == 5): if len(closest) == 5 and not all:
break break
else: else:
continue continue
@ -262,6 +263,10 @@ def speedtest():
parser.add_argument('--simple', action='store_true', parser.add_argument('--simple', action='store_true',
help='Suppress verbose output, only show basic ' help='Suppress verbose output, only show basic '
'information') 'information')
parser.add_argument('--list', action='store_true',
help='Display a list of speedtest.net servers '
'sorted by distance')
parser.add_argument('--server', help='Specify a server ID to test against')
args = parser.parse_args() args = parser.parse_args()
if not args.simple: if not args.simple:
@ -270,17 +275,41 @@ def speedtest():
if not args.simple: if not args.simple:
print('Retrieving speedtest.net server list...') print('Retrieving speedtest.net server list...')
#servers = closestServers(config['client'])
if args.list or args.server:
servers = closestServers(config['client'], True)
if args.list:
serverList = []
for server in servers:
line = ('%(id)4s) %(sponsor)s (%(name)s, %(country)s) '
'[%(d)0.2f km]' % server)
serverList.append(line)
try:
print('\n'.join(serverList))
except IOError:
pass
sys.exit(0)
else:
servers = closestServers(config['client']) servers = closestServers(config['client'])
if not args.simple: if not args.simple:
client = dict(config['client'].items()) print('Testing from %(isp)s (%(ip)s)...' % config['client'])
print('Testing From: %(isp)s - %(ip)s' % client)
if args.server:
try:
best = getBestServer(filter(lambda x: x['id'] == args.server,
servers))
except IndexError:
print('Invalid server ID')
sys.exit(1)
else:
if not args.simple: if not args.simple:
print('Selecting best server based on ping...') print('Selecting best server based on ping...')
best = getBestServer(servers) best = getBestServer(servers)
if not args.simple: if not args.simple:
print('Hosted by %(sponsor)s (%(name)s): %(latency)sms' % best) print('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
'%(latency)s ms' % best)
else: else:
print('Ping: %(latency)s ms' % best) print('Ping: %(latency)s ms' % best)
@ -307,7 +336,7 @@ def speedtest():
ulspeed = uploadSpeed(best['url'], sizes, args.simple) ulspeed = uploadSpeed(best['url'], sizes, args.simple)
if not args.simple: if not args.simple:
print() print()
print('Upload speed: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2)) print('Upload: %s Mbit/s' % round((ulspeed / 1024 / 1024) * 8, 2))
if args.share: if args.share:
dlspeedk = int(round((dlspeed / 1024) * 8, 0)) dlspeedk = int(round((dlspeed / 1024) * 8, 0))