Skip to content

Instantly share code, notes, and snippets.

@puhitaku
Created November 5, 2016 17:52
Show Gist options
  • Save puhitaku/a7d68957ba416ba8b152b197967878ad to your computer and use it in GitHub Desktop.
Save puhitaku/a7d68957ba416ba8b152b197967878ad to your computer and use it in GitHub Desktop.
Slack Emoji Counter
#!/usr/bin/env python3
count_all = False # Include aliases in count or not
import fileinput, sys
from collections import OrderedDict
import requests as req
from bs4 import BeautifulSoup
def sort_ordered_dict(d):
return sorted(d.items(), key=lambda t: t[1], reverse=True)
def main():
if sys.argv[-1] in ['-h', '--help']:
print('Pipe the HTML code or specify a file in command argument.')
sys.exit()
bs = BeautifulSoup(''.join(list(fileinput.input())))
emojis = bs.find_all('tr', {'class': 'emoji_row'})
author_count = OrderedDict()
for e in emojis:
author = e.find('td', {'class': 'author_cell'})
name = author.text.strip()
emoji_type = author.findPreviousSibling()
if 'Image' in emoji_type.text or count_all:
author_count[name] = author_count.get(name, 0) + 1
author_count = sort_ordered_dict(author_count)
for k, v in author_count:
print('{author}: {count}'.format(author=k, count=v))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment