Implement case insensitive country filter with --country option and update README accordingly

This commit is contained in:
Benjamin Piouffle 2016-10-17 09:55:27 +11:00
parent 7b09d8759f
commit 6d99912878
2 changed files with 14 additions and 1 deletions

View File

@ -92,6 +92,7 @@ Usage
--list Display a list of speedtest.net servers sorted by --list Display a list of speedtest.net servers sorted by
distance distance
--server SERVER Specify a server ID to test against --server SERVER Specify a server ID to test against
--country COUNTRY Select the best server matching country name
--mini MINI URL of the Speedtest Mini server --mini MINI URL of the Speedtest Mini server
--source SOURCE Source IP address to bind to --source SOURCE Source IP address to bind to
--timeout TIMEOUT HTTP timeout in seconds. Default 10 --timeout TIMEOUT HTTP timeout in seconds. Default 10

View File

@ -583,6 +583,8 @@ def speedtest():
help='Display a list of speedtest.net servers ' help='Display a list of speedtest.net servers '
'sorted by distance') 'sorted by distance')
parser.add_argument('--server', help='Specify a server ID to test against') parser.add_argument('--server', help='Specify a server ID to test against')
parser.add_argument('--country', type=str.lower,
help='Select the best server matching country name')
parser.add_argument('--mini', help='URL of the Speedtest Mini server') parser.add_argument('--mini', help='URL of the Speedtest Mini server')
parser.add_argument('--source', help='Source IP address to bind to') parser.add_argument('--source', help='Source IP address to bind to')
parser.add_argument('--timeout', default=10, type=int, parser.add_argument('--timeout', default=10, type=int,
@ -627,7 +629,7 @@ 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: if args.list or args.server or args.country:
servers = closestServers(config['client'], True) servers = closestServers(config['client'], True)
if args.list: if args.list:
serverList = [] serverList = []
@ -650,6 +652,16 @@ def speedtest():
except IndexError: except IndexError:
print_('Invalid server ID') print_('Invalid server ID')
sys.exit(1) sys.exit(1)
elif args.country:
try:
if not args.simple:
print_('Selecting best server in %s based on latency...' %
args.country.capitalize())
best = getBestServer(filter(lambda x: x['country'].lower() ==
args.country, servers))
except IndexError:
print_('No server available for this country')
sys.exit(1)
elif args.mini: elif args.mini:
name, ext = os.path.splitext(args.mini) name, ext = os.path.splitext(args.mini)
if ext: if ext: