-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
350 lines (294 loc) · 12.2 KB
/
export.py
File metadata and controls
350 lines (294 loc) · 12.2 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python3
"""
Clean Matrix channel export.json to a minimal schema for publishing.
Output format:
{
"messages": [ ... ],
"processed_ids": [ "event_id", ... ],
"last_processed_ts": 1771110622252
}
Types: journal (short reading notes), link, question, idea, project, field_note, blog_post, reply
- Link-only posts → type "link" (even if posted with 📥)
- Reading with substantive text → type "journal"
"""
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
# Emoji → type mapping (reading → journal for posts with text; link-only overridden to link)
EMOJI_TO_TYPE = {
"📥": "journal", # short reading notes; link-only gets overridden to "link"
"🔗": "link",
"❓": "question",
"💾": "project",
"💡": "idea",
"📔": "field_note",
"📄": "blog_post",
}
CATEGORY_EMOJIS = list(EMOJI_TO_TYPE.keys())
def _clean_text_for_keywords(body: str) -> str:
"""Strip emoji, URLs, markdown, and metadata so YAKE sees clean prose."""
text = body or ""
# Remove "originally posted on ..." metadata
text = re.sub(r"_?\s*originally posted[^_\n]*_?", "", text, flags=re.I)
for emoji in CATEGORY_EMOJIS:
text = text.replace(emoji + "\uFE0F", "").replace(emoji, "")
text = re.sub(r"https?://[^\s\)\]]+", "", text)
text = re.sub(r"\[([^\]]+)\]\([^\)]+\)", r"\1", text) # [text](url) → text
text = re.sub(r"[#*_`~>|]", " ", text) # strip markdown punctuation
text = re.sub(r"\s+", " ", text).strip()
return text
def extract_keywords(body: str) -> list:
"""
Auto-extract keywords from post text using YAKE + hashtags.
Falls back to hashtag-only extraction if YAKE is unavailable.
"""
keywords = []
body = body or ""
# Explicit #hashtags (always honoured — they're intentional)
hashtags = re.findall(r"#(\w[\w\-]*)", body)
keywords.extend(hashtags)
# Auto-extract with YAKE
clean = _clean_text_for_keywords(body)
if len(clean) > 30: # skip very short posts — not enough signal
try:
import yake
extractor = yake.KeywordExtractor(
lan="en", n=3, top=10, dedupLim=0.5,
)
for phrase, _score in extractor.extract_keywords(clean):
phrase = phrase.strip()
if phrase.lower() not in [k.lower() for k in keywords]:
keywords.append(phrase)
except ImportError:
pass # YAKE not installed — hashtags only
return list(dict.fromkeys(keywords))[:10] # Dedupe, limit 10
def get_message_content(msg: dict) -> tuple[str, str]:
"""Extract (body, formatted_body) from a message. formatted_body may be empty."""
content = msg.get("content") or {}
body = content.get("body") or ""
formatted_body = content.get("formatted_body") or ""
return body, formatted_body
def is_link_only(body: str) -> bool:
"""True if the post is essentially just a URL or citation (no substantive prose)."""
# Strip metadata and emoji
body = re.sub(r"_\s*originally posted[^_]*_", "", body, flags=re.I)
body = re.sub(r"originally posted[^\n]*", "", body, flags=re.I)
for emoji in CATEGORY_EMOJIS:
body = body.replace(emoji + "\uFE0F", "").replace(emoji, "") # variant first, then base
body = body.strip()
# Single markdown link [title](url) or bare URL = link
if re.match(r"^\s*\[([^\]]*)\]\(https?://[^\)]+\)\s*$", body):
return True
if re.match(r"^\s*https?://\S+\s*$", body):
return True
# Remove URLs; if little prose left, it's link-only
without_urls = re.sub(r"https?://[^\s\)\]]+", "", body)
without_urls = re.sub(r"\[([^\]]+)\]\([^\)]+\)", r"\1", without_urls) # [text](url) → text
without_urls = re.sub(r"\s+", " ", without_urls).strip()
return len(without_urls) < 100
def is_category_post(msg: dict) -> bool:
"""Check if a message is a root post in one of the publishable categories."""
if msg.get("type") != "m.room.message":
return False
content = msg.get("content") or {}
# Skip edits (m.replace) and thread replies - we only want root posts
relates_to = content.get("m.relates_to") or {}
if relates_to.get("rel_type") == "m.replace":
return False
if relates_to.get("rel_type") == "m.thread":
return False
body, _ = get_message_content(msg)
body = body.lstrip()
# Strip leading markdown (#, -, *, etc.) so "### 📔 Field Note" matches
body_normalized = re.sub(r"^[\s#\-*]+", "", body)
return any(body_normalized.startswith(emoji) for emoji in CATEGORY_EMOJIS)
def get_message_type(msg: dict, is_reply: bool) -> str:
"""Map message to type: journal, link, question, etc., or 'reply'."""
if is_reply:
return "reply"
body, _ = get_message_content(msg)
body = body.lstrip()
body_normalized = re.sub(r"^[\s#\-*]+", "", body)
for emoji, t in EMOJI_TO_TYPE.items():
if body_normalized.startswith(emoji) or body_normalized.startswith(emoji + "\uFE0F"):
if t == "journal" and is_link_only(body):
return "link"
return t
return "field_note" # fallback for matched-but-unusual format
def get_parent_id(msg: dict) -> str | None:
"""For replies, return the parent message id (from m.thread or m.in_reply_to)."""
content = msg.get("content") or {}
relates_to = content.get("m.relates_to") or {}
if relates_to.get("rel_type") == "m.thread":
return relates_to.get("event_id")
in_reply = relates_to.get("m.in_reply_to") or content.get("m.in_reply_to")
if isinstance(in_reply, dict) and in_reply.get("event_id"):
return in_reply["event_id"]
return None
def build_edit_map(messages: list) -> dict:
"""Build original_id -> (body, formatted_body) for the latest edit of each message."""
edits = {} # original_id -> (body, formatted_body)
for msg in messages:
if msg.get("type") != "m.room.message":
continue
content = msg.get("content") or {}
relates_to = content.get("m.relates_to") or {}
if relates_to.get("rel_type") != "m.replace":
continue
original_id = relates_to.get("event_id")
if not original_id:
continue
new_content = content.get("m.new_content") or content
body = new_content.get("body") or ""
formatted_body = new_content.get("formatted_body") or ""
edits[original_id] = (body, formatted_body)
return edits
def to_minimal_message(msg: dict, is_reply: bool, edit_map: dict) -> dict:
"""Convert a Matrix message to the minimal schema."""
eid = msg.get("event_id")
ts = msg.get("origin_server_ts", 0)
msg_type = get_message_type(msg, is_reply)
parent_id = get_parent_id(msg) if is_reply else None
body, formatted_body = get_message_content(msg)
if eid in edit_map:
body, formatted_body = edit_map[eid]
out = {
"id": eid,
"ts": ts,
"type": msg_type,
"body": body,
"parent_id": parent_id,
}
if formatted_body:
out["formatted_body"] = formatted_body
# Keywords for root posts (helps with sorting/filtering)
if not is_reply:
kw = extract_keywords(body)
if kw:
out["keywords"] = kw
return out
def process_messages(
messages: list,
existing_export: dict | None = None,
) -> dict:
"""Filter messages to minimal schema. Used by both file-based export and bot."""
already_processed = set()
existing_messages = []
existing_last_ts = 0
if existing_export:
already_processed = set(existing_export.get("processed_ids") or [])
existing_messages = existing_export.get("messages") or []
existing_last_ts = existing_export.get("last_processed_ts") or 0
new_only = [m for m in messages if m.get("event_id") not in already_processed]
# Keep existing roots so we can attach new replies to their threads
existing_root_ids = {m["id"] for m in existing_messages if not m.get("parent_id")}
else:
new_only = messages
existing_root_ids = set()
category_root_ids = set(existing_root_ids)
for msg in new_only:
if is_category_post(msg):
eid = msg.get("event_id")
if eid:
category_root_ids.add(eid)
keep_ids = set(category_root_ids)
for msg in new_only:
if msg.get("type") != "m.room.message":
continue
root_id = get_parent_id(msg)
if root_id and root_id in category_root_ids:
keep_ids.add(msg.get("event_id"))
changed = True
while changed:
changed = False
for msg in new_only:
if msg.get("type") != "m.room.message":
continue
eid = msg.get("event_id")
if eid in keep_ids:
continue
parent_id = get_parent_id(msg)
if parent_id and parent_id in keep_ids:
keep_ids.add(eid)
changed = True
kept = [m for m in new_only if m.get("event_id") in keep_ids]
kept.sort(key=lambda m: m.get("origin_server_ts", 0))
edit_map = build_edit_map(messages) # use full list for edit resolution
minimal_new = []
for m in kept:
eid = m.get("event_id")
is_reply = eid not in category_root_ids
minimal_new.append(to_minimal_message(m, is_reply, edit_map))
if existing_messages:
all_messages = existing_messages + minimal_new
all_messages.sort(key=lambda m: m.get("ts", 0))
minimal = all_messages
processed_ids = sorted(already_processed | keep_ids)
else:
minimal = minimal_new
processed_ids = sorted(keep_ids)
last_processed_ts = max(
[m.get("origin_server_ts", 0) for m in kept] + [existing_last_ts],
default=0,
)
return {
"messages": minimal,
"processed_ids": processed_ids,
"last_processed_ts": last_processed_ts,
}
def clean_export(input_path: str, output_path: str, incremental: bool = False) -> None:
"""Filter export to minimal schema: category posts and their threads."""
with open(input_path, encoding="utf-8") as f:
data = json.load(f)
messages = data.get("messages", [])
existing = None
if incremental and Path(output_path).exists():
try:
with open(output_path, encoding="utf-8") as f:
existing = json.load(f)
if existing.get("processed_ids"):
print(f" Incremental: {len(existing['processed_ids'])} already processed")
except (json.JSONDecodeError, OSError):
pass
out = process_messages(messages, existing_export=existing)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(out, f, indent=2, ensure_ascii=False)
print(f"Wrote {output_path}")
print(f" Original: {len(messages)} → {len(out['messages'])} messages")
print(f" processed_ids: {len(out['processed_ids'])}, last_processed_ts: {out['last_processed_ts']}")
def review_types(cleaned_path: str) -> None:
"""Print root messages with id, type, and body preview for manual review."""
with open(cleaned_path, encoding="utf-8") as f:
data = json.load(f)
for m in data.get("messages") or []:
if m.get("parent_id"):
continue
body = (m.get("body") or "")[:80].replace("\n", " ").rstrip()
suffix = "..." if len(m.get("body") or "") > 80 else ""
print(f"{m['id']}\t{m['type']}\t{body}{suffix}")
def main():
base = Path(__file__).parent
input_path = base / "export.json"
output_path = base / "content.json"
incremental = False
args = sys.argv[1:]
if "--review" in args:
args = [a for a in args if a != "--review"]
target = args[0] if args else str(base / "content.json")
review_types(target)
return
if "--incremental" in args:
incremental = True
args = [a for a in args if a != "--incremental"]
if args:
input_path = Path(args[0])
if len(args) > 1:
output_path = Path(args[1])
if not input_path.exists():
print(f"Error: {input_path} not found", file=sys.stderr)
sys.exit(1)
clean_export(str(input_path), str(output_path), incremental=incremental)
if __name__ == "__main__":
main()