Skip to content

Instantly share code, notes, and snippets.

@rdar-lab
Forked from greird/slack-files-downloader.sh
Last active August 10, 2024 15:21
Show Gist options
  • Save rdar-lab/ace9b579781e498b21a5011d42a65adc to your computer and use it in GitHub Desktop.
Save rdar-lab/ace9b579781e498b21a5011d42a65adc to your computer and use it in GitHub Desktop.
Download all files from a Slack workspace export folder.
#!/bin/bash
#
# This script will browse a Slack export folder and download all files in a new /files folder
#
# HOW TO:
# 1. As a Workspace admin, download an export of your Slack history (https://www.slack.com/services/export)
# 2. Make sure you have jq installed (https://stedolan.github.io/jq/)
# 3. Make sure you have curl installed
# 4. Place this file at the root of your Slack export folder
# 5. Run `bash slack-files-downloader.sh > download.log` in your terminal
#
# OPTIONS
# -o Overwrite files if they already exist in destination folder, otherwise skip them.
# -s Do not show message when a file is skipped
while getopts "os" flag
do
case $flag in
o) overwrite=true;;
s) silent=true;;
esac
done
echo Override=$overwrite
echo Silent=$silent
# printf "\nSelect a channel to look into or leave empty for all channels:\n"
# read userchannel
# This is only public channels
#channels=$(cat channels.json | jq -rc '.[].name')
# This is to use all subdirectories instead
channels=$(find . -maxdepth 1 -type d ! -name "files" -printf "%P\n")
echo Scanning: $channels
for channel in $channels
do
if [[ $channel == $userchannel ]] || [[ -z $userchannel ]]
then
printf "\n============================================\nLooking into $channel...\n============================================\n"
for message in "$channel"/*.json
do
files=$(cat $message | jq -c '.[].files | del(..|nulls)' | sed 's/ //g')
for file_entry in $files
do
if [[ $file_entry != "null" ]]
then
for file in $(echo $file_entry | jq -c '.[] | [.id, .name, .url_private_download] | del(..|nulls)' | sed 's/ //g')
do
if [[ $file != [] ]]
then
file_id=$(echo $file | jq -r '.[0]')
file_title=$(echo $file | jq -r '.[1]')
url=$(echo $file | jq -rc '.[2]')
if [[ $url != "null" ]]
then
filename_raw="${file_id}_${file_title}"
filename=$(echo $filename_raw | sed -e 'y/āáǎàçēéěèīíǐìōóǒòūúǔùǖǘǚǜüĀÁǍÀĒÉĚÈĪÍǏÌŌÓǑÒŪÚǓÙǕǗǙǛÜ/aaaaceeeeiiiioooouuuuuuuuuAAAAEEEEIIIIOOOOUUUUUUUUU/')
filename="${filename##*/}"
if [ -f "files/$channel/$filename" ] && [[ $overwrite != true ]]
then
if [[ $silent != true ]]
then
printf "$filename already exists in destination folder. ignoring it!\n"
fi
continue
fi
printf "Downloading $filename... from URL $url\n"
mkdir -p files/$channel
curl --progress-bar $url -o "files/$channel/$filename"
fi
fi
done
fi
done
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment