Miscellaneous scripts
This repository contains miscellaneous scripts that does not fit in one repository, yet I will use them sometimes for my personal use. Note that some of the scripts might contain hardcoded paths and opinionated presets, and you are advised to inspect them before actually using.
Loading...
Searching...
No Matches
telegram.json2txt.py
Go to the documentation of this file.
1import json
2import sys
3from pathlib import Path
4
5MEDIA = {
6 "sticker": "[Sticker]",
7 "animation": "[GIF]",
8 "voice_message": "[Voice]",
9 "video_file": "[Video]",
10 "audio_file": "[Audio]",
11 "video_message": "[VideoNote]",
12}
13
14
15def text(raw):
16 if isinstance(raw, str):
17 return raw.replace("\n", " ")
18 return "".join(p if isinstance(p, str) else p.get("text", "") for p in raw).replace("\n", " ")
19
20
21def fmt(msg):
22 if msg.get("type") != "message":
23 return None
24 ts = msg["date"][:16].replace("T", " ")
25 sender = msg.get("from") or msg.get("actor") or "Unknown"
26 parts = [f"[{ts}] {sender}:"]
27 if rid := msg.get("reply_to_message_id"):
28 parts.append(f"[reply:{rid}]")
29 if mt := msg.get("media_type"):
30 parts.append(MEDIA.get(mt, f"[{mt}]"))
31 if "photo" in msg:
32 parts.append("[Photo]")
33 if body := text(msg.get("text", "")):
34 parts.append(body)
35 return " ".join(parts)
36
37
38src = Path(sys.argv[1] if len(sys.argv) > 1 else "result.json")
39dst = Path(sys.argv[2] if len(sys.argv) > 2 else src.with_suffix(".txt"))
40
41data = json.loads(src.read_text(encoding="utf-8"))
42lines = list(filter(None, map(fmt, data["messages"])))
43dst.write_text("\n".join(lines) + "\n", encoding="utf-8")
44print(f"{len(lines)} messages -> {dst}")
constexpr std::string_view map
Definition raycaster.cpp:43