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!!!")