Skip to content

Instantly share code, notes, and snippets.

@paulochf
Last active July 30, 2018 16:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulochf/3682e0ad2d80ba22243722fd2b6bcbbd to your computer and use it in GitHub Desktop.
Save paulochf/3682e0ad2d80ba22243722fd2b6bcbbd to your computer and use it in GitHub Desktop.
Get starred threads dialogs from Slack
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# coding: utf-8
# ### 1. Get your Slack token
#
# Access https://api.slack.com/custom-integrations/legacy-tokens#legacy_token_generator, get the key for desired workspace and paste it below.
# In[ ]:
TOKEN = "your-token-here"
output_folder = './data'
# ### 2. Star some threads
#
# ### 3. Run this script
# In[ ]:
from datetime import datetime
from pathlib import Path
from re import findall
from slackclient import SlackClient
# In[ ]:
sc = SlackClient(TOKEN)
data_path = Path(output_folder)
# In[ ]:
users = {}
def add_user(user_id):
user = sc.api_call("users.profile.get", user=msg["user"])["profile"]
users[msg["user"]] = {
"real_name": user['real_name'],
"display_name": f"(@{user['display_name']})" if user['display_name'] else ""
}
def clean_msg(text):
user_handles_in_msg = findall(r"<@[^>]+>", text)
for mention in user_handles_in_msg:
user_id = mention.replace("<@", "").replace(">", "")
add_user(user_id)
handle = user['display_name'] or user['real_name']
text = text.replace(mention, handle)
return text
def print_hr(file, num=1):
print(file=file)
for _ in range(num):
print("=" * 80, file=file)
print(file=file)
# In[ ]:
data_path.mkdir(parents=True, exist_ok=True)
starreds = sc.api_call("stars.list")["items"]
for i_star, starred in enumerate(starreds):
mess_id = "_".join(starred['message']["permalink"].split("/")[-2:])
print(f"[{i_star+1} / {len(starreds)}] {mess_id}")
msg = starred["message"]
thread_ts = msg.get("thread_ts")
with open(data_path / Path(f"{mess_id}.txt"), "w+") as f:
text = clean_msg(msg["text"])
print(text, file=f)
print_hr(f, 2)
if thread_ts:
msgs = sc.api_call("conversations.replies", channel=starred["channel"], ts=thread_ts)["messages"]
for i, msg in enumerate(msgs):
add_user(msg["user"])
the_date = datetime.fromtimestamp(starred["date_create"]).isoformat()
print(f"{i+1}/{len(msgs)}", file=f)
print(
f"{the_date} :: [{msg['user']}] {users[msg['user']]['real_name']} {users[msg['user']]['display_name']}",
file=f
)
text = clean_msg(msg["text"])
print(text, file=f)
print_hr(f)
@paulochf
Copy link
Author

Used Python 3.7 . For other versions, use with caution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment