-
-
Save adamwulf/fa7c18f4f67db87f4469a277cdc86888 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| # | |
| # This script will browse a Slack export folder and download all files in a new /export 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. Place this file at the root of your Slack export folder, next to channels.json | |
| # 4. Run `bash slack-files-downloader.sh` 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 | |
| printf "\nSelect one specific file type to download or leave empty for any (e.g. mp3, binary, jpg, png):\n" | |
| read usertype | |
| printf "\nSelect a channel to look into or leave empty for all channels:\n" | |
| read userchannel | |
| for channel in $(cat channels.json | jq -rc '.[].name') | |
| do | |
| if [[ $channel == $userchannel ]] || [[ -z $userchannel ]] | |
| then | |
| printf "\n============================================\nLooking into #$channel...\n============================================\n" | |
| for file in "$channel"/*.json | |
| do | |
| for a in $(cat $file | jq -c '.[].files[]? | [.title, .url_private_download, .filetype, .timestamp] | del(..|nulls)' | sed 's/ //g') | |
| do | |
| filetype=$(echo $a | jq -r '.[2]') | |
| if [[ $filetype == $usertype ]] || [[ -z $usertype ]] || [[ -z $filetype ]] | |
| then | |
| filename_raw=$(echo $a | jq -r '.[0]') | |
| filename=$(echo $filename_raw | sed -e 'y/āáǎàçēéěèīíǐìōóǒòūúǔùǖǘǚǜüĀÁǍÀĒÉĚÈĪÍǏÌŌÓǑÒŪÚǓÙǕǗǙǛÜ/aaaaceeeeiiiioooouuuuuuuuuAAAAEEEEIIIIOOOOUUUUUUUUU/') | |
| filename="${filename##*/}" | |
| if [[ ! -z $filename_raw ]] && [[ $filename_raw != "null" ]] | |
| then | |
| ts=$(echo $a | jq -r '.[3]') | |
| year=$(date -r $ts '+%Y') | |
| month=$(date -r $ts '+%m') | |
| # prefix filenames with date/time, as the same filename could be used | |
| # multiple times in different slack messages with different file contents. | |
| datetime=$(date -r $ts '+%Y%m%d-%H%M%S') | |
| filename="${datetime}-${filename}" | |
| if [ -f "export/$channel/$year/$month/$filename" ] && [[ $overwrite != true ]] | |
| then | |
| if [[ $silent != true ]] | |
| then | |
| printf "$filename already exists in destination folder. Skipping!\n" | |
| fi | |
| continue | |
| fi | |
| printf "Downloading $filename...\n" | |
| mkdir -p export/$channel/$year/$month | |
| url=$(echo $a | jq -rc '.[1]') | |
| printf "downloading to export/$channel/$year/$month/$filename with $ts\n" | |
| curl --progress-bar $url -o "export/$channel/$year/$month/$filename" | |
| fi | |
| fi | |
| done | |
| done | |
| fi | |
| done |
Thank you so much for your contribution, this really saved me a lot of time, and thanks to @v01pe for the fork, which I forked off of as well and made my own rendition. Much appreciated and thank you both for sharing!
@v01pe Thanks for the suggestion! I've updated the gist to include your change 🙏
I had to add:
channel=${channel//$'\r'/}
right before:
printf "\n============================================\nLooking into #$channel...\n============================================\n"
because there were some \r's sneaking in there and breaking all kinds of stuff.
and I also had to use datetime=$(date '+%H%M%S') because the datetime format you have gave me an empty string. Likewise with your year and month strings.
This is possibly all because I'm using Git Bash on Windows rather than a real bash client.
But now that I've done all that it seems pretty happy! Thanks!
Attention: I just realized (using a forked version of your script), that if there are multiple files per message, only the first one will be downloaded! Instead of filtering
'.[].files[0]', you can filter'.[].files[]?', which will iterate all files, if any are available. This also is much faster for channels with many messages, that don't contain files.