【C#】Slackからemojiを一括ダウンロードするツールを作った

概要

複数Slack Team間のemojiの同期をしたかったので、emojiを一括ダウンロードしてくるツールをC#で書きました。

Chrome拡張のSlack Emoji Toolsと合わせることで、一括でのemojiのお引っ越しが出来ます。

動作

f:id:notargs:20170516183940p:plain f:id:notargs:20170516183946p:plain

コード

"ここにトークンを入力"ここ(https://api.slack.com/custom-integrations/legacy-tokens)から引っ張ってきたTokenをぶち込みましょう。

あと、参照にSystem.Runtime.Serializationを追加してください。

/*
SlackEmojiDownloader

Copyright (c) 2017 Yutaka Sato

This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace SlackEmojiDownloader
{
    [DataContract]
    internal class EmojiResponce
    {
        [DataMember(Name = "ok")] private bool _ok;
        [DataMember(Name = "emoji")] private Dictionary<string, string> _emoji;

        public bool Ok => _ok;
        public Dictionary<string, string> Emoji => _emoji;
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            const string folderName = "Images";
            const string url = "https://slack.com/api/emoji.list?pretty=1&token=";
            const string token = "ここにトークンを入力";

            Stream stream;
            using (var webClient = new WebClient())
            {
                stream = webClient.OpenRead(url + token);
            }
            if (stream == null) return;

            var settings = new DataContractJsonSerializerSettings {UseSimpleDictionaryFormat = true};
            var jsonSerializer = new DataContractJsonSerializer(typeof(EmojiResponce), settings);
            var jsonObj = jsonSerializer.ReadObject(stream) as EmojiResponce;

            Directory.CreateDirectory(folderName);

            if (jsonObj == null) return;
            
            foreach (var keyValuePair in jsonObj.Emoji)
            {
                var value = keyValuePair.Value;
                var key = keyValuePair.Key;
                var fileName = Path.Combine(folderName, key + Path.GetExtension(value));
                Console.WriteLine(fileName);
                var webClient = new WebClient();
                if (value.Substring(0, 5) == "alias") continue;
                webClient.DownloadFile(new Uri(value), fileName);
            }
        }
    }
}