-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·122 lines (111 loc) · 5.18 KB
/
main.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
# -- coding: utf-8 --
from __init__ import *
import html
localQuery = True
def draw(locationsRawList: list, output_path: str, file_name: str) -> None:
"""
绘制traceMap
:param locationsRawList: list, 需要绘制轨迹的经纬度信息,格式为[[lat0, lon0, msg0], [lat1, lon1, msg1], ...] (纬度,经度,城市名,信息)
:param output_path: str, 轨迹图保存路径
:param file_name: str, 轨迹图保存文件名
"""
# 计算中心
content = []
location_center_lat = (locationsRawList[0][0] + locationsRawList[-1][0]) / 2
if abs(locationsRawList[0][1] - locationsRawList[-1][1]) > 180:
location_center_lng = (locationsRawList[0][1] + locationsRawList[-1][1] + 360) / 2
else:
location_center_lng = (locationsRawList[0][1] + locationsRawList[-1][1]) / 2
content += 'map.centerAndZoom(new BMapGL.Point({}, {}), 4)\n'.format(location_center_lng, location_center_lat)
isIPv4 = (IPy.IP(locationsRawList[0][5]).version() == 4)
if isIPv4:
locationsRawList[0][5] = str(IPy.IP(locationsRawList[0][5]).make_net('24'))
locationsRawList[-1][5] = str(IPy.IP(locationsRawList[-1][5]).make_net('24'))
else:
locationsRawList[0][5] = str(IPy.IP(locationsRawList[0][5]).make_net('48'))
locationsRawList[-1][5] = str(IPy.IP(locationsRawList[-1][5]).make_net('48'))
tableDataList = [[i[7], i[5], i[9], i[8], i[4], i[2]] for i in locationsRawList]
textList = []
for k, i in enumerate(locationsRawList):
lat = i[0]
lng = i[1]
text = i[5] + ' ' \
+ (('AS' + i[4]) if i[4] != '' else '') + ' ' \
+ 'TTL:' + i[7] + ' ' \
+ i[3] + ' ' \
+ 'RTT:' + i[8] + 'ms' # + i[6]
if k == len(locationsRawList) - 1:
textList.append(html.escape(text))
text = '<br>'.join(textList)
lat += random.uniform(-0.01, 0.01)
lng += random.uniform(-0.01, 0.01)
content += 'AddPathPoint(path, {}, {})\n'.format(lat, lng)
content += 'AddPoint(map, "{}", "{}", {}, {})\n'.format(i[2], text, lat, lng)
textList = []
break
if lat == locationsRawList[k + 1][0] and lng == locationsRawList[k + 1][1]:
textList.append(html.escape(text))
continue
else:
textList.append(html.escape(text))
text = '<br>'.join(textList)
lat += random.uniform(-0.01, 0.01)
lng += random.uniform(-0.01, 0.01)
content += 'AddPathPoint(path, {}, {})\n'.format(lat, lng)
content += 'AddPoint(map, "{}", "{}", {}, {})\n'.format(i[2], text, lat, lng)
textList = []
with open('template/template.html', 'r', encoding='utf-8') as f:
template = f.read()
new_content = (template.replace("%_REPLACE_CONTENT0_%", ''.join(content))).replace(
"%_REPLACE_CONTENT1_%", json.dumps(tableDataList, ensure_ascii=False)
)
with open(os.path.join(output_path, file_name), 'w', encoding='utf-8') as fp:
fp.write(new_content)
def process(rawData: dict, filename=str(int(datetime.datetime.now().timestamp())) + '.html') -> str:
"""
处理原始数据,获取HTML文件路径
:param filename: 导出的文件名,默认时间戳
:param rawData: dict, 原始数据
:return: str, HTML文件路径
"""
# print(rawData)
urlPrefix = "https://assets.nxtrace.org/tracemap/"
coordinatesList = []
for k in rawData['Hops']:
for j in k:
if j['Success']:
if 'lat' not in j['Geo']:
return "不受支持的版本,请更新至最新版本NextTrace。"
if j['Geo']['lat'] == 0 and j['Geo']['lng'] == 0:
continue
if j['Geo']['prov'] == "" and j['Geo']['country'] in ['中国', '美国', '俄罗斯']:
continue
tmpCity = ''
if j['Geo']['country'] != '':
tmpCity = j['Geo']['country']
if j['Geo']['prov'] != '':
tmpCity = j['Geo']['prov']
if j['Geo']['city'] != '':
tmpCity = j['Geo']['city']
coordinatesList.append(
[
j['Geo']['lat'],
j['Geo']['lng'],
tmpCity,
j['Geo']['owner'],
j['Geo']['asnumber'] if 'asnumber' in j['Geo'] else '',
j['Address']['IP'] if 'IP' in j['Address'] else '',
j['whois'] if 'whois' in j else '',
f'{j["TTL"]}' if 'TTL' in j else '',
f'{(j["RTT"] / 1_000_000):.2f}' if 'RTT' in j else '', # unit: ms
j['Hostname'] if 'Hostname' in j else ''
]
)
break
if (len(coordinatesList) == 0):
return "没有需要绘制的数据。"
draw(coordinatesList, './html', filename)
return urlPrefix + filename
if __name__ == '__main__':
json.load(open('test/test.json', 'r'))
print(process(json.load(open('test/test.json', 'r')), filename='demo1.html'))