-
Notifications
You must be signed in to change notification settings - Fork 26
/
telegram_chat_parser.py
277 lines (222 loc) · 8.63 KB
/
telegram_chat_parser.py
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
""" telegrama_chat_parser.py
author : Artur Rodrigues Rocha Neto
email : [email protected]
github : https://github.com/keizerzilla
created : 23/december/2020
description : Script to parse a Telegram chat history JSON file into CSV tabular format.
details : This script is a way to parse the most (but not all) of the Telegram Data Export Schema.
reference : https://core.telegram.org/import-export
requirements : Python 3.x
You can use this file as an executable script or importing the TelegramChatParser class in your project.
Have fun! I hope this little program helps you! (:
- Artur
"""
import csv
import json
import argparse
from datetime import datetime
def timestamp():
""" [AUXILIARY] Generates string with current timestamp.
Returns
-------
stamp: datetime string in '%Y-%m-%d_%H-%M-%S' format.
"""
now = datetime.now()
stamp = now.strftime("%Y-%m-%d_%H-%M-%S")
return stamp
def debug(msg):
""" [AUXILIARY] Simple debug function with runtime timestamp.
Parameters
----------
msg: Developer text to be shown in the default output.
"""
print(f"DEBUG | {timestamp()} | {msg}")
class TelegramChatParser:
""" The full chat parser abstraction in the form of a class.
"""
def __init__(self):
""" Le constructor.
"""
self.columns = [
"msg_id",
"sender",
"sender_id",
"reply_to_msg_id",
"date",
"date_unixtime",
"msg_type",
"msg_content",
"forwarded_from",
"action",
"has_mention",
"has_email",
"has_phone",
"has_hashtag",
"is_bot_command",
]
self.file_types = [
"animation",
"video_file",
"video_message",
"voice_message",
"audio_file",
]
self.mention_types = [
"mention",
"mention_name",
]
def process_message(self, message):
""" Parses the contents of a Telegram 'Message' object.
Parameters
----------
message: a dictionary in the structure of a 'Message' object.
Returns
-------
parsed_row: a row ready to be stored in the CSV.
"""
if message["type"] != "message":
return None
msg_id = message["id"]
sender = message["from"]
sender_id = message["from_id"]
date = message["date"]
date_unixtime = message["date_unixtime"]
reply_to_msg_id = message.get("reply_to_message_id", "")
action = message.get("action", "")
forwarded_from = message.get("forwarded_from", "")
has_mention = 0
has_email = 0
has_phone = 0
has_hashtag = 0
is_bot_command = 0
msg_content = message.get("text", "")
msg_type = "text"
if "media_type" in message:
msg_type = message["media_type"]
if message["media_type"] == "sticker":
if "sticker_emoji" in message:
msg_content = message["file"]
else:
msg_content = "?"
elif message["media_type"] in self.file_types:
msg_content = message["file"]
elif "file" in message:
msg_type = "file"
msg_content = message["file"]
if "photo" in message:
msg_type = "photo"
msg_content = message["photo"]
elif "poll" in message:
msg_type = "poll"
msg_content = str(message["poll"]["total_voters"])
elif "location_information" in message:
msg_type = "location"
loc = message["location_information"]
msg_content = f"{loc['latitude']},{loc['longitude']}"
if isinstance(msg_content, list):
txt_content = ""
for part in msg_content:
if isinstance(part, str):
txt_content += part
elif isinstance(part, dict):
if part["type"] == "link":
msg_type = "link"
elif part["type"] in self.mention_types:
has_mention = 1
elif part["type"] == "email":
has_email = 1
elif part["type"] == "phone":
has_phone = 1
elif part["type"] == "hashtag":
has_hashtag = 1
elif part["type"] == "bot_command":
is_bot_command = 1
txt_content += part["text"]
msg_content = txt_content
msg_content = msg_content.replace("\n", " ")
parsed_row = {
"msg_id" : msg_id,
"sender" : sender,
"sender_id" : sender_id,
"reply_to_msg_id" : reply_to_msg_id,
"date" : date,
"date_unixtime" : date_unixtime,
"msg_type" : msg_type,
"msg_content" : msg_content,
"forwarded_from" : forwarded_from,
"action" : action,
"has_mention" : has_mention,
"has_email" : has_email,
"has_phone" : has_phone,
"has_hashtag" : has_hashtag,
"is_bot_command" : is_bot_command,
}
return parsed_row
def process_chat(self, jdata):
""" Parses the contents of a 'Chat' object.
Parameters
----------
jdata: a dictionary in the structure of a 'Chat' object.
"""
chat = jdata["name"]
rows = []
for message in jdata["messages"]:
row = self.process_message(message)
if row is not None:
rows.append(row)
dump = {
"chat" : chat,
"rows" : rows,
}
debug(f"{chat} parsed ok!")
return dump
def process(self, chat_history_json):
""" Parses a exported file.
Parameters
----------
chat_history_json: path to 'result.json' created by Telegram Desktop.
"""
with open(chat_history_json, "r", encoding="utf-8-sig") as input_file:
contents = input_file.read()
jdata = json.loads(contents)
if "chats" in jdata:
return [self.process_chat(chat) for chat in jdata["chats"]["list"]]
elif "left_chats" in jdata:
return [self.process_chat(chat) for chat in jdata["left_chats"]["list"]]
else:
return [self.process_chat(jdata)]
def to_csv(self, data, include_timestamp=False):
""" Saves the parsed data to CSV files, one for each chat exported.
The filename with match the chat name with optional timestamp.
Parameters
----------
data: list of parsed chats.
include_timestamp: if True, puts a timestamp in the filename.
"""
for dump in data:
output_filepath = f"{dump['chat']}.csv"
if include_timestamp:
output_filepath = f"{timestamp()}_{output_filepath}"
with open(output_filepath, "w", encoding="utf-8-sig", newline="") as output_file:
writer = csv.DictWriter(output_file, self.columns, dialect="unix", quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader()
for row in dump["rows"]:
writer.writerow(row)
if __name__ == "__main__":
""" Usage as executable script.
Example
-------
python3 telegram_chat_parser.py <result.json_filepath>
"""
parser = argparse.ArgumentParser(
prog="telegram_chat_parser.py",
description="Script to parse a Telegram chat history JSON file into CSV tabular format.",
usage="python3 telegram_chat_parser.py result.json -o my_chat_data.csv",
epilog="Post issues at https://github.com/keizerzilla/telegram-chat-parser/issues"
)
parser.add_argument("chat_history_json", help="'result.json' file path of exported chat history.")
args = vars(parser.parse_args())
chat_history_json = args["chat_history_json"]
parser = TelegramChatParser()
data = parser.process(chat_history_json)
parser.to_csv(data)