Exemplo n.º 1
0
 public void AddCheerEmote(string prefix, CheerEmote emote)
 {
     CheerEmotes.TryAdd(prefix, emote);
 }
Exemplo n.º 2
0
        public static List <CheerEmote> GetBits(string cacheFolder)
        {
            List <CheerEmote> cheerEmotes = new List <CheerEmote>();
            string            bitsFolder  = Path.Combine(cacheFolder, "bits");

            if (!Directory.Exists(bitsFolder))
            {
                Directory.CreateDirectory(bitsFolder);
            }

            using (WebClient client = new WebClient())
            {
                client.Headers.Add("Accept", "application/vnd.twitchtv.v5+json");
                client.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");

                JObject globalCheer = JObject.Parse(client.DownloadString("https://api.twitch.tv/kraken/bits/actions"));

                foreach (JToken emoteToken in globalCheer["actions"])
                {
                    string prefix = emoteToken["prefix"].ToString();
                    List <KeyValuePair <int, ThirdPartyEmote> > tierList = new List <KeyValuePair <int, ThirdPartyEmote> >();
                    CheerEmote newEmote = new CheerEmote()
                    {
                        prefix = prefix, tierList = tierList
                    };
                    byte[] finalBytes = null;
                    foreach (JToken tierToken in emoteToken["tiers"])
                    {
                        try
                        {
                            int    minBits  = tierToken["min_bits"].ToObject <int>();
                            string fileName = Path.Combine(bitsFolder, prefix + minBits + "_2x.gif");

                            if (File.Exists(fileName))
                            {
                                finalBytes = File.ReadAllBytes(fileName);
                            }
                            else
                            {
                                byte[] bytes = client.DownloadData(tierToken["images"]["dark"]["animated"]["2"].ToString());
                                File.WriteAllBytes(fileName, bytes);
                                finalBytes = bytes;
                            }

                            if (finalBytes != null)
                            {
                                MemoryStream    ms    = new MemoryStream(finalBytes);
                                ThirdPartyEmote emote = new ThirdPartyEmote(new List <SKBitmap>()
                                {
                                    SKBitmap.Decode(finalBytes)
                                }, SKCodec.Create(ms), prefix, "gif", "", 2, finalBytes);
                                tierList.Add(new KeyValuePair <int, ThirdPartyEmote>(minBits, emote));
                            }
                        }
                        catch
                        { }
                    }
                    cheerEmotes.Add(newEmote);
                }
            }

            return(cheerEmotes);
        }
Exemplo n.º 3
0
        public static List <CheerEmote> GetBits(string cacheFolder, string channel_id = "")
        {
            List <CheerEmote> cheerEmotes = new List <CheerEmote>();
            string            bitsFolder  = Path.Combine(cacheFolder, "bits");

            if (!Directory.Exists(bitsFolder))
            {
                TwitchHelper.CreateDirectory(bitsFolder);
            }

            using (WebClient client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;
                client.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");

                GqlCheerResponse cheerResponse = JsonConvert.DeserializeObject <GqlCheerResponse>(client.UploadString("https://gql.twitch.tv/gql", "{\"query\":\"query{cheerConfig{groups{nodes{id, prefix, tiers{bits}}, templateURL}},user(id:\\\"" + channel_id + "\\\"){cheer{cheerGroups{nodes{id,prefix,tiers{bits}},templateURL}}}}\",\"variables\":{}}"));

                if (cheerResponse != null && cheerResponse.data != null)
                {
                    List <CheerGroup> groupList = new List <CheerGroup>();

                    foreach (CheerGroup group in cheerResponse.data.cheerConfig.groups)
                    {
                        groupList.Add(group);
                    }

                    if (cheerResponse.data.user != null && cheerResponse.data.user.cheer != null && cheerResponse.data.user.cheer.cheerGroups != null)
                    {
                        foreach (var group in cheerResponse.data.user.cheer.cheerGroups)
                        {
                            groupList.Add(group);
                        }
                    }

                    foreach (CheerGroup group in groupList)
                    {
                        string templateURL = group.templateURL;

                        foreach (CheerNode node in group.nodes)
                        {
                            string prefix = node.prefix;
                            List <KeyValuePair <int, TwitchEmote> > tierList = new List <KeyValuePair <int, TwitchEmote> >();
                            CheerEmote newEmote = new CheerEmote()
                            {
                                prefix = prefix, tierList = tierList
                            };
                            foreach (Tier tier in node.tiers)
                            {
                                try
                                {
                                    int    minBits    = tier.bits;
                                    string fileName   = Path.Combine(bitsFolder, prefix + minBits + "_2x.gif");
                                    byte[] finalBytes = null;

                                    if (File.Exists(fileName))
                                    {
                                        try
                                        {
                                            finalBytes = File.ReadAllBytes(fileName);
                                        }
                                        catch { }
                                    }
                                    if (finalBytes == null)
                                    {
                                        string url   = templateURL.Replace("PREFIX", node.prefix.ToLower()).Replace("BACKGROUND", "dark").Replace("ANIMATION", "animated").Replace("TIER", tier.bits.ToString()).Replace("SCALE.EXTENSION", "2.gif");
                                        byte[] bytes = client.DownloadData(url);
                                        try
                                        {
                                            File.WriteAllBytes(fileName, bytes);
                                        }
                                        catch { }
                                        finalBytes = bytes;
                                    }

                                    if (finalBytes != null)
                                    {
                                        MemoryStream ms    = new MemoryStream(finalBytes);
                                        TwitchEmote  emote = new TwitchEmote(new List <SKBitmap>()
                                        {
                                            SKBitmap.Decode(finalBytes)
                                        }, SKCodec.Create(ms), prefix, "gif", "", 2, finalBytes);
                                        tierList.Add(new KeyValuePair <int, TwitchEmote>(minBits, emote));
                                    }
                                }
                                catch
                                { }
                            }
                            cheerEmotes.Add(newEmote);
                        }
                    }
                }
            }

            return(cheerEmotes);
        }