LoginSignup
3
1

More than 3 years have passed since last update.

Slackから全員分のプロフィール画像(アイコン)をダウンロードする

Posted at

決算パーティー用に一人を選ぶルーレットを作るため、社員全員の顔写真をSlackから取りました。

APIでユーザーのアイコンのURLを取得しましょう。
https://slack.com/api/users.list?token=xoxb-12345678-12345679-thairoo1airi6om7Ahga
このような形で、ユーザー全員の情報をjsonで取るために有効なtokenを発行している必要があります。

トークンがない場合は、https://api.slack.com/apps/にいき、
「Create New App」、名前をつけて作成したら「Permission」を選択肢、「User Token Scopes
」でusers.readを設定。
インストールを押すとxoxbから始まるトークンが発行されます。こちらが画像つきで詳しかったです。

有効なtokenの用意ができたら以下のpythonスクリプトでダウンロードができます。レポジトリ(gist)はこちらです。

download-slack-profile-pictures.py
import requests
import time

TOKEN = "xoxb-12345678-12345679-thairoo1airi6om7Ahga"


def main():
    url = "https://slack.com/api/users.list?token=" + TOKEN
    response = requests.get(url)
    response.raise_for_status()
    for i, member in enumerate(response.json()['members']):
        image512px_url = member['profile']['image_512']
        print(i + 1, image512px_url)
        response = requests.get(image512px_url)
        response.raise_for_status()
        filename = f"./{member['team_id']}-{member['id']}-{member['name']}.jpg"
        with open(filename, 'wb') as f:
            f.write(response.content)
        time.sleep(1)


if __name__ == '__main__':
    main()

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1