123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container-fluid">
  
  <h1 id="title">Twitch API Project</h1>
  
  <div class="row">
    <div class="col-md-3"></div>
    <div id="output" class="col-md-6"></div>
    <div class="col-md-3"></div>
  </div>
  
   <p id="made-by">by <a id="made-by-link"  href="https://codepen.io/ForM/#" target="_blank">Form</a></p>
  
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Russo+One);

body {
  background : black;
  margin: 0 auto;
  text-align: center;
  font-family: 'Russo One', sans-serif;
  font-size: 1.3em;
  
}

#title{
  color: white;
  text-shadow: -1px 0 black, 0 2px black, 2px 0 black, 0 -1px black;
}

#output {
  background-color: #0f145b;
}

.green-text{
  color: green;
}

#made-by{
  margin-top: 40px;
  font-size: 1em;
  color: white;
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

#made-by-link{
  color: #afafb5;
}
            
          
!
            
              $(document).ready(function() {
  
  getInfo(tObj);

});

// A function to get the necessary info.
function getInfo(obj) {
  var status = "";
  var name = "";
  var link = "";
  var nLink = "https://www.twitch.tv/";
  var img = "";
  var color = "";

  for (var i = 0; i < obj.length; i++) {
    // Check if the current element/object in the array of objects has a stream property
    if (obj[i].hasOwnProperty("stream")) {
      // Check if the stream isn't offline
      if (obj[i].stream != null) {
        status = obj[i].stream.status;
        name = obj[i].stream.display_name;
        link = nLink + name;
        img = obj[i].stream.logo;
        color = "green";
      } else { //if it's offline, change 'status' to 'Offline'
        status = "Offline";
        name = obj[i].display_name;
        link = nLink + name;
        img = offlineImg;
        color= "";
      }
    } else if (obj[i].hasOwnProperty("error")) {
      // Check for the case when the channel doesn't exist
      name = obj[i].error;
      status = "Channel doesn't exist";
      link = "#";
      img = errorImg;
      color = "red";
    }
    addInfo(name, status, link, img);
    addStyle();
    console.log(status + " " + link);
  }
}

// A function to add the info the web page by html code
function addInfo(name, status, link, img) {
  var output = $("#output");
  
  output.append('<img class ="logo img-responsive" src =' + img + ' />' + '<p class="name"> ' + name + '</p>');
  output.append('<a class ="link" href="' + link + '" target="_blank">' +'<p class = "status">' + status + '</p></a>');
}

function addStyle() {
  var output = $("#output");
  var logo = $(".logo");
  var name = $(".name");
  var link = $(".link");
  var status = $(".status");

  output.css({
    "border": "1px solid black",
    "border-radius": "2%",
    "margin-top": "50px",
    "padding": "30px",
    "text-align": "left",
  });
  
  logo.css({
    "width": "30px",
    "display": "inline",
    "padding-top": "5px",
  });
  
  name.css({
    "display": "inline",
    "color": "white",
    "font-size": "1.18em",
  });
  
  link.css({
    "color": "#afafb5",
    "font-size" : "0.9em",
  });
  
  status.css({
    
    "padding-top":"15px",
    "padding-bottom":"20px",
    "border-bottom": "0.5px white solid",
  });

}

// Hardcoded twitch API JSON
var tObj = [{
  "stream": {
    "mature": false,
    "status": "Greg working on Electron-Vue boilerplate w/ Akira #programming #vuejs #electron",
    "broadcaster_language": "en",
    "display_name": "FreeCodeCamp",
    "game": "Creative",
    "language": "en",
    "_id": 79776140,
    "name": "freecodecamp",
    "created_at": "2015-01-14T03:36:47Z",
    "updated_at": "2016-09-17T05:00:52Z",
    "delay": null,
    "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/freecodecamp-profile_image-d9514f2df0962329-300x300.png",
    "banner": null,
    "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/freecodecamp-channel_offline_image-b8e133c78cd51cb0-1920x1080.png",
    "background": null,
    "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/freecodecamp-profile_banner-6f5e3445ff474aec-480.png",
    "profile_banner_background_color": null,
    "partner": false,
    "url": "https://www.twitch.tv/freecodecamp",
    "views": 161989,
    "followers": 10048,
    "_links": {
      "self": "https://api.twitch.tv/kraken/channels/freecodecamp",
      "follows": "https://api.twitch.tv/kraken/channels/freecodecamp/follows",
      "commercial": "https://api.twitch.tv/kraken/channels/freecodecamp/commercial",
      "stream_key": "https://api.twitch.tv/kraken/channels/freecodecamp/stream_key",
      "chat": "https://api.twitch.tv/kraken/chat/freecodecamp",
      "subscriptions": "https://api.twitch.tv/kraken/channels/freecodecamp/subscriptions",
      "editors": "https://api.twitch.tv/kraken/channels/freecodecamp/editors",
      "teams": "https://api.twitch.tv/kraken/channels/freecodecamp/teams",
      "videos": "https://api.twitch.tv/kraken/channels/freecodecamp/videos"
    }
  },
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/freecodecamp",
    "channel": "https://api.twitch.tv/kraken/channels/freecodecamp"
  }
}, {
  "stream": null,
  "display_name": "OgamingSC2",
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/ogamingsc2",
    "channel": "https://api.twitch.tv/kraken/channels/ogamingsc2"
  }
}, {
  "stream": {
    "mature": false,
    "status": "RERUN: StarCraft 2 - Kane vs. HuK (ZvP) - WCS Season 3 Challenger AM - Match 4",
    "broadcaster_language": "en",
    "display_name": "ESL_SC2",
    "game": "StarCraft II",
    "language": "en",
    "_id": 30220059,
    "name": "esl_sc2",
    "created_at": "2012-05-02T09:59:20Z",
    "updated_at": "2016-09-17T06:02:57Z",
    "delay": null,
    "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-profile_image-d6db9488cec97125-300x300.jpeg",
    "banner": null,
    "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-channel_offline_image-5a8657f8393c9d85-1920x1080.jpeg",
    "background": null,
    "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-profile_banner-f8295b33d1846e75-480.jpeg",
    "profile_banner_background_color": "#050506",
    "partner": true,
    "url": "https://www.twitch.tv/esl_sc2",
    "views": 60843789,
    "followers": 135275,
    "_links": {
      "self": "https://api.twitch.tv/kraken/channels/esl_sc2",
      "follows": "https://api.twitch.tv/kraken/channels/esl_sc2/follows",
      "commercial": "https://api.twitch.tv/kraken/channels/esl_sc2/commercial",
      "stream_key": "https://api.twitch.tv/kraken/channels/esl_sc2/stream_key",
      "chat": "https://api.twitch.tv/kraken/chat/esl_sc2",
      "subscriptions": "https://api.twitch.tv/kraken/channels/esl_sc2/subscriptions",
      "editors": "https://api.twitch.tv/kraken/channels/esl_sc2/editors",
      "teams": "https://api.twitch.tv/kraken/channels/esl_sc2/teams",
      "videos": "https://api.twitch.tv/kraken/channels/esl_sc2/videos"
    }
  },
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/esl_sc2",
    "channel": "https://api.twitch.tv/kraken/channels/esl_sc2"
  }
}, {
  "stream": null,
  "display_name": "noobs2ninjas",
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams/esl_sc2",
    "channel": "https://api.twitch.tv/kraken/channels/esl_sc2"
  }
}, {
  "error": "Not Found",
  "status": 404,
  "message": "Channel 'not-a-valid-account' does not exist"
}];

var offlineImg = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Antu_user-offline.svg/2000px-Antu_user-offline.svg.png";

var errorImg = "https://upload.wikimedia.org/wikipedia/commons/d/dc/Icon_attention.svg"

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console