-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_builder.py
More file actions
66 lines (54 loc) · 2.19 KB
/
forward_builder.py
File metadata and controls
66 lines (54 loc) · 2.19 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
58
59
60
61
62
63
64
65
66
"""
OneBot 原始嵌套合并转发构建器
直接构造 dict 结构,通过 bot.call_action 发送
"""
MAX_CHUNK = 3500
MAX_PER_FWD = 8
def build_raw_forward(title: str, sections: list[dict], uin: int) -> list[list]:
"""返回 [outer_fwd_1, outer_fwd_2, ...],每个可传给 call_action 的 messages 参数。"""
valid = [s for s in sections if s.get("content", "").strip()]
batch_size = MAX_PER_FWD
results = []
total = max((len(valid) + batch_size - 1) // batch_size, 1)
for batch_idx in range(0, len(valid), batch_size):
batch = valid[batch_idx:batch_idx + batch_size]
outer = []
n = len(results) + 1
tag = f" ({n}/{total})" if total > 1 else ""
outer.append(_node(uin, f"📢 {title}{tag}"))
for sec in batch:
h = sec.get("heading", "")
c = sec.get("content", "")
sub = [x for x in sec.get("sub_sections", []) if x.get("content", "").strip()]
if len(sub) >= 2 or len(c) > 500:
inner = [_node(uin, f"━━━ {h} ━━━")]
for h5 in sub:
for chunk in _split(f"▸ {h5['heading']}\n{h5['content']}", MAX_CHUNK):
inner.append(_node(uin, chunk))
if not sub:
for chunk in _split(c, MAX_CHUNK):
inner.append(_node(uin, chunk))
outer.append({
"type": "node",
"data": {"user_id": uin, "nickname": "守望先锋补丁", "content": inner},
})
else:
for chunk in _split(f"━━━ {h} ━━━\n{c}", MAX_CHUNK):
outer.append(_node(uin, chunk))
results.append(outer)
return results
def _node(uin: int, text: str) -> dict:
return {"type": "node", "data": {"user_id": uin, "nickname": "守望先锋补丁", "content": text}}
def _split(text: str, n: int) -> list[str]:
if len(text) <= n:
return [text]
r = []
while len(text) > n:
i = text.rfind("\n", 0, n)
if i == -1:
i = n
r.append(text[:i].strip())
text = text[i:].strip()
if text:
r.append(text)
return r