-
-
Save colelawrence/eab50414e031216532c12b3bc663f263 to your computer and use it in GitHub Desktop.
Slack file downloader from export archive (2021)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# Slack file downloader from export archive | |
# | |
# Requirements: | |
# Python 2 (or Python3 if you can use six) | |
# | |
# How to use: | |
# 1. Log in as admin, export your chat logs, and download archive. | |
# 2. Unarchive archive to directory (ex. TeamName export Apr 24 2016) | |
# 3. Run this script. | |
# `python2 slack-download.py "TeamName export Apr 24 2016"` | |
import json | |
import sys | |
import urllib | |
import urlparse | |
import os | |
import glob | |
def err(message): | |
print('[-] '+message) | |
os.exit(-1) | |
def main(export_path): | |
if not os.path.isdir(export_path): | |
err('Slack exported directory required.') | |
os.chdir(export_path) | |
for check_file in ('users.json', 'channels.json'): | |
if not os.path.isfile(check_file): | |
err('It seems that given directory is not an export directory.') | |
for json_path in glob.glob('*/*.json'): | |
channel, filename = os.path.split(json_path) | |
date = filename.split('.')[0] | |
with open(json_path, 'rb') as f: | |
data = json.load(f) | |
for content in data: | |
files = [] | |
if 'file' in content: | |
files.append(content['file']) | |
if 'files' in content: | |
for file in content['files']: | |
files.append(file) | |
for file in files: | |
if 'url_private_download' not in file: | |
continue | |
url = file['url_private_download'] | |
try: | |
os.mkdir(os.path.join(channel, date)) | |
except OSError: | |
pass | |
# Ensure that the files each have different names for each different id | |
filename = "%s - %s" % (file['id'], urlparse.urlparse(url).path.split('/')[-1]) | |
download_path = os.path.join(channel, date, filename) | |
if os.path.isfile(download_path): | |
print('[-] Already downloaded %s -> %s' % (url, download_path)) | |
else: | |
print('[+] Downloading %s -> %s' % (url, download_path)) | |
with open(download_path, 'wb') as f: | |
f.write(urllib.urlopen(url).read()) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print('Usage: %s <Slack exported directory>' % sys.argv[0]) | |
sys.exit(-1) | |
export_path = sys.argv[1] | |
main(export_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correctly downloads multiple files present in the content and takes improvement from edokan's fork to ensure files with the same name don't overwrite each other (for example when you paste an image into slack it gets
image.png
as a name).