Skip to content

Commit 71aa024

Browse files
committed
迁移支持
1 parent 7c7a176 commit 71aa024

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

main.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,36 @@ def process(rawData: dict, filename=str(int(datetime.datetime.now().timestamp())
9191
# print(rawData)
9292
urlPrefix = "https://assets.nxtrace.org/tracemap/"
9393
coordinatesList = []
94-
for k in rawData['Hops']:
95-
for j in k:
96-
if j['Success']:
97-
if 'lat' not in j['Geo']:
94+
for hop_group in rawData.get('Hops', []):
95+
if not hop_group:
96+
continue
97+
for j in hop_group:
98+
if not j or not isinstance(j, dict):
99+
continue
100+
if j.get('Success'):
101+
geo = j.get('Geo') or {}
102+
if 'lat' not in geo:
98103
return "不受支持的版本,请更新至最新版本NextTrace。"
99-
if j['Geo']['lat'] == 0 and j['Geo']['lng'] == 0:
104+
if geo.get('lat') == 0 and geo.get('lng') == 0:
100105
continue
101-
if j['Geo']['prov'] == "" and j['Geo']['country'] in ['中国', '美国', '俄罗斯']:
106+
if geo.get('prov') == "" and geo.get('country') in ['中国', '美国', '俄罗斯']:
102107
continue
103108
tmpCity = ''
104-
if j['Geo']['country'] != '':
105-
tmpCity = j['Geo']['country']
106-
if j['Geo']['prov'] != '':
107-
tmpCity = j['Geo']['prov']
108-
if j['Geo']['city'] != '':
109-
tmpCity = j['Geo']['city']
109+
if geo.get('country'):
110+
tmpCity = geo['country']
111+
if geo.get('prov'):
112+
tmpCity = geo['prov']
113+
if geo.get('city'):
114+
tmpCity = geo['city']
115+
address = j.get('Address') or {}
110116
coordinatesList.append(
111117
[
112-
j['Geo']['lat'],
113-
j['Geo']['lng'],
118+
geo.get('lat'),
119+
geo.get('lng'),
114120
tmpCity,
115-
j['Geo']['owner'],
116-
j['Geo']['asnumber'] if 'asnumber' in j['Geo'] else '',
117-
j['Address']['IP'] if 'IP' in j['Address'] else '',
121+
geo.get('owner', ''),
122+
geo['asnumber'] if 'asnumber' in geo else '',
123+
address['IP'] if 'IP' in address else '',
118124
j['whois'] if 'whois' in j else '',
119125
f'{j["TTL"]}' if 'TTL' in j else '',
120126
f'{(j["RTT"] / 1_000_000):.2f}' if 'RTT' in j else '', # unit: ms

regenerate_html.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
3+
# Regenerate HTML outputs from stored JSON logs.
4+
# Processes each ./log/*.json through main.process() and writes ./html/<name>.html.
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
REPO_ROOT="${SCRIPT_DIR}"
10+
LOG_DIR="${REPO_ROOT}/log"
11+
HTML_DIR="${REPO_ROOT}/html"
12+
13+
if [[ ! -d "${LOG_DIR}" ]]; then
14+
echo "Log directory not found: ${LOG_DIR}" >&2
15+
exit 1
16+
fi
17+
18+
mkdir -p "${HTML_DIR}"
19+
20+
if ! find "${LOG_DIR}" -maxdepth 1 -type f -name '*.json' -print -quit | grep -q .; then
21+
echo "No JSON log files found in ${LOG_DIR}; nothing to regenerate." >&2
22+
exit 0
23+
fi
24+
25+
echo "Removing previous HTML outputs in ${HTML_DIR}"
26+
find "${HTML_DIR}" -maxdepth 1 -type f -name '*.html' -exec rm -f {} +
27+
28+
(
29+
cd "${REPO_ROOT}"
30+
python3 - "${LOG_DIR}" "${HTML_DIR}" <<'PY'
31+
import json
32+
import traceback
33+
import sys
34+
from pathlib import Path
35+
36+
from main import process
37+
38+
log_dir = Path(sys.argv[1])
39+
html_dir = Path(sys.argv[2])
40+
41+
for idx, json_path in enumerate(log_dir.glob("*.json"), start=1):
42+
output_name = json_path.stem + ".html"
43+
print(f"[{idx}] Generating {output_name} from {json_path.name}", flush=True)
44+
try:
45+
with json_path.open("r", encoding="utf-8") as fh:
46+
data = json.load(fh)
47+
result = process(data, filename=output_name)
48+
output_file = html_dir / output_name
49+
50+
if isinstance(result, str) and result.endswith(output_name) and output_file.exists():
51+
continue
52+
if output_file.exists():
53+
continue
54+
print(f"Skipping {json_path.name}; process() returned: {result}", file=sys.stderr, flush=True)
55+
except Exception as exc:
56+
print(f"Skipping {json_path.name}; error encountered: {exc}", file=sys.stderr, flush=True)
57+
traceback.print_exc()
58+
59+
print(f"HTML regeneration complete. Files saved to {html_dir}")
60+
PY
61+
)

0 commit comments

Comments
 (0)