Last active
June 28, 2024 10:11
-
-
Save marnitto/f9970c2bbf2798782c06bc2b9d58c0cc to your computer and use it in GitHub Desktop.
Slack file downloader from export archive
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 | |
# | |
# 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. | |
# `python 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: | |
if 'file' not in content: | |
continue | |
if 'url_private_download' not in content['file']: | |
continue | |
url = content['file']['url_private_download'] | |
try: | |
os.mkdir(os.path.join(channel, date)) | |
except OSError: | |
pass | |
filename = urlparse.urlparse(url).path.split('/')[-1] | |
download_path = os.path.join(channel, date, | |
filename) | |
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