-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_tg.py
More file actions
33 lines (29 loc) · 909 Bytes
/
parse_tg.py
File metadata and controls
33 lines (29 loc) · 909 Bytes
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
import json
filename = "veronika"
with_names = True
with open(filename + ".json", encoding="utf-8") as f:
content = json.load(f)["messages"]
results = []
for item in content:
if not "from" in item:
continue
is_sticker = "sticker_emoji" in item
prefix = f"{item['from']}: " if with_names else ""
plain_text = ""
for c in item["text"]:
if type(c) is str:
plain_text += c
else:
plain_text += c["text"]
if "\n" in plain_text:
continue
is_text = len(plain_text) > 0
assert not is_sticker or not is_text
if is_sticker:
results += [prefix + item["sticker_emoji"]]
elif is_text:
results += [prefix + plain_text]
print(len(results), "messages")
with open(f"{filename}_messages_{['anon', 'names'][with_names]}.txt", "w", encoding="utf-8") as f:
for line in results:
f.write(line + "\n")