PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 12ms app.codeStats 0ms

/Chat streamer/Badges.cs

https://bitbucket.org/majosa/transparent-chat
C# | 104 lines | 100 code | 3 blank | 1 comment | 6 complexity | 9f93aacebfccaf78ca652ed371758c95 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Xml.Linq;
  5using Newtonsoft.Json;
  6using Newtonsoft.Json.Linq;
  7using System.IO;
  8using System.Text;
  9using System.Net;
 10using System.Windows.Media.Imaging;
 11using System.Threading.Tasks;
 12
 13namespace Chat_streamer
 14{
 15    public class Badges
 16    {
 17        enum BadgeType
 18        {
 19            Mod, Admin, Boardcaster, Staff, Subscriptor
 20        }
 21        private Dictionary<String, BitmapSource> badges;
 22        public Badges(String channel)
 23        {
 24            WebClient request = new WebClient();
 25            request.OpenReadCompleted+=onFileOpened;
 26            request.OpenReadAsync(new Uri("https://api.twitch.tv/kraken/chat/"+channel+"/badges"));
 27            badges = new Dictionary<string, BitmapSource>();
 28        }
 29
 30        void openResource(String badge, String url)
 31        {
 32            DownloaderArgs args=new DownloaderArgs();
 33            args.cache=BitmapCacheOption.OnDemand;
 34            args.options=BitmapCreateOptions.None;
 35            args.downloadedHandle=delegate(object sender, EventArgs ev)
 36            {
 37                badges.Add(badge, ((BitmapDecoder)sender).Frames[0]);
 38            };
 39            args.path=new Uri(url);
 40            DownloadersQueue.AddToQueue(args);
 41        }
 42
 43        void onFileOpened(object sender, OpenReadCompletedEventArgs e)
 44        {
 45            JObject doc = (JObject)JObject.Load(new JsonTextReader(new StreamReader(e.Result)));
 46            foreach(var element in doc){
 47                if (element.Key == "mod") continue; //Outdated icon
 48                if (element.Key[0] == '_') continue; //Autoreference node
 49                if (!element.Value.HasValues) continue; //In subscriber node can appear this case
 50                JToken image;
 51                if (((JObject)element.Value).TryGetValue("alpha",out image))
 52                {
 53                    openResource(element.Key, image.Value<string>());
 54                }
 55                else
 56                {
 57                    openResource(element.Key, element.Value["image"].Value<string>());
 58                }
 59            }
 60            //JSON keeps outdated, use real badges
 61            openResource("mod", "http://www-cdn.jtvnw.net/images/xarth/g/g18_sword-FFFFFF80.png");
 62            openResource("ign", "http://www-cdn.jtvnw.net/images/xarth/g/g18_ign-00000080.png");
 63        }
 64        public BitmapSource GetBadge(String name)
 65        {
 66            return badges.ContainsKey(name) ? badges[name] : null;
 67        }
 68        public BitmapSource Mod
 69        {
 70            get
 71            {
 72                return badges.ContainsKey("mod") ? badges["mod"] : null;
 73            }
 74        }
 75        public BitmapSource Staff
 76        {
 77            get
 78            {
 79                return badges.ContainsKey("staff") ? badges["staff"] : null;
 80            }
 81        }
 82        public BitmapSource Admin
 83        {
 84            get
 85            {
 86                return badges.ContainsKey("admin") ? badges["admin"] : null;
 87            }
 88        }
 89        public BitmapSource Boardcaster
 90        {
 91            get
 92            {
 93                return badges.ContainsKey("broadcaster") ? badges["broadcaster"] : null;
 94            }
 95        }
 96        public BitmapSource Subscriptor
 97        {
 98            get
 99            {
100                return badges.ContainsKey("chansub") ? badges["chansub"] : null;
101            }
102        }
103    }
104}