Skip to content

Instantly share code, notes, and snippets.

@christippett
Created July 11, 2018 06:11
Show Gist options
  • Save christippett/355797d3c9f82563b2eaae619031749e to your computer and use it in GitHub Desktop.
Save christippett/355797d3c9f82563b2eaae619031749e to your computer and use it in GitHub Desktop.
Download files referenced in Slack JSON export
import os
import argparse
import json
import requests
class SlackFileDownloader:
def __init__(self, output_dir, slack_token):
self.output_dir = output_dir
self.slack_token = slack_token
def parse_export(self, export_file):
with open(export_file, 'r') as f:
content = f.read()
events = json.loads(content)
for event in events:
self.process_event(event)
def process_event(self, event):
event_file = event.get('file')
if event_file:
file_url = event_file.get('url_private')
file_name = event_file.get('name')
self.download_file(file_url, file_name)
def download_file(self, url, name):
print(f"Downloading file: {name}")
headers = {'Authorization': f"Bearer {self.slack_token}"}
r = requests.get(url, stream=True, headers=headers)
save_path = os.path.join(self.output_dir, name)
os.makedirs(os.path.dirname(save_path), exist_ok=True)
with open(save_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download file attachments related to Slack export')
parser.add_argument('-f', '--file', help='Exported Slack JSON', required=True)
parser.add_argument('-t', '--token', help='Slack API token (legacy)', required=True)
parser.add_argument('-o', '--output', help='Output directory', default="files")
args = parser.parse_args()
slack_downloader = SlackFileDownloader(output_dir=args.output, slack_token=args.token)
slack_downloader.parse_export(args.file)
@christippett
Copy link
Author

Slack data can be exported using a tool like slack-history-export. Script requires a legacy Slack API token which can be obtained here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment