Last active
February 17, 2025 09:22
-
-
Save RageshAntony/eb4e6e3a82ae07d417831ba3d422c753 to your computer and use it in GitHub Desktop.
Whatsapp Chat Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const fs = require('fs'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
const filename = "chat_export.txt"; | |
let datetime_str = ""; | |
rl.question("Enter your name: ", function(sender_name) { | |
let messages = []; | |
let buffer = null; | |
fs.readFile(filename, "utf8", function(err, data) { | |
if (err) throw err; | |
const lines = data.split("\n"); | |
for (let i = 0; i < lines.length; i++) { | |
let line = lines[i]; | |
if (line.includes(" - ")) { | |
datetime_str = line.split(" - ")[0]; | |
let message = line.split(" - ")[1]; | |
if (buffer !== null) { | |
let message = buffer.join("\n").trim(); | |
let [date_str, time_str] = datetime_str.split(", "); | |
messages.push({ | |
"date": date_str, | |
"time": time_str, | |
"sender_name": sender_name, | |
"message": message | |
}); | |
buffer = null; | |
} | |
let name_message_parts = message.split(": "); | |
if (name_message_parts.length !== 2) { | |
continue; | |
} | |
sender_name = name_message_parts[0]; | |
message = name_message_parts[1]; | |
buffer = [message.trim()]; | |
} else if (buffer !== null) { | |
buffer.push(line.trim()); | |
} | |
} | |
if (buffer !== null) { | |
let message = buffer.join("\n").trim(); | |
let [date_str, time_str] = datetime_str.split(", "); | |
messages.push({ | |
"date": date_str, | |
"time": time_str, | |
"sender_name": sender_name, | |
"message": message | |
}); | |
} | |
let jsonStr = JSON.stringify(messages, null, 4); | |
fs.writeFile("chat_export.json", jsonStr, function(err) { | |
if (err) throw err; | |
console.log("Done!"); | |
rl.close(); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
if __name__ == '__main__': | |
filename = "chat_export.txt" | |
sender_name = input("Enter your name: ") | |
messages = [] | |
buffer = None | |
with open(filename, "r", encoding="utf-8") as f: | |
for line in f: | |
if " - " in line: | |
if buffer is not None: | |
message = "\n".join(buffer).strip() | |
date_str, time_str = datetime_str.split(", ") | |
messages.append({ | |
"date": date_str, | |
"time": time_str, | |
"sender_name": sender_name, | |
"message": message | |
}) | |
buffer = None | |
datetime_str, message = line.split(" - ") | |
name_message_parts = message.split(": ") | |
if len(name_message_parts) != 2: | |
continue | |
sender_name, message = name_message_parts | |
buffer = [message.strip()] | |
elif buffer is not None: | |
buffer.append(line.strip()) | |
if buffer is not None: | |
message = "\n".join(buffer).strip() | |
date_str, time_str = datetime_str.split(", ") | |
messages.append({ | |
"date": date_str, | |
"time": time_str, | |
"sender_name": sender_name, | |
"message": message | |
}) | |
with open("chat_export.json", "w") as f: | |
json.dump(messages, f) | |
print("Done!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Chat Export Viewer</title> | |
<style> | |
.container { | |
display: flex; | |
flex-direction: column; | |
align-items: flex-start; | |
padding: 10px; | |
background-color: #f2f2f2; | |
height: 100vh; | |
box-sizing: border-box; | |
font-family: sans-serif; | |
} | |
.chat-bubble { | |
max-width: 50%; | |
padding: 10px; | |
margin: 5px; | |
border-radius: 10px; | |
box-sizing: border-box; | |
} | |
.chat-bubble.sent { | |
align-self: flex-end; | |
background-color: #dcf8c6; | |
} | |
.chat-bubble.received { | |
align-self: flex-start; | |
background-color: #fff; | |
} | |
.chat-bubble .metadata { | |
display: flex; | |
flex-direction: row; | |
justify-content: space-between; | |
color: #777; | |
font-size: small; | |
margin-bottom: 5px; | |
} | |
.chat-bubble .message { | |
font-size: 16px; | |
margin-top: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"></div> | |
<script> | |
fetch('chat_export.json') | |
.then(response => response.json()) | |
.then(data => { | |
const container = document.querySelector('.container'); | |
data.forEach(message => { | |
const bubble = document.createElement('div'); | |
bubble.className = `chat-bubble ${message.sender_name.includes( 'Sivakumar') ? 'sent' : 'received'}`; | |
bubble.innerHTML = ` | |
<div class="metadata"> | |
<span>${message.sender_name}</span> | |
<span>${message.date} ${message.time}</span> | |
</div> | |
<p class="message">${message.message}</p> | |
`; | |
container.appendChild(bubble); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
07/08/22, 8:18 pm - Ragesh Antony: 3 days holiday | |
done the same thing here also. Just binge watched | |
Alice in the Borderland | |
Queens Gambit | |
☺️😊 | |
07/08/22, 9:01 pm - Sivakumar: 😂same here . Watching lock & key | |
07/08/22, 9:13 pm - Ragesh Antony: awesome series | |
08/08/22, 2:12 pm - Sivakumar: Yes 1st episode ae romba pidichuruchu | |
08/08/22, 2:13 pm - Ragesh Antony: Yeah season 3 is coming this month | |
08/08/22, 2:13 pm - Sivakumar: Ohh ok | |
08/08/22, 2:13 pm - Sivakumar: Apo athukula 2 seasons complete pananum polayae😂 | |
08/08/22, 2:14 pm - Ragesh Antony: Whatched 3 series in 1 week | |
Also | |
Never Have I ever | |
08/08/22, 2:14 pm - Ragesh Antony: Watch this | |
Tamil Family life in US story |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment