Skip to content

Instantly share code, notes, and snippets.

@yushinna
Created December 2, 2021 07:06
Show Gist options
  • Save yushinna/8b966b1bd32cfc36761ee81528ffc795 to your computer and use it in GitHub Desktop.
Save yushinna/8b966b1bd32cfc36761ee81528ffc795 to your computer and use it in GitHub Desktop.
import requests
import time
import sys
from heapq import nlargest
from datetime import date, timedelta
SLACK_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# download slack data
users = requests.get('https://slack.com/api/users.list',
params={'token': SLACK_TOKEN}).json()['members']
channels = requests.get(
'https://slack.com/api/conversations.list',
params={
'token': SLACK_TOKEN,
'exclude_archived': 'true',
'limit': 1000
}
).json()['channels']
today = date.today()
messages = {}
for channel in channels:
if channel['is_archived'] is False and \
channel['name'].startswith('times_') is True:
from_date = int(time.mktime((today - timedelta(7)).timetuple()))
to_date = int(time.mktime(today.timetuple()))
retry = 20
latest = to_date
oldest = from_date
has_more = True
message_list = []
while(has_more):
payload = {
"token": SLACK_TOKEN,
"channel": channel["id"],
"latest": latest,
"oldest": oldest
}
for i in range(retry):
try:
r = requests.get(
"https://slack.com/api/conversations.history",
params=payload
)
except Exception:
t, v, tb = sys.exc_info()
if r.status_code == 429:
sleep_time = int(r.headers["Retry-After"])
time.sleep(sleep_time)
continue
else:
break
else:
continue
d = r.json()
message_list.extend(d['messages'])
has_more = 'has_more' in d and d['has_more']
if has_more:
latest = int(float(d["messages"][-1]["ts"]))
messages[channel['id']] = message_list
data = {
'users': users,
'channels': channels,
'messages': messages
}
# calcurate number of reply and react
channel_ids = [d['id'] for d in data['channels']]
row = []
for channel_id in channel_ids:
if channel_id in data['messages']:
messages = data['messages'][channel_id]
for message in messages:
if message['type'] == 'message' and message.get(
'subtype') != 'bot_message':
# get user
user = message['user']
# get reply users
reply_users = message.get('reply_users')
if reply_users is not None:
reply_users_count = len(set(reply_users))
reply_users = ', '.join(reply_users)
else:
reply_users = 'None'
reply_users_count = 0
# get react users
reactions = message.get('reactions')
if reactions is not None:
react_users = []
for reaction in reactions:
L = reaction['users']
react_users.append(', '.join(str(x) for x in L))
react_users = ', '.join(react_users)
react_users_count = len(set(list(react_users.split(','))))
else:
react_users = 'None'
react_users_count = 0
ts = int(float(message['ts']) * 1000000)
url = 'https://stockmarkteam.slack.com/archives/{}/p{}'.format(
channel_id, ts)
d = {
'channel_id': channel_id,
'text': message['text'],
'user': user,
'reply_users': reply_users,
'reply_users_count': reply_users_count,
'react_users': react_users,
'react_users_count': react_users_count,
'url': url,
'score': reply_users_count + react_users_count
}
row.append(d)
top3 = nlargest(3, row, key=lambda x: x['score'])
output['1_url'] = top3[0]['url']
output['1_reply_users_count'] = top3[0]['reply_users_count']
output['1_react_users_count'] = top3[0]['react_users_count']
output['2_url'] = top3[1]['url']
output['2_reply_users_count'] = top3[1]['reply_users_count']
output['2_react_users_count'] = top3[1]['react_users_count']
output['3_url'] = top3[2]['url']
output['3_reply_users_count'] = top3[2]['reply_users_count']
output['3_react_users_count'] = top3[2]['react_users_count']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment