This commit is contained in:
Dan Lenski 2023-10-05 11:36:08 +08:00 committed by GitHub
commit e9e9a5b9e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 43 deletions

View File

@ -1219,13 +1219,9 @@ class Speedtest(object):
sizes = { sizes = {
'upload': up_sizes[ratio - 1:], 'upload': up_sizes[ratio - 1:],
} }
if self._use_socket:
sizes['download'] = [245388, 505544, 1118012, 1986284, 4468241, sizes['download'] = [245388, 505544, 1118012, 1986284, 4468241,
7907740, 12407926, 17816816, 24262167, 7907740, 12407926, 17816816, 24262167,
31625365] 31625365]
else:
sizes['download'] = [350, 500, 750, 1000, 1500, 2000, 2500,
3000, 3500, 4000]
size_count = len(sizes['upload']) size_count = len(sizes['upload'])
@ -1290,6 +1286,7 @@ class Speedtest(object):
) )
urls = [ urls = [
'https://www.speedtest.net/api/js/servers',
'://www.speedtest.net/speedtest-servers-static.php', '://www.speedtest.net/speedtest-servers-static.php',
'http://c.speedtest.net/speedtest-servers-static.php', 'http://c.speedtest.net/speedtest-servers-static.php',
'://www.speedtest.net/speedtest-servers.php', '://www.speedtest.net/speedtest-servers.php',
@ -1315,6 +1312,10 @@ class Speedtest(object):
raise ServersRetrievalError() raise ServersRetrievalError()
stream = get_response_stream(uh) 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 = [] serversxml_list = []
while 1: while 1:
@ -1332,7 +1333,16 @@ class Speedtest(object):
raise ServersRetrievalError() raise ServersRetrievalError()
serversxml = ''.encode().join(serversxml_list) 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) printer('Servers XML:\n%s' % serversxml, debug=True)
try: try:
@ -1362,30 +1372,29 @@ class Speedtest(object):
attrib = server.attrib attrib = server.attrib
except AttributeError: except AttributeError:
attrib = dict(list(server.attributes.items())) attrib = dict(list(server.attributes.items()))
attriblist.append(attrib)
for attrib in attriblist:
if servers and int(attrib.get('id')) not in servers: if servers and int(attrib.get('id')) not in servers:
# Not one of the preselected servers
continue continue
if (int(attrib.get('id')) in self.config['ignore_servers'] if (int(attrib.get('id')) in self.config['ignore_servers']
or int(attrib.get('id')) in exclude): or int(attrib.get('id')) in exclude):
# One of the specifically excluded or ignored servers
continue continue
host, port = attrib['host'].split(':') host, port = attrib['host'].split(':')
attrib['host'] = (host, int(port)) attrib['host'] = (host, int(port))
try: try:
d = distance(self.lat_lon, d = attrib['d'] = distance(self.lat_lon,
(float(attrib.get('lat')), (float(attrib.get('lat')),
float(attrib.get('lon')))) float(attrib.get('lon'))))
except Exception: except Exception:
continue continue
attrib['d'] = d self.servers.setdefault(d, []).append(attrib)
try:
self.servers[d].append(attrib)
except KeyError:
self.servers[d] = [attrib]
break break
@ -1703,8 +1712,8 @@ class Speedtest(object):
for size in self.config['sizes']['download']: for size in self.config['sizes']['download']:
for _ in range(0, self.config['counts']['download']): for _ in range(0, self.config['counts']['download']):
urls.append( urls.append(
'%s/random%sx%s.jpg' % '%s/download?size=%d' %
(os.path.dirname(self.best['url']), size, size) (os.path.dirname(os.path.dirname(self.best['url'])), size)
) )
request_count = len(urls) request_count = len(urls)