Support JSON server list in addition to XML server list(s)

The JSON server list (https://www.speedtest.net/api/js/servers) is what the
Speedtest.net web interface currently uses.
This commit is contained in:
Daniel Lenski 2021-12-30 16:03:50 -05:00
parent ca4f4a3222
commit 0096adccf7
1 changed files with 41 additions and 25 deletions

View File

@ -1286,6 +1286,7 @@ class Speedtest(object):
)
urls = [
'https://www.speedtest.net/api/js/servers',
'://www.speedtest.net/speedtest-servers-static.php',
'http://c.speedtest.net/speedtest-servers-static.php',
'://www.speedtest.net/speedtest-servers.php',
@ -1311,6 +1312,10 @@ class Speedtest(object):
raise ServersRetrievalError()
stream = get_response_stream(uh)
try:
is_json = uh.headers.getheader('content-type').startswith('application/json')
except AttributeError:
is_json = uh.getheader('content-type').startswith('application/json')
serversxml_list = []
while 1:
@ -1328,7 +1333,16 @@ class Speedtest(object):
raise ServersRetrievalError()
serversxml = ''.encode().join(serversxml_list)
attriblist = []
if is_json:
printer('Servers JSON:\n%s' % serversxml, debug=True)
try:
attriblist = json.loads(serversxml)
except (ValueError, json.JSONDecodeError):
raise ServersRetrievalError()
else:
printer('Servers XML:\n%s' % serversxml, debug=True)
try:
@ -1358,7 +1372,9 @@ class Speedtest(object):
attrib = server.attrib
except AttributeError:
attrib = dict(list(server.attributes.items()))
attriblist.append(attrib)
for attrib in attriblist:
if servers and int(attrib.get('id')) not in servers:
continue