Skip to content

Instantly share code, notes, and snippets.

@pedropapa
Last active January 1, 2022 18:59
Show Gist options
  • Save pedropapa/2eb9389089bbac2ef688533bfa6ae4b2 to your computer and use it in GitHub Desktop.
Save pedropapa/2eb9389089bbac2ef688533bfa6ae4b2 to your computer and use it in GitHub Desktop.
Rank whatsapp group contacts sorting by messages count
<html>
<body>
<script type="text/javascript">
/**
* To get whatsapp group messages file, first go to the group in whatsapp, then select "export chat" options.
* Retrieve the file from your phone and put it in the same directory as this .html file.
*/
const fileName = prompt('Enter whatsapp group messages backup file, without extension (txt).\n\nPS: File must be in same directory as this file.', '_chat');
const data = fetch(fileName + '.txt', { mode: 'no-cors' })
.then((response) => response.text())
.then((data) => {
const keysrt = (key) => (a, b) => (a[key] < b[key]) ? 1 : ((a[key] > b[key]) ? -1 : 0);
const contactsRegex = /\[.{18}\s(.*?):/g;
const contacts = [...new Set([...data.matchAll(contactsRegex)].map((c) => c[1]))];
const escaper = (c) => c.split('').map(c => c.match(/\W/) ? `\\${c}` : c).join('');
let contactsMessages = contacts.map((contact) => ({
contact,
times: [...data.matchAll(RegExp('\\[.{18}\\s' + escaper(contact) + ':', 'g'))].length
}));
contactsMessages = contactsMessages.sort(keysrt('times'));
document.write(contactsMessages.map((contactInfo) => `${contactInfo.contact}: <b>${contactInfo.times}</b>`).join('<br />'));
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment