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
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "### 1. Get your Slack token\n\nAccess https://api.slack.com/custom-integrations/legacy-tokens#legacy_token_generator, get the key for desired workspace and paste it below. "
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-25T15:24:43.089453Z",
"start_time": "2018-07-25T15:24:43.085398Z"
},
"trusted": true
},
"cell_type": "code",
"source": "TOKEN = \"your-token-here\"\n\noutput_folder = './data'",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### 2. Star some threads\n\n### 3. Run this script"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-25T15:00:30.055455Z",
"start_time": "2018-07-25T15:00:29.824649Z"
},
"trusted": true
},
"cell_type": "code",
"source": "from datetime import datetime\nfrom pathlib import Path\nfrom re import findall\nfrom slackclient import SlackClient",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-25T15:00:30.062534Z",
"start_time": "2018-07-25T15:00:30.057412Z"
},
"trusted": true
},
"cell_type": "code",
"source": "sc = SlackClient(TOKEN)\n\ndata_path = Path(output_folder)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-25T15:13:23.485155Z",
"start_time": "2018-07-25T15:13:23.477133Z"
},
"trusted": true
},
"cell_type": "code",
"source": "users = {}\n\ndef add_user(user_id):\n user = sc.api_call(\"users.profile.get\", user=msg[\"user\"])[\"profile\"]\n users[msg[\"user\"]] = {\n \"real_name\": user['real_name'],\n \"display_name\": f\"(@{user['display_name']})\" if user['display_name'] else \"\"\n }\n\ndef clean_msg(text):\n user_handles_in_msg = findall(r\"<@[^>]+>\", text)\n\n for mention in user_handles_in_msg:\n user_id = mention.replace(\"<@\", \"\").replace(\">\", \"\")\n \n add_user(user_id)\n handle = user['display_name'] or user['real_name']\n \n text = text.replace(mention, handle)\n \n return text\n\ndef print_hr(file, num=1):\n print(file=file)\n \n for _ in range(num):\n print(\"=\" * 80, file=file)\n \n print(file=file)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-07-25T15:22:33.508453Z",
"start_time": "2018-07-25T15:20:21.180989Z"
},
"trusted": true
},
"cell_type": "code",
"source": "data_path.mkdir(parents=True, exist_ok=True)\n\nstarreds = sc.api_call(\"stars.list\")[\"items\"]\n \nfor i_star, starred in enumerate(starreds):\n mess_id = \"_\".join(starred['message'][\"permalink\"].split(\"/\")[-2:])\n print(f\"[{i_star+1} / {len(starreds)}] {mess_id}\")\n\n msg = starred[\"message\"]\n thread_ts = msg.get(\"thread_ts\")\n\n with open(data_path / Path(f\"{mess_id}.txt\"), \"w+\") as f:\n text = clean_msg(msg[\"text\"])\n \n print(text, file=f)\n print_hr(f, 2)\n\n if thread_ts:\n msgs = sc.api_call(\"conversations.replies\", channel=starred[\"channel\"], ts=thread_ts)[\"messages\"]\n\n for i, msg in enumerate(msgs):\n add_user(msg[\"user\"])\n\n the_date = datetime.fromtimestamp(starred[\"date_create\"]).isoformat()\n\n print(f\"{i+1}/{len(msgs)}\", file=f)\n print(\n f\"{the_date} :: [{msg['user']}] {users[msg['user']]['real_name']} {users[msg['user']]['display_name']}\",\n file=f\n )\n\n text = clean_msg(msg[\"text\"])\n\n print(text, file=f)\n print_hr(f)",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.0",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "d16a26e8a0de7a3038a2784b4b69502c",
"data": {
"description": "Get starred threads dialogs from Slack",
"public": false
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/d16a26e8a0de7a3038a2784b4b69502c"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
# 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