1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
diff --git a/speedtest.py b/speedtest.py
index a33296d..e7490b1 100755
--- a/speedtest.py
+++ b/speedtest.py
@@ -20,6 +20,7 @@ import re
import csv
import sys
import math
+import json
import errno
import signal
import socket
@@ -1250,10 +1251,7 @@ class Speedtest(object):
)
urls = [
- '://www.speedtest.net/speedtest-servers-static.php',
- 'http://c.speedtest.net/speedtest-servers-static.php',
- '://www.speedtest.net/speedtest-servers.php',
- 'http://c.speedtest.net/speedtest-servers.php',
+ "://www.speedtest.net/api/js/servers",
]
headers = {}
@@ -1296,53 +1294,31 @@ class Speedtest(object):
printer('Servers XML:\n%s' % serversxml, debug=True)
try:
- try:
- try:
- root = ET.fromstring(serversxml)
- except ET.ParseError:
- e = get_exception()
- raise SpeedtestServersError(
- 'Malformed speedtest.net server list: %s' % e
- )
- elements = etree_iter(root, 'server')
- except AttributeError:
- try:
- root = DOM.parseString(serversxml)
- except ExpatError:
- e = get_exception()
- raise SpeedtestServersError(
- 'Malformed speedtest.net server list: %s' % e
- )
- elements = root.getElementsByTagName('server')
- except (SyntaxError, xml.parsers.expat.ExpatError):
+ elements = json.loads(serversxml)
+ except SyntaxError:
raise ServersRetrievalError()
for server in elements:
- try:
- attrib = server.attrib
- except AttributeError:
- attrib = dict(list(server.attributes.items()))
-
- if servers and int(attrib.get('id')) not in servers:
+ if servers and int(server.get('id')) not in servers:
continue
- if (int(attrib.get('id')) in self.config['ignore_servers']
- or int(attrib.get('id')) in exclude):
+ if (int(server.get('id')) in self.config['ignore_servers']
+ or int(server.get('id')) in exclude):
continue
try:
d = distance(self.lat_lon,
- (float(attrib.get('lat')),
- float(attrib.get('lon'))))
+ (float(server.get('lat')),
+ float(server.get('lon'))))
except Exception:
continue
- attrib['d'] = d
+ server['d'] = d
try:
- self.servers[d].append(attrib)
+ self.servers[d].append(server)
except KeyError:
- self.servers[d] = [attrib]
+ self.servers[d] = [server]
break
|