My old friend and I have been trying to save our chat histories recently for the nostalgia and the memories. Google chat history saves in a latest-oldest order. I’d like to make it to oldest-latest as well as change the pattern of the text. Any idea how I can implement this in Python?
For reference this is the Hangouts file.
JavaScript
x
33
33
1
From 1597166228247121622@xxx Sat Dec 30 18:33:39 +0000 2017
2
X-GM-THRID: 1597166193327506679
3
X-Gmail-Labels: chat
4
From: Nash MIME-Version: 1.0
5
Content-Type: text/plain
6
7
I understand, don't worry
8
9
From 1597166202534663022@xxx Sat Dec 30 18:33:06 +0000 2017
10
X-GM-THRID: 1597166193327506679
11
X-Gmail-Labels: Chat
12
From: Nash MIME-Version: 1.0
13
Content-Type: text/plain
14
15
Have a safe trip
16
17
From 1588224320874515054@xxx Sat Dec 30 15:45:43 +0000 2017
18
X-GM-THRID: 1588205400363537982
19
X-Gmail-Labels: Chat
20
From: Sash MIME-Version: 1.0
21
Content-Type: text/plain
22
23
I FEEL YA
24
25
From 1588224307132362082@xxx Sat Dec 30 15:45:30 +0000 2017
26
X-GM-THRID: 1588205400363537982
27
X-Gmail-Labels: Chat
28
From: Sash MIME-Version: 1.0
29
Content-Type: text/plain
30
31
HOW IN THE WORLD ARE ALL OF YOU SHARING THE SAME HOTEL ROOM ??
32
33
And this is what I want it to look like.
JavaScript
1
5
1
[25/04/2018, 3:11:11 PM] Sash: pigeons !
2
[25/04/2018, 3:11:24 PM] Nash: pls no
3
[25/04/2018, 3:11:55 PM] Nash: dont need em gutur guturs
4
[25/04/2018, 3:13:13 PM] Sash: turn it up beetches
5
Advertisement
Answer
You can use modules re
and datetime
.
Example for your text:
JavaScript
1
14
14
1
import re
2
import datetime
3
4
text = text.split('nn')
5
6
datetime_pattern = 'w{3} w{3} d{2} d{2}:d{2}:d{2} .d{4} d{4}'
7
name_pattern = 'From:sw*s+MIME-Version:'
8
9
for i in range(0, len(text), 2):
10
date = datetime.datetime.strptime(re.search(datetime_pattern, text[i]).group(), '%a %b %d %H:%M:%S %z %Y')
11
name = re.search(name_pattern, text[i]).group()[6:-14].strip()
12
message = text[i + 1].strip()
13
print('[{0}] {1}: {2}'.format(date.strftime('%d/%m/%Y, %I:%M:%S %p'), name, message))
14
The result:
JavaScript
1
5
1
[30/12/2017, 06:33:39 PM] Nash: I understand, don't worry
2
[30/12/2017, 06:33:06 PM] Nash: Have a safe trip
3
[30/12/2017, 03:45:43 PM] Sash: I FEEL YA
4
[30/12/2017, 03:45:30 PM] Sash: HOW IN THE WORLD ARE ALL OF YOU SHARING THE SAME HOTEL ROOM ??
5