LoginSignup
5
1

More than 3 years have passed since last update.

RubyでSlackの絵文字(emoji)を一括エクスポート

Last updated at Posted at 2019-06-27

はじめに

  • タイトルの通りです。
  • Slackのワークスペースを引っ越しするというイベントがございました。
  • すでに諸先輩方に書いていただいたものをRubyで書いてみただけです。

Special Thanks

こちらでSlackのAPI(emoji.list)をお試しいただけます

作品

require 'open-uri'
require 'json'
require 'pathname'

# https://api.slack.com/apps
# |> Create New App
# |> Add features and functionality
# |> Permissions
# |> Scopes
# |> emoji:read
# |> xoxp-****
# |> replace TOKEN
TOKEN = 'とーくん'
URL = "https://slack.com/api/emoji.list?token=#{TOKEN}"
IMAGES_DIR = Pathname('images')

IMAGES_DIR.mkpath

body = open(URL, &:read)
emojis = JSON.parse(body)['emoji']

f = ->(_, url) { url.start_with?('alias') }
size = emojis.reject(&f).size

emojis.reject(&f).each.with_index(1) do |(key, url), i|
  #puts "#{key} => #{url}"
  extention = url.split('.').last

  filepath = IMAGES_DIR + "#{key}.#{extention}"
  filepath.binwrite open(url, &:read)

  print "%3d %%\e[G" % (i*100/size)
end

動作確認環境

$ ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
  • MacBook Pro(13-inch,2017,Two Thunderbolt 3 ports)
  • macOS Mojave バージョン 10.14.5
5
1
2

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
5
1