Created
November 2, 2017 23:45
-
-
Save intentionally-left-nil/90bc480fa10d1efcf57ceaf7d1c00f0c to your computer and use it in GitHub Desktop.
download files from slack
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
import os | |
from slackclient import SlackClient | |
import requests | |
slack_token = os.environ["SLACK_API_TOKEN"] | |
sc = SlackClient(slack_token) | |
if not os.path.exists('files'): | |
os.makedirs('files') | |
channels = sc.api_call("channels.list")["channels"] | |
sightings_channel = next(x for x in channels if x["name"] == "sasquatch_sightings") | |
channel_id = sightings_channel["id"] | |
files = sc.api_call("files.list", channel=channel_id, types="images", count=1000)["files"] | |
total = len(files) | |
for idx, item in enumerate(files): | |
url = item["url_private_download"] | |
title = item["title"] | |
filetype = item["filetype"] | |
if not title.endswith(f".{filetype}"): | |
title = f"{title}.{filetype}" | |
timestamp = item["timestamp"] | |
headers = { | |
"Authorization": "Bearer " + slack_token | |
} | |
response = requests.get(url, headers=headers, stream=True) | |
response.raise_for_status() | |
path = os.path.join('files', f"{timestamp}_{title}") | |
print(f"downloading {idx}/{total}") | |
with open(path, 'wb') as handle: | |
for block in response.iter_content(1024): | |
handle.write(block) | |
print("DONE!!!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment