-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_specific.py
More file actions
135 lines (106 loc) · 4.58 KB
/
export_specific.py
File metadata and controls
135 lines (106 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
export_specific.py
==================
Exports only the specific channels, DMs, and group DMs you configure below.
Usage:
python export_specific.py
"""
import os
import json
from datetime import datetime
from slack_client import (
auth_check, fetch_users, get_all_pages,
export_conversation, write_gpt_file, resolve
)
# ─────────────────────────────────────────────────────────────────────────────
# CONFIGURE WHAT YOU WANT TO EXPORT
# ─────────────────────────────────────────────────────────────────────────────
# Channels (use exact Slack channel name, no # prefix)
CHANNELS = [
"android-automation-team",
"debug-openclaw",
"general",
]
# DMs — use the person's Slack display name (as shown in Slack sidebar)
DM_USERS = [
"Fawad Raza",
"Jop",
"elicett",
]
# Group DMs — list of member display names (all members must match)
# Leave empty [] to skip group DMs
GROUP_DMS = [
["elicett", "Fawad Raza", "Jop", "Ysha Lia"], # the group from sidebar
]
# ─────────────────────────────────────────────────────────────────────────────
# OTHER SETTINGS
# ─────────────────────────────────────────────────────────────────────────────
OUTPUT_DIR = "slack_export/specific"
FETCH_FILES = True
FETCH_THREADS = True
# ─────────────────────────────────────────────────────────────────────────────
def members_match(conv_members, wanted_names, user_map):
"""Check if a group DM's members match the wanted name list (order-insensitive)."""
resolved = set(resolve(uid, user_map) for uid in conv_members)
return resolved == set(wanted_names)
def should_include(conv, user_map):
"""Return True if this conversation matches the user's config."""
is_dm = conv.get("is_im", False)
is_group = conv.get("is_mpim", False)
is_channel = not is_dm and not is_group
if is_channel:
name = conv.get("name", "")
return name in CHANNELS
if is_dm:
other = resolve(conv.get("user", ""), user_map)
return other in DM_USERS
if is_group:
if not GROUP_DMS:
return False
members = conv.get("members", [])
return any(members_match(members, wanted, user_map) for wanted in GROUP_DMS)
return False
def main():
os.makedirs(OUTPUT_DIR, exist_ok=True)
print("Connecting to Slack...")
me, workspace = auth_check()
print(f" Logged in as : {me}")
print(f" Workspace : {workspace}\n")
user_map = fetch_users()
print(f" {len(user_map)} users loaded\n")
print("Fetching conversation list...")
all_convs = get_all_pages(
"conversations.list", "channels",
{
"types": "public_channel,private_channel,im,mpim",
"exclude_archived": False,
"limit": 200,
}
)
# Filter to only what was configured above
selected = [c for c in all_convs if should_include(c, user_map)]
print(f" Matched {len(selected)} conversation(s) from your config\n")
if not selected:
print(" Nothing matched. Check your CHANNELS / DM_USERS / GROUP_DMS names above.")
return
with open(os.path.join(OUTPUT_DIR, "_users.json"), "w") as f:
json.dump(user_map, f, indent=2, ensure_ascii=False)
gpt_lines = [
"SLACK SPECIFIC EXPORT",
f"Workspace : {workspace}",
f"User : {me}",
f"Date : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
f"Exported : {len(selected)} conversations",
]
print("Exporting selected conversations...")
for conv in selected:
lines = export_conversation(
conv, user_map, OUTPUT_DIR,
fetch_files=FETCH_FILES,
fetch_threads=FETCH_THREADS,
)
gpt_lines.extend(lines)
write_gpt_file(OUTPUT_DIR, gpt_lines)
print(f"\nDone! Output saved to: {os.path.abspath(OUTPUT_DIR)}/")
if __name__ == "__main__":
main()