Last active
August 29, 2015 14:25
-
-
Save shinespark/e40dc1b4302140bd48fc to your computer and use it in GitHub Desktop.
Slackで、特定単語(人名など)の発言回数順に取得するコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
from collections import defaultdict | |
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 = defaultdict(int) | |
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