transparent-chat /Chat streamer/Badges.cs

Language C# Lines 105
MD5 Hash f6c6bf6966eda2741f5a94771f2bab94 Estimated Cost $2,408 (why?)
Repository https://bitbucket.org/majosa/transparent-chat.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text;
using System.Net;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;

namespace Chat_streamer
{
    public class Badges
    {
        enum BadgeType
        {
            Mod, Admin, Boardcaster, Staff, Subscriptor
        }
        private Dictionary<String, BitmapSource> badges;
        public Badges(String channel)
        {
            WebClient request = new WebClient();
            request.OpenReadCompleted+=onFileOpened;
            request.OpenReadAsync(new Uri("https://api.twitch.tv/kraken/chat/"+channel+"/badges"));
            badges = new Dictionary<string, BitmapSource>();
        }

        void openResource(String badge, String url)
        {
            DownloaderArgs args=new DownloaderArgs();
            args.cache=BitmapCacheOption.OnDemand;
            args.options=BitmapCreateOptions.None;
            args.downloadedHandle=delegate(object sender, EventArgs ev)
            {
                badges.Add(badge, ((BitmapDecoder)sender).Frames[0]);
            };
            args.path=new Uri(url);
            DownloadersQueue.AddToQueue(args);
        }

        void onFileOpened(object sender, OpenReadCompletedEventArgs e)
        {
            JObject doc = (JObject)JObject.Load(new JsonTextReader(new StreamReader(e.Result)));
            foreach(var element in doc){
                if (element.Key == "mod") continue; //Outdated icon
                if (element.Key[0] == '_') continue; //Autoreference node
                if (!element.Value.HasValues) continue; //In subscriber node can appear this case
                JToken image;
                if (((JObject)element.Value).TryGetValue("alpha",out image))
                {
                    openResource(element.Key, image.Value<string>());
                }
                else
                {
                    openResource(element.Key, element.Value["image"].Value<string>());
                }
            }
            //JSON keeps outdated, use real badges
            openResource("mod", "http://www-cdn.jtvnw.net/images/xarth/g/g18_sword-FFFFFF80.png");
            openResource("ign", "http://www-cdn.jtvnw.net/images/xarth/g/g18_ign-00000080.png");
        }
        public BitmapSource GetBadge(String name)
        {
            return badges.ContainsKey(name) ? badges[name] : null;
        }
        public BitmapSource Mod
        {
            get
            {
                return badges.ContainsKey("mod") ? badges["mod"] : null;
            }
        }
        public BitmapSource Staff
        {
            get
            {
                return badges.ContainsKey("staff") ? badges["staff"] : null;
            }
        }
        public BitmapSource Admin
        {
            get
            {
                return badges.ContainsKey("admin") ? badges["admin"] : null;
            }
        }
        public BitmapSource Boardcaster
        {
            get
            {
                return badges.ContainsKey("broadcaster") ? badges["broadcaster"] : null;
            }
        }
        public BitmapSource Subscriptor
        {
            get
            {
                return badges.ContainsKey("chansub") ? badges["chansub"] : null;
            }
        }
    }
}
Back to Top