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 Namespace Reference

Functions

 fmt (msg)
 text (raw)

Variables

 data = json.loads(src.read_text(encoding="utf-8"))
 dst = Path(sys.argv[2] if len(sys.argv) > 2 else src.with_suffix(".txt"))
 encoding
 lines = list(filter(None, map(fmt, data["messages"])))
dict MEDIA
 src = Path(sys.argv[1] if len(sys.argv) > 1 else "result.json")

Function Documentation

◆ fmt()

telegram.fmt ( msg)

Definition at line 21 of file telegram.json2txt.py.

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

References text().

Here is the call graph for this function:

◆ text()

telegram.text ( raw)

Definition at line 15 of file telegram.json2txt.py.

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

Referenced by fmt().

Here is the caller graph for this function:

Variable Documentation

◆ data

telegram.data = json.loads(src.read_text(encoding="utf-8"))

Definition at line 41 of file telegram.json2txt.py.

◆ dst

telegram.dst = Path(sys.argv[2] if len(sys.argv) > 2 else src.with_suffix(".txt"))

Definition at line 39 of file telegram.json2txt.py.

◆ encoding

telegram.encoding

Definition at line 43 of file telegram.json2txt.py.

◆ lines

telegram.lines = list(filter(None, map(fmt, data["messages"])))

Definition at line 42 of file telegram.json2txt.py.

◆ MEDIA

dict telegram.MEDIA
Initial value:
1= {
2 "sticker": "[Sticker]",
3 "animation": "[GIF]",
4 "voice_message": "[Voice]",
5 "video_file": "[Video]",
6 "audio_file": "[Audio]",
7 "video_message": "[VideoNote]",
8}

Definition at line 5 of file telegram.json2txt.py.

◆ src

telegram.src = Path(sys.argv[1] if len(sys.argv) > 1 else "result.json")

Definition at line 38 of file telegram.json2txt.py.