Skip to content

Instantly share code, notes, and snippets.

@ysakasin
Created December 7, 2018 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysakasin/58b6189e46764f4efeb6c4823d04bc0a to your computer and use it in GitHub Desktop.
Save ysakasin/58b6189e46764f4efeb6c4823d04bc0a to your computer and use it in GitHub Desktop.
# This script is released under the public domain.
# What's this?
# This script counts how many times emoji are used on your slack.
# It requires exported slack data.
# How to use it:
# $ ruby emoji_statistics.rb /path/to/exported/dir
require 'json'
path = ARGV[0]
if !Dir.exist?(path)
puts path
STDERR.puts "Error: invalid argument"
exit(1)
end
class Statistics
def initialize()
@stat = {}
end
def add(emoji, num = 1)
@stat[emoji] ||= 0
@stat[emoji] += num
end
def sort()
@stat.sort {|(_, x), (_, y)| x <=> y}
end
end
@msg = Statistics.new
@reaction = Statistics.new
@total = Statistics.new
EMOJI_REGEX = /:[^:\s\.\/A-Z]+:/
Dir.glob(File.join(path, "**/*.json")).each do |file|
str = File.read(file)
obj = JSON.parse(str)
obj.each do |msg|
msg["text"]&.scan(EMOJI_REGEX)&.each do |emoji|
@msg.add(emoji)
@total.add(emoji)
end
msg["reactions"]&.each do |r|
emoji = ":#{r["name"]}:"
@reaction.add(emoji, r["count"])
@total.add(emoji, r["count"])
end
end
end
puts "in msg"
puts @msg.sort.reverse[0..19]
puts "--------------------"
puts "in reaction "
puts @reaction.sort.reverse[0..19]
puts "--------------------"
puts "in total "
puts @total.sort.reverse[0..19]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment