Skip to content

Instantly share code, notes, and snippets.

@ScriptAutomate
Last active May 8, 2024 20:11
Show Gist options
  • Save ScriptAutomate/3b85fc74f2e161d55922ccdd388a4aae to your computer and use it in GitHub Desktop.
Save ScriptAutomate/3b85fc74f2e161d55922ccdd388a4aae to your computer and use it in GitHub Desktop.
Slack Export Cleanup / Prep Scripts

Slack Export Cleanup / Prep Scripts

These three files were used to help clean up a Slack Workspace channel export prior to importing into a Discord server.

Ultimately, the Salt Project Slack Archive Export was imported into the new Salt Project Discord Server:

Prereqs

Scripts

  • migration-channels.txt: Just a file listing the channels you wish to ultimately export. This is used by clean-channel-listing.py as a source list to clean up the channels.json file that Slack exports
  • clean-channel-listing.py: As said above, cleans up channels.json. NOTE: You should also delete any of the directories in the Slack export that you don't plan to import, as an import tool may use different methods of import that don't reference channels.json, but also because clean-channel-updates.py may then require more processing time.
  • clean-channel-updates.py: A simple script that deletes channel_join and channel_leave messages in channels, so that they aren't included as part of your Slack import.

Actual Import

Post-Import

  • We saved all of our channels underneath a Slack Archive Discord server category
  • We appended all of the imported channels with -slack-archive to help prevent any confusion in case someone linked to an actual active channel in Discord with the same name (as you can have multiple channels with the same name...)
  • We turned all of the Slack Archive channels to read-only, so that they are ultimately searchable and linkable, but users can't respond to messages in the archive (as those messages aren't actually tied to Discord user accounts directly)
'''
Reads in a `migration-channels.txt` file, being the channels you wish
to import, and cleans up all the channels listed within `channels.json`
that the Slack export includes.
NOTE: Depending on what tool you use to process your exported channels,
it may also be a good idea to DELETE the directories of the unwanted
Slack channel exports, in addition to running this script to clean up
the `channels.json`. Doing so will also ensure that the follow up
`clean-channel-updates.py` script doesn't need to also process every
channel you aren't planning on importing.
'''
import json
# Function to read names from a file and return them as a set
def read_names(file_path):
with open(file_path, 'r') as file:
return set(name.strip() for name in file.readlines())
# Function to clean the JSON file based on the names read from the text file
def clean_json_file(json_file_path, names_set):
with open(json_file_path, 'r') as file:
data = json.load(file)
# Filter out the unwanted entries
data = [entry for entry in data if entry.get('name') in names_set]
with open(json_file_path, 'w') as file:
json.dump(data, file, indent=4)
# Read the list of names from the text file
names = read_names("migration-channels.txt")
# Clean the JSON file
clean_json_file("channels.json", names)
'''
Clean up all of the channels so that join and leave messages
aren't going to be included in a Slack message archive export!
'''
import os
import json
def clean_json_file(file_path):
with open(file_path, 'r') as file:
data = json.load(file)
# Filter out the unwanted entries
data = [entry for entry in data if not entry.get('subtype') in ['channel_join', 'channel_leave']]
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
def walk_and_clean_json(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
clean_json_file(file_path)
# Replace '.' with the path to the directory you want to process,
# OR run this script as-is at the root dir of your Slack export extracted archive
walk_and_clean_json('.')
announcements
cloud
develop
documentation
enterprise
formulas
freebsd
general
heist-salt
idem-project
irc
macos
module-docs
networks
pop
releases
salt-docs-working-group
salt-extensions
security
ssh
testing
wg_formulas
windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment