Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Last active January 3, 2022 19:39
Show Gist options
  • Save pedropapa/9727a5f2752e9171e5579b257f2beba1 to your computer and use it in GitHub Desktop.
Save pedropapa/9727a5f2752e9171e5579b257f2beba1 to your computer and use it in GitHub Desktop.
Rank whatsapp group contacts sorting by messages count
/**
* Requirements:
* - NodeJS 12+
*
* Instructions:
* - To get whatsapp group messages file, first go to the group in whatsapp, then select "export chat" option.
* Retrieve the file from your phone and put it somewhere in your computer.
*/
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const keysrt = (key) => (a, b) => (a[key] < b[key]) ? 1 : ((a[key] > b[key]) ? -1 : 0);
const escaper = (c) => c.split('').map(c => c.match(/\W/) ? `\\${c}` : c).join('');
(() => {
rl.question('Whatsapp group messages backup file name, without extension: ', (filePath) => {
const data = fs.readFile((filePath || '_chat.txt').trim(), 'utf8', (err, data) => {
if (err || !data) {
console.error('File not found');
process.exit();
}
const contactsRegex = /\[.{18,20}\s(.*?):/g;
const contacts = [...new Set([...data.matchAll(contactsRegex)].map((c) => c[1]))];
const contactsMessages = contacts.map((contact) => ({
contact,
times: [...data.matchAll(RegExp('\\[.{18,20}\\s' + escaper(contact) + ':', 'g'))].length
})).sort(keysrt('times'));
console.log(contactsMessages.map((contactInfo) => `${contactInfo.contact}: ${contactInfo.times}`).join('\n'));
process.exit();
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment