Advertisement
Guest User

/r/DotA2 Twitch Stream List

a guest
Jul 30th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var logocss = {'float': 'left', 'display': 'inline', 'width': '32px', 'height': '32px'};
  2. var pcss = {'padding-left': '13px', 'display': 'inline'};
  3. var licss = {'padding': '2px 7px 6px 13px', 'display': 'none', 'overflow': 'hidden', 'height': '40px'};
  4. var base = $('<ul id="base">');
  5. var streams = {};
  6.  
  7. function streamElement(s) {
  8.     var elem = $('<li id="' + s._id + '" class="twitch"> ').css(licss),
  9.         wrapper = $('<p>').appendTo(elem),
  10.         container = $('<a>').css({'height': '62px'}).attr({'target': '_blank', 'href': s.channel.url}).appendTo(wrapper);
  11.     if (s.channel.logo === null)
  12.         s.channel.logo = "http://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_300x300.png";
  13.     $('<img>').attr('src', s.channel.logo).css(logocss).appendTo(container).load(function () {
  14.         elem.slideDown(1000);
  15.     });
  16.     $('<p>' + s.channel.display_name + '<small style="color:#888"> (' + s.viewers + ' Viewers) </small></p>').css(pcss).appendTo(container);
  17.     $('<br><p><small style="line-height: 20px">' + s.channel.status + '</small></p>').css(pcss).appendTo(container);
  18.     return elem;
  19. }
  20.  
  21. function refreshStreams() {
  22.     $.getJSON("https://api.twitch.tv/kraken/streams?game=Dota%202&limit=10", function (data) {
  23.         $.each(streams, function (k, v) {
  24.             this.marked = true;
  25.             this.updated = false;
  26.         });
  27.         $(data.streams).each(function () {
  28.             this.marked = false;
  29.             this.newstream = false;
  30.             if (streams[this._id]) {
  31.                 if (streams[this._id].viewers !== this.viewers)
  32.                     this.updated = true;
  33.             } else {
  34.                 this.newstream = true;
  35.             }
  36.             streams[this._id] = this;
  37.         });
  38.         $.each(streams, function (k, stream) {
  39.             if (stream.marked) {
  40.                 //console.log(stream.channel.name + " deleted");
  41.                 delete streams[k];
  42.                 $('#' + k).slideUp(1000, function () {
  43.                     this.remove();
  44.                 });
  45.             } else if (stream.updated) {
  46.                 //console.log(stream.channel.name + " (" + k + ") updated");
  47.                 if ($('#' + k).length) {
  48.                     $(".twitch").each(function () {
  49.                         if (parseInt($(this).find('small')[0].innerText.slice(2, -10), 10) < stream.viewers) {
  50.                             if ($('#' + k).attr('id') === $(this).prev().attr('id') || $('#' + k).attr('id') === $(this).attr('id')) {
  51.                                 $('#' + k).replaceWith(streamElement(stream).show());
  52.                                 return false;
  53.                             } else {
  54.                                 $('#' + k).slideUp(1000, function () {
  55.                                     this.remove();
  56.                                 });
  57.                                 $(this).before(streamElement(stream));
  58.                                 return false;
  59.                             }
  60.                         }
  61.                     });
  62.                 }
  63.             } else if (stream.newstream) {
  64.                 var placed = false;
  65.                 if ($(".twitch").length) {
  66.                     $(".twitch").each(function () {
  67.                         if (parseInt($(this).find('small')[0].innerText.slice(2, -10), 10) < stream.viewers) {
  68.                             $(this).before(streamElement(stream));
  69.                             placed = true;
  70.                             return false;
  71.                         }
  72.                     });
  73.                 }
  74.                 if (placed)
  75.                     return true;
  76.                 base.append(streamElement(stream));
  77.             } else {
  78.                 //console.log(stream.channel.name + " unchanged");
  79.                 //this should never happen
  80.             }
  81.         });
  82.         setTimeout(refreshStreams, 15000);
  83.     });
  84. }
  85.  
  86. $(function () {
  87.     $.ajaxSetup({
  88.     // Disable caching of AJAX responses
  89.         cache: false
  90.     });
  91.     var listelem = $('<li>').appendTo(base),
  92.         head = $('<p>').appendTo(listelem);
  93.     head.html("Live Streams");
  94.     $('div.md p:eq(5)').after(base);
  95.     refreshStreams();
  96. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement