-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_json.py
70 lines (48 loc) · 1.75 KB
/
build_json.py
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
import requests
import os
from lxml.html.soupparser import fromstring
import json
import io
import random
nodes = []
links = []
linked = []
if not os.path.isfile('pings.cache'):
data = requests.get("https://wondernetwork.com/pings").text
with open('pings.cache', 'wb') as f:
f.write(data.encode('utf-8'))
with open('pings.cache', 'rb') as f:
data = '\n'.join([x.decode('utf-8') for x in f.readlines()])
xml_data = fromstring(data)
m = 0
for a in xml_data.xpath("//td[contains(@class,'is-bucket')]/a"):
__, __, city_from, city_to = a.attrib['href'].split('/')
ping = a.text.strip()
if ping[-2:] == "ms":
ping = float(ping[:-2].replace(',', ''))
if city_from not in nodes:
nodes.append(city_from)
if city_to not in nodes:
nodes.append(city_to)
m = max(ping, m)
if (city_from, city_to) not in linked and (city_to, city_from) not in linked:
if random.randint(0, 20) == 0 or ping < 100:
links.append({'source': nodes.index(city_from), 'target': nodes.index(city_to), 'value': ping})
linked.append((city_from, city_to))
nodes_mapping = {}
name = '?'
group_id = 0
with io.open('nodes.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
line = line.strip()
if line:
if line[0] == '#':
name = line[1:]
group_id += 1
else:
nodes_mapping[line] = {'group': group_id, 'group_name': name}
nodes = [{'name': x, 'group': nodes_mapping[x]['group'], 'group_name': nodes_mapping[x]['group_name']} for x in nodes]
final = {'nodes': nodes, 'links': links}
with io.open('data.json', 'w', encoding='utf-8') as f:
json.dump(final, f)
print(len(links))