-
-
Save person4268/6c3da99c2646995e588797709d8c3505 to your computer and use it in GitHub Desktop.
Slack file downloader from export archive (2021)
This file contains hidden or 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 | |
# | |
# Updated to python 3 with 2to3, and tqdm was added. | |
# Usage: slack-download.py <extracted archive export dir> <full path to download dir> | |
import json | |
import sys | |
import urllib.request, urllib.parse, urllib.error | |
import urllib.parse | |
import os | |
import glob | |
from tqdm import tqdm | |
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 tqdm(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'], urllib.parse.urlparse(url).path.split('/')[-1]) | |
download_path = os.path.join(sys.argv[2], channel, date, filename) | |
os.makedirs(os.path.dirname(download_path), exist_ok=True) | |
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.request.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