Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container-fluid text-center">
  <header>
    <h1><img src="https://pbs.twimg.com/profile_images/509073338191183872/fYdty6yd.png" alt="twitch logo" width="7%"/>Twitch Streamers</h1>
  </header>
  <main>
    <article id="online_streams">
      <h2>Online Streamers</h2>
    </article>
    <article id="featured_streams">
      <h2>Featured Streamers</h2>
      <ul id="online">
      <h3>Online featured streamers</h3>
      </ul>
      <ul id="offline">
      <h3>Offline featured streamers</h3>
      </ul>
      <ul id="closed">
        <h3>The following accounts have been closed or deleted :</h3>
      </ul>
    </article>
  </main>
  <footer class="pull-right">Coded by <a href="http://bernardpagoaga.net">Bernard Pagoaga</a></footer>
</div>
              
            
!

CSS

              
                .container-fluid {padding: 10vh 5vw;}
main { padding: 5vh 0;}
h1 {
  background: #64459B;
  color: white;
  margin: 0;
  padding-top: 2vh;
  padding-bottom: 1vh;
}
h1 img {margin-right: 10px;}

@media screen and(max-width: 768px) {
  hi img {width: 20%;}
  
}
  
h2 {
  color: #64459B;
  border-bottom: 1px solid #64459B;
  padding-bottom: 2vh;
}
h3 {
  color: white;
  background: #64459B;
  padding: 2vh;
}
ul {
  list-style: none;
  padding: 0;
}

article {
  clear: both;
  float: none;
  padding: 5vh 0;
}

a {
  color: #64459B;
  display: inline-block;
  width: 80%;
}

.btn {text-align: left;}
.btn:hover {
  color: #64459B;
}

li:hover {
  margin: -1px;
  border: 1px solid #64459B;
  border-radius: 0;
}
#offline li {
  display: inline-block;
  width: 100%;
}

li img {
  width: 20%;
  margin-right: 10px;
}

#closed {display: none;}

.plus {
  color: #64459B;
  margin-left: 10px;
  width: 15%;
  vertical-align: middle;
  cursor: pointer;
}






              
            
!

JS

              
                // this function get details for featured streamers
function getStreamDetails(featured_channel){

  featured_channel.forEach(function(channel){
    $.getJSON('https://api.twitch.tv/kraken/streams/'+channel+'?client_id=q4m6l1ubzqe21xq819bu08ohrfu1d00&callback=?', function(data) {
      
      var state = data.stream;
      if (data.status == 422){
        $('#closed').append($('<li>').html(data.message)).show();
      }
      
      if (state == null) {
        //put a red background
        var channel = data._links.channel;
        
        $.getJSON(channel+'?client_id=q4m6l1ubzqe21xq819bu08ohrfu1d00&callback=?', function(channel){
          var stream = new OnlineStream(channel.name, channel.game, channel.url, channel.logo);
          
          var li = $('<li>').append($('<a>')
                              .prepend($('<img>').attr('src', stream.thumbnail))
                              .attr('href', channel.url).addClass('btn btn_link').append(channel.name));
          
          $('#offline').append(li);
        });
      
      }else{
        //put a green background
        var channel = data.stream.channel;
        var stream = new OnlineStream(channel.name, channel.game, channel.url, channel.logo);
        
        var game_array = [stream.game];
        //console.log(game_array);
        createUl(game_array, '#online');
        var stream=[stream];
        createLi(stream, "#online");

      }
      
    });
  });
  
}

// Constructor for the OnlineStream Object
function OnlineStream(name, game, url, thumbnail){
  this.name = name;
  this.game = game;
  this.url = url;
  this.thumbnail = thumbnail;
}

//get the online streams from the twitch API (default limit)
function getStreams(){
  $.getJSON('https://api.twitch.tv/kraken/streams?client_id=q4m6l1ubzqe21xq819bu08ohrfu1d00&callback=?', function(data) {
    var streams = createObjects(data.streams);
    //Creating Games lists
    var game_array = gameArray(streams);
    createUl(game_array, "#online_streams");
    
    //Add li of each stream matching the game
    createLi(streams, "#online_streams");
    
    
  });
}

// Create Ul
function createUl(streams, list_id){
  
    var row = $('<div>').addClass('row');
    
    $(list_id).append(row);
    
    streams.forEach(function(game){
      var game_ = game.replace(/[\s:']/g,'_');
      var ul= $('<ul>').addClass("col-xs-12 col-sm-4 col-md-3")
                       .attr('id', game_);
      var h2 = $('<h3>').text(game);
      row.append(ul
           .prepend(h2)
          );
    });
}

// Create Li
function createLi(streams, list_id){
  streams.forEach(function(element){
    
      var game_ = element.game.replace(/[\s:']/g,'_');
      var li = $('<li>');
      var ul = list_id +" "+ "ul#";
  
      $(ul+game_).append(li
                      .append($('<a>')
                        .prepend($('<img>').attr('src', element.thumbnail))
                        .append(element.name)
                        .attr({href : element.url, target : '_blank'})
                        .addClass('btn btn_link')
                      )
                      .append($('<a>')
                        .addClass('plus')
                        .attr({title:'Add to your featured streamers'})
                        .html('+'))
                     
                    );
    });
}

//create an array to store all our objects
function createObjects(data){
  var streams = [];
  for(var i=0; i<data.length; i++){
    streams[i] = new OnlineStream(data[i].channel.name, 
          data[i].channel.game,
          data[i].channel.url,
          data[i].channel.logo);
  }
  return streams;
};

//Create an array listing all different games
function gameArray(array) {
	var game_array = []
  
	for(var i = 0; i < array.length; i++) {
    var game = array[i].game;
    //var is_in_array = $.inArray(game, game_array);
		if( game_array.indexOf(game) == -1 && game) {
			game_array.push(game);
		}
	}
	return game_array;
}


$(document).ready(function(){
  getStreams();
  
  //array of featured streams
  var featured_channels = ["freecodecamp", "storbeck", "terakilobyte", "habathcx","RobotCaleb","thomasballinger","noobs2ninjas","beohoff","riotgames", "mushisgosu", "c9sneaky","brunofin"];
  
  
  // we use the delegation event of jquery, without this we can't click an element generated by our functions
  $('article').on('click', '.plus',function(){
    var elem = $(this).prev();
    var name = [elem[0].innerText];
    getStreamDetails(name);
  });
  
  getStreamDetails(featured_channels);
  
});


              
            
!
999px

Console