-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
executable file
·135 lines (115 loc) · 3.91 KB
/
nodes.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import re
import json
import requests
import StringIO
r = requests.get("https://nodes.wlan-si.net/network/map/#lat=46.17&long=14.96&zoom=8&type=m&project=1,3,6,7,8,9,10,11,13,15,18,19,20,21,23,24&status=up,visible,down,duped,new,pending", verify=False)
data = r.text
#open("nodes.wlan-si.net-map.html", 'w').write(data.encode('ascii', 'replace'))
#data = open("nodes.wlan-si.net-map.html").read()
parser = re.compile("var nodes = (.*?);", re.MULTILINE | re.DOTALL)
parsed = parser.search(data).groups()[0]
parsed = parsed.rstrip(' \n],') + ']'
nodes = json.loads(parsed.replace("'", '"'))
parser = re.compile("var links = (.*?);", re.MULTILINE | re.DOTALL)
parsed = parser.search(data).groups()[0]
parsed = parsed.rstrip(' \n],') + ']'
links = json.loads(parsed.replace("'", '"'))
kml = StringIO.StringIO()
kml.write("""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
""")
for status in ('up', 'down', 'duped', 'pending', 'visible'):
d = {'status': status,
}
kml.write("""
<Style id="%(status)s">
<IconStyle>
<Icon>
<href>https://nodes.wlan-si.net/images/status_%(status)s_gmap.png</href>
<hotSpot x="0.5" y="0.5" xunits="fractions" yunits="fractions"/>
</Icon>
</IconStyle>
<ListStyle>
</ListStyle>
<labelStyle>
<color>cc7faa55</color>
</labelStyle>
</Style>""" % d)
nodes = sorted(nodes, cmp=lambda a, b: cmp(a['name'], b['name']))
for n in nodes:
n['wname'] = n['name'].decode('utf-8')
n['wurl'] = n['url'].decode('utf-8')
kml.write("""
<Placemark id="%(pk)s">
<name>%(wname)s</name>
<styleUrl>#%(status)s</styleUrl>
<description>
<![CDATA[
Status: %(status)s<br>
<a href="https://nodes.wlan-si.net/node/%(pk)s/">more information</a>
]]>
</description>
<Point>
<coordinates>%(long)s,%(lat)s,0</coordinates>
</Point>
</Placemark>""" % n)
ip_to_node = dict(((node['ip'], node) for node in nodes))
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
for link in links:
link.update({'lat0': float(ip_to_node[link['src']]['lat']),
'lon0': float(ip_to_node[link['src']]['long']),
'lat1': float(ip_to_node[link['dst']]['lat']),
'lon1': float(ip_to_node[link['dst']]['long']),
'argb': '8f' + link['color'].lstrip('#'),
'id': (link['src']+'_to_'+link['dst']).replace('.','_')})
dist_km = haversine(link['lon0'], link['lat0'],
link['lon1'], link['lat1'])
link['dist'] = dist_km
link['from'] = ip_to_node[link['src']]
link['from.pk'] = ip_to_node[link['src']]['pk']
link['from.name'] = ip_to_node[link['src']]['name']
link['to.pk'] = ip_to_node[link['dst']]['pk']
link['to.name'] = ip_to_node[link['dst']]['name']
kml.write("""
<Placemark>
<description>
<![CDATA[
From: <a href="https://nodes.wlan-si.net/node/%(from.pk)s/">%(from.name)s</a> (%(src)s)<br>
To: <a href="https://nodes.wlan-si.net/node/%(to.pk)s/">%(to.name)s</a> (%(dst)s)<br>
Distance: %(dist).2f km<br>
]]>
</description>
<Style>
<LineStyle>
<color>%(argb)s</color>
<width>4</width>
</LineStyle>
</Style>
<LineString id="%(id)s">
<coordinates>
%(lon0)s,%(lat0)s,0
%(lon1)s,%(lat1)s,0
</coordinates>
</LineString>
</Placemark>""" % link)
kml.write("""
</Document>
</kml>
""")
print kml.getvalue()