-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtelegram-export-converter.py
259 lines (211 loc) · 9.14 KB
/
telegram-export-converter.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
from html import unescape
from time import time
from sys import argv
import csv
import os
import re
class Message:
def __init__(self):
self.message_id = None
self.timestamp = None
self.sender = None
self.fwd = None
self.reply = None
self.content = None
def toTuple(self):
if self.message_id: self.message_id = self.message_id.replace('message', '')
if self.timestamp: self.timestamp = ' '.join(self.timestamp.split()[:2])
if self.sender: self.sender = unescape(self.sender.strip())
if self.fwd: self.fwd = unescape(self.fwd.strip())
if self.reply: self.reply = self.reply.replace('message', '')
if self.content: self.content = unescape(self.content.strip())
return (self.message_id, self.timestamp, self.sender, self.fwd, self.reply, self.content)
t0 = time()
message_id_new_pattern = re.compile('<div class="message default clearfix" id="([^"]+)')
message_id_joined_pattern = re.compile('<div class="message default clearfix joined" id="([^"]+)')
timestamp_pattern = re.compile('<div class="pull_right date details" title="([^"]+)')
fwd_pattern = re.compile('<div class="userpic userpic\d+" style="width: 42px; height: 42px">')
fwd_reply_pattern = re.compile('<div class="reply_to details">')
fwd_sender_pattern = re.compile('([^<]+)<span class="date details')
same_fwd_media_pattern = re.compile('<div class="media_wrap clearfix">')
same_fwd_text_pattern = re.compile('<div class="text">')
reply_pattern = re.compile('In reply to <a href="(?:messages\d*.html)?#go_to_([^"]+)"')
photo_pattern = re.compile('<div class="media clearfix pull_left media_photo">')
video_pattern = re.compile('<div class="media clearfix pull_left media_video">')
voice_pattern = re.compile('<div class="media clearfix pull_left media_voice_message">')
audio_pattern = re.compile('<div class="media clearfix pull_left media_audio_file">')
file_pattern = re.compile('<div class="media clearfix pull_left media_file">')
contact_pattern = re.compile('<div class="media clearfix pull_left media_contact">')
contact_link_pattern = re.compile('<a class="media clearfix pull_left block_link media_contact" href="[^"]+"')
location_link_pattern = re.compile('<a class="media clearfix pull_left block_link media_location" href="[^"]+"')
call_pattern = re.compile('<div class="media clearfix pull_left media_call( success)?">')
poll_pattern = re.compile('<div class="media_poll">')
game_pattern = re.compile('<a class="media clearfix pull_left block_link media_game" href="[^"]+">')
html_link_pattern = re.compile('</?a[^<]*>')
html_span_pattern = re.compile('</?span[^<]*>')
html_tags = ['em', 'strong', 'code', 'pre', 's']
################################################################################
print("Starting...")
# Scans current directory for message<n>.html Telegram chat export files
message_files = []
n = 1
for file in os.listdir():
if file.startswith('messages') and file.endswith('.html'):
message_files.append('messages' + (str(n) if n > 1 else '') + '.html')
n += 1
if not message_files:
print('No message.html files found. Are you sure the script is in the right directory? Exiting...')
exit()
print(f'Loading all {len(message_files)} message files...')
# Loads all files content into memory
lines = []
for file in message_files:
with open(file, encoding='UTF-8') as f:
lines += [line.replace('\n', '').strip() for line in f if line.strip()]
# Writes all concatenated message<n>.html files for debugging
# with open('rawHTML.txt', 'w+', encoding='UTF-8') as f:
# f.write('\n'.join(lines) + '\n')
# Sets output filename as the chat's name
chat_name = lines[15]
output_file = 'Telegram-' + ''.join(c if c.isalnum() else '_' for c in chat_name) + '.csv'
################################################################################
print(f'Processing \'{chat_name}\'...')
messages = []
cur = 0
last_sender = None
last_fwd_sender = None
while cur < len(lines):
# Skip lines that aren't the start of a message
if not lines[cur].startswith('<div class='):
cur += 1
continue
# Check if it's a new sender's message
new = True
message_id = re.findall(message_id_new_pattern, lines[cur])
if not message_id:
new = False
message_id = re.findall(message_id_joined_pattern, lines[cur])
# Skip lines that aren't the start of a message
if not message_id:
cur += 1
continue
m = Message()
m.message_id = message_id[0]
if new: # New sender
# If it's from a Deleted Account, no initial is
# shown as avatar, so there's a line less to skip
if lines[cur+4] == '</div>':
cur += 8
else:
cur += 9
timestamp = re.findall(timestamp_pattern, lines[cur])
if timestamp:
m.timestamp = timestamp[0]
else:
m.timestamp = None
cur += 4
m.sender = lines[cur]
last_sender = m.sender
cur += 3
m.content = lines[cur]
else: # Same sender as the message before
cur += 2
timestamp = re.findall(timestamp_pattern, lines[cur])
if timestamp:
m.timestamp = timestamp[0]
else:
m.timestamp = None
m.sender = last_sender
cur += 4
m.content = lines[cur]
is_fwd = re.match(fwd_pattern, m.content)
is_same_fwd_text = re.match(same_fwd_text_pattern, m.content)
is_same_fwd_media = re.match(same_fwd_media_pattern, m.content)
is_reply = re.findall(reply_pattern, m.content)
is_fwd_reply_same_fwd_text = re.findall(fwd_reply_pattern, m.content)
if is_fwd:
# If it's from a Deleted Account, no initial is
# shown as avatar, so there's a line less to skip
if lines[cur+2] == '</div>':
cur += 7
else:
cur += 8
fwd_sender = re.findall(fwd_sender_pattern, lines[cur])
m.fwd = fwd_sender[0]
last_fwd_sender = m.fwd
cur += 2
is_fwd_reply = re.findall(fwd_reply_pattern, lines[cur])
if is_fwd_reply:
cur += 4
else:
cur += 1
m.content = lines[cur]
elif is_fwd_reply_same_fwd_text:
m.fwd = last_fwd_sender
cur += 4
m.content = lines[cur]
elif is_same_fwd_text:
m.fwd = last_fwd_sender
cur += 1
m.content = lines[cur]
elif is_same_fwd_media:
m.fwd = last_fwd_sender
cur += 6
m.content = f'[{lines[cur]}]'
elif is_reply:
m.reply = is_reply[0]
cur += 3
m.content = lines[cur]
if m.content.startswith('<'):
is_photo = re.match(photo_pattern, m.content)
is_video = re.match(video_pattern, m.content)
is_voice = re.match(voice_pattern, m.content)
is_audio = re.match(audio_pattern, m.content)
is_file = re.match(file_pattern, m.content)
is_contact = re.match(contact_pattern, m.content)
is_contact_link = re.match(contact_link_pattern, m.content)
is_location_link = re.match(location_link_pattern, m.content)
is_call = re.match(call_pattern, m.content)
is_poll = re.match(poll_pattern, m.content)
is_game = re.match(game_pattern, m.content)
# Write type of media as content
if any([is_photo, is_video, is_voice, is_audio, is_file]):
cur += 5
m.content = f'[{lines[cur]}]'
elif is_contact or is_contact_link:
cur += 5
m.content = f'[Contact - {lines[cur]} - {lines[cur+3]}]'
elif is_location_link:
cur += 5
m.content = f'[{lines[cur]} - {lines[cur+3]}]'
elif is_call:
cur += 8
m.content = f'[Call - {lines[cur]}]'
elif is_poll:
m.content = f'[{lines[cur+5]} - {lines[cur+2]}]'
elif is_game:
m.content = f'[Game - {lines[cur+5]} - {lines[cur+11]}]'
# Replace HTML line breaks
if '<br>' in m.content:
m.content = m.content.replace('<br>', '\\n')
# Remove HTML formatting tags
if '<' in m.content and any(f'<{tag}>' in m.content for tag in html_tags):
for tag in html_tags:
m.content = m.content.replace(f'<{tag}>', '')
m.content = m.content.replace(f'</{tag}>', '')
# Remove HTML tags with args
if '<a' in m.content:
m.content = re.sub(html_link_pattern, '', m.content)
if '<span' in m.content:
m.content = re.sub(html_span_pattern, '', m.content)
# Handle animated emojis, as they're not logged properly by Telegram (might change soon?)
if m.content == '</div>':
m.content = '[Animated emoji]'
messages.append(m)
cur += 1
# Write CSV
with open(output_file, 'w+', encoding='UTF-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(list(messages[0].__dict__.keys()))
writer.writerows([m.toTuple() for m in messages])
print(f'Written to \'{output_file}\' in {(time()-t0):.2f}s.')