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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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">



  <div id="pageOne" class="text-center">
    <h1>Use the TwitchTV JSON API</h1>
    <h2>A FreeCodeCamp Exercise</h2>
    
  </div>
  <div class="channelHeaders row text-center">

    <div class="col-xs-2">
      <h2 class="header">Logo:</h2>
    </div>
    <div class="col-xs-2">
      <h2 class="header">Display Name:</h2>
    </div>
    <div class="col-xs-2">
      <h2 class="header">Status:</h2>
    </div>
    <div class="col-xs-2">
      <h2 class="header">Follower Of:</h2>
    </div>
  </div>
  <div id="followerInfo">
    <!--<button id="test">Test Button</button>-->
  </div>
</div>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
              
            
!

CSS

              
                button{
  width: 180px;
}

.header {
  font-size: 1.5em;
}

.textCol{
  font-size: 1em;
  word-wrap: break-word;
}

.logo{
  max-width: 70px;
  max-height: 70px;
  border-radius: 50%;
}

.channelRow{
  border: 1px solid #000;
  text-align: center;
  padding: 20px;
}

.textCol{
  padding: 10px;
}

img{
  size: 50%;
}

.statusColumn{
  color: blue;
  
}
              
            
!

JS

              
                
$(document).ready(function(){
  // Set variables used for API calls:
  var url1="https://wind-bow.gomix.me/twitch-api/streams/freecodecamp?callback=?";
  var twitchApiUrl = "https://api.twitch.tv/kraken"
  var clientID = "gdlz2qp4towqgesit6f8qxk2s89ap5";
  // Set starting list of streamers:
  var streamers = ['freecodecamp', 'bmkibler', 'Savjz', 'WowHobbs']
  // Initialize variables used in other functions:
  var followersListing = {};
  var channelName = "";
  var channelStatus = "";
  var channelLogo = "";
  
  // function called to create a row for each streamer displayed.
  // It first sets placeholder values for Logo and Status values and places two buttons.
  function createRow(streamer, following){
    $('#followerInfo').append("<div id = '" + streamer + "row' class = 'row channelRow'><div class='col-xs-2 logodiv'>Logo Goes Here</div><div class='col-xs-2 textCol'>" + streamer + "</div><div class='col-xs-2 statusColumn textCol'>Status Goes Here</div><div class='col-xs-2 textCol'>" + following + "</div><div class='col-xs-2 textCol'><button class='addFollowedChannels'>Add 5 Followed Channels</button><button class='hideChannel'>Hide Channel</button></div>");
  }
  
  //function called to update status information for each Streamer row.  It is called by the checkOnlineStatus function.
  function updateStreamerStatus(streamer, status, game, viewers){
    if(status==="offline"){
      $("#" + streamer + "row div:nth-child(3)").html("<a href='https://www.twitch.tv/"+streamer+"'rel='noopener noreferrer' target='_blank'>"+status+"</a>");
    } else {
      $("#" + streamer + "row div:nth-child(3)").html("<a href='https://www.twitch.tv/"+streamer+"'rel='noopener noreferrer' target='_blank'>"+status+"</a> <p>game:" +game+" </p><p>viewers: "+viewers+"</p>");
    }
  }
  
  // function called to add the streamer's logo to the Streamer row.  It is called by the getChannelInfo function.
  function addStreamerLogo(streamer, logo){
    $("#" + streamer + "row div:nth-child(1)").html("<img src='" + logo + "' class='logo'>");
  }
  
  // function called to add the streamer's display name to the Streamer row.  It is called by the getChannelInfo function.
  function addStreamerDisplayName(streamer, displayName){
    $("#" + streamer + "row div:nth-child(2)").html(displayName);
  }
  
  // function called to add an array of followers to the object that tracks followers needing to be added to the page.
  function addFollowersToListing(streamer, followers){
    followersListing[streamer] = followers;
  }
  
  // function that makes a call to the Twitch API to check a streamer's status.  If the streamer is online, the function collects information about the game played and the number of viewers.  It then uses this information to call the updateStreamerStatus function.  It is called by the addStreamerInfo function.
  function checkOnlineStatus(streamer){
    var checkOnlineUrl = twitchApiUrl + "/streams/" + streamer;
    var status = "";
    var game = "";
    var viewers = null;
    $.ajax({
      type:"GET",
      url:checkOnlineUrl,
      headers:{
      'Client-ID': clientID
      },
      success: function(streamData){
        console.log(streamData);
        if(streamData.stream===null){
          status = "offline";
        } else {
          status = "online";
          game = streamData.stream.game;
          viewers = streamData.stream.viewers;
        }
        updateStreamerStatus(streamer, status, game, viewers);
      }
    })
  }
  
  // function that gets logo and display_name information for each streamer.  It is called by the addStreamerInfo function.
  function getChannelInfo(streamer) {
    var channelInfoUrl = twitchApiUrl + "/channels/" + streamer;
    $.ajax({
      type:"GET",
      url:channelInfoUrl,
      headers:{
        'Client-ID': clientID
      },  
      success: function(channelData) {
        addStreamerLogo(streamer, channelData.logo);
        addStreamerDisplayName(streamer, channelData.display_name);
      }
    })
  }
  
  // master function that calls other functions for each streamer to be added - first createRow to create a row for the streamer; then checkOnlineStatus to collect additional information if the stremaer is online; then getChannelInfo to collect logo and display_name information.  It is called by the document.ready() function as well as by the addFiveFollowers function.
  function addStreamerInfo(streamer, followedBy){
    createRow(streamer, followedBy);
    checkOnlineStatus(streamer);
    getChannelInfo(streamer);
  }
  
  // function that adds streamer information for five of the followers in the array of followers associated with a particular streamer.  It is called by the getListOfFollowers function.
  function addFiveFollowers(streamer){
    for(var i=0; i<5; i++){
      addStreamerInfo(followersListing[streamer].pop(), streamer);
    }
  }
  
  // function that makes a call to the Twitch API to get the list of followers for a particular streamer.  If the call is successful, it checks to see if the list of followers has already been added to the followersListing object for that streamer.  If it has, then it calls addFiveFollowers for that streamer.  If it has not, then it adds all followers for that streamer to the followersListing object.  It is called by the Add 5 Followed Channels button.
  function getListOfFollowers(streamer){
    var getFollowersUrl = twitchApiUrl + "/users/" + streamer + "/follows/channels/";
    $.ajax({
      type:"GET",
      url:getFollowersUrl,
      headers:{
      'Client-ID': clientID
      },
      success: function(followersData){
        if(followersListing.hasOwnProperty(streamer)){
          console.log("We already have a list here");
          addFiveFollowers(streamer);
        } else {
          var followersList = [];
          for(var i=0;i<followersData.follows.length;i++){
            followersList.push(followersData.follows[i].channel.display_name);
          }
          addFollowersToListing(streamer, followersList);
          console.log(followersListing);
          addFiveFollowers(streamer);
        }        
      }
    })
  }
  
  // Initializes the page by calling addStreamerInfo for the starting list of streamers.
  for(var i = 0; i<streamers.length; i++){
    addStreamerInfo(streamers[i], "N/A");
  }
  
  $("body").on("click", ".addFollowedChannels", function(){
    var displayName = $(this).parent().prev().prev().prev().text();
    getListOfFollowers(displayName);
  });
  
    $('body').on("click", ".hideChannel", function(){
    $(this).parent().parent().hide();    
  });
});
              
            
!
999px

Console