Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yuzuemon/5e7175bb72e535f488a6 to your computer and use it in GitHub Desktop.
Save yuzuemon/5e7175bb72e535f488a6 to your computer and use it in GitHub Desktop.
Slackで、特定単語(人名など)の発言回数順に取得するコード
# -*- coding: UTF-8 -*-
from collections
from urllib import request, parse
import json
import sys
argvs = sys.argv
argc = len(argvs)
if argc < 3:
print('Usage: python test.py token search_word')
sys.exit()
token = argvs[1]
search_word = parse.quote(argvs[2])
url = 'https://slack.com/api/search.messages?token=' + token + '&query=' + search_word + '&count=1000'
res = request.urlopen(url).read()
matches = json.loads(res.decode('utf-8'))['messages']['matches']
count_dict = collections.Counter()
for post in matches:
if post['type'] == 'message' and post['user'] != '':
count_dict[post['username']] += 1
for l in sorted(count_dict.items(), key=lambda x: x[1], reverse=True):
print(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment