-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyearbook_export.py
More file actions
57 lines (49 loc) · 3.23 KB
/
Copy pathyearbook_export.py
File metadata and controls
57 lines (49 loc) · 3.23 KB
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
"""从本地累计记录生成无外部依赖的交通年鉴 HTML。"""
from __future__ import annotations
import html
import json
import os
import trip_analytics as analytics
def _e(value):
return html.escape(str(value or ""))
def build_html(transactions, year=None, title="NEKO TRANSPORT YEARBOOK"):
summary = analytics.year_summary(transactions, year)
chosen = [t for t in transactions
if analytics._date(t) and analytics._date(t).year == summary["year"]]
routes = analytics.route_frequencies(chosen, directed=False)[:12]
stations = analytics.station_frequencies(chosen)[:12]
months = summary["months"]
max_trips = max([m["trips"] for m in months] or [1])
month_html = "".join(
f'<div class="barrow"><span>{_e(m["month"])}</span>'
f'<i style="width:{m["trips"]/max_trips*100:.1f}%"></i><b>{m["trips"]}</b></div>'
for m in months)
route_html = "".join(
f'<tr><td>{_e(r["in_station"])} → {_e(r["out_station"])}</td>'
f'<td>{_e(r["line"])}</td><td>{r["count"]}</td></tr>' for r in routes)
station_html = "".join(
f'<tr><td>{_e(s["station"])}</td><td>{s["count"]}</td>'
f'<td>{_e(s["first"])}</td></tr>' for s in stations)
data_json = html.escape(json.dumps(summary, ensure_ascii=False, default=str))
return f"""<!doctype html><html lang="zh"><head><meta charset="utf-8">
<title>{_e(title)} {summary['year']}</title><style>
body{{background:#03140A;color:#62FF85;font:14px Consolas,'Microsoft YaHei',monospace;margin:0;padding:32px}}
h1,h2{{color:#3CFF6A;letter-spacing:2px}} .grid{{display:grid;grid-template-columns:repeat(4,1fr);gap:12px}}
.card{{border:1px solid #1f8f3a;background:#0A2412;padding:18px}} .n{{font-size:30px;font-weight:bold}}
section{{margin-top:28px}} table{{width:100%;border-collapse:collapse}}td,th{{border-bottom:1px solid #1f8f3a;padding:8px;text-align:left}}
.barrow{{display:flex;align-items:center;gap:8px;margin:7px 0}}.barrow span{{width:70px}}.barrow i{{height:12px;background:#3CFF6A;min-width:2px}}.barrow b{{color:#FFB000}}
.muted{{color:#1FB04A}}@media(max-width:720px){{.grid{{grid-template-columns:1fr 1fr}}}}
</style></head><body><h1>[ {summary['year']} TRANSPORT YEARBOOK ]</h1><p class="muted">{_e(title)} · LOCAL DATA</p>
<div class="grid"><div class="card"><div class="n">{summary['records']}</div>记录</div>
<div class="card"><div class="n">{summary['active_days']}</div>活跃日</div>
<div class="card"><div class="n">{summary['stations']}</div>车站</div>
<div class="card"><div class="n">{summary['routes']}</div>唯一区间</div></div>
<section><h2>月度行程</h2>{month_html or '<p>暂无</p>'}</section>
<section><h2>常用路线</h2><table><tr><th>区间</th><th>线路</th><th>次数</th></tr>{route_html}</table></section>
<section><h2>最常到访车站</h2><table><tr><th>车站</th><th>经过</th><th>首次</th></tr>{station_html}</table></section>
<details><summary>机器可读摘要</summary><pre>{data_json}</pre></details></body></html>"""
def export(path, transactions, year=None, title="NEKO TRANSPORT YEARBOOK"):
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(build_html(transactions, year, title))
return path