Skip to content

Instantly share code, notes, and snippets.

@stpe
Created March 10, 2016 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stpe/d5b262a11c99bf4ec280 to your computer and use it in GitHub Desktop.
Save stpe/d5b262a11c99bf4ec280 to your computer and use it in GitHub Desktop.
Parse Slack export to show top list of most popular emojis
"use strict";
var fs = require("fs");
var path = require("path");
var dir = "./export";
let count = fs.readdirSync(dir)
// we only want channel sub-directories
.filter(file => fs.statSync(path.join(dir, file)).isDirectory())
.reduce((a, subdir) => a.concat(
// read sub-directory
fs.readdirSync(path.join(dir, subdir))
// parse each logfile
.map(file => JSON.parse(fs.readFileSync(path.join(dir, subdir, file))))
), [])
// flatten array of array of messages
.reduce((a, b) => a.concat(b), [])
// keep only messages with text property and not bots!
.filter(msg => msg.user && msg.type == "message" && !msg.subtype)
// count occurences for each emoji
.reduce((count, msg) => {
(msg.text.match(/:([a-z0-9_]+):/g) || [])
.forEach(emoji => count[emoji] ? count[emoji]++ : count[emoji] = 1);
return count;
}, {});
// print top list
Object
.keys(count)
.sort((a, b) => count[b] - count[a])
.forEach(emoji => console.log(`${emoji} ${count[emoji]}`));
@isabellamnav
Copy link

Hi Stefan! 

I hope it's ok reaching out like this - I am very new to Github and anything related to coding. 
I found this code that you put together roughly 5 years ago, and I had some questions about its use cases. I am hoping it can help solve a problem I have with counting the number of specific emojis used in one of our Community Slack Channels. 

Can it be used to count emojis via a downloaded csv file from a specific slack channel? 

@stpe
Copy link
Author

stpe commented Apr 13, 2021

Hi @isabellamnav! Welcome to Github!

This code uses the directory as specified with dir to look for sub-directories of each channel, and then parse the channel log file (as it was structured 5 years ago, at least), which is in JSON format.

It cannot be used without modification to use a csv file. Parts of the code may probably be re-used if the csv file is parsed into a similar structure as the JSON file.

@isabellamnav
Copy link

Awesome! Thank you for the welcome and for responding :)

Turns out our archives are in JSON format so this will work for us!

Thanks again for sharing your knowledge!

@boatiemcboatface
Copy link

Wow, This worked really well, Thank you for sharing this!

Question though, as I too am new to github and coding in general. Do you know how I can make it so that it doesn't grab the emojis in user's slack statuses? We use an app that automatically sets everyone's status to an emoji based on what their calendar status currently is and I think the report that I received was mostly just those emojis from statuses given that they were the calendar emoji and the bulb emoji which almost noone would use except for that app. Any ideas? Thanks again for creating this though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment