Front End Projects: Twitch API

Front End Projects: Twitch API
0

#1

So i’m working on my twitch api project for free code camp and am just wondering if there’s a better way to utilize the twitch api… currently I am making a separate call to the api for each user in my twitchUsers array. Is there a better way?

var twitchUsers = ["freecodecamp", "storbeck", "terakilobyte", "habathcx", "RobotCaleb", "thomasballinger", "noobs2ninjas", "beohoff", "CapcomFighters"];
var $main = $('main');

$(function() {
  $.each(twitchUsers, function(index, user) {
    $.getJSON(`https://api.twitch.tv/kraken/streams/${user}?callback=?`, function(twitch) {
      if (twitch.stream) {
        $main.append(`<p>${user} is currently streaming ${twitch.stream.game}.</p>`);
      } else {
        $main.append(`<p>${user} is offline.</p>`);
      }
    });
  })
})

#2

@raymeibaum in fact you will end up doing each query twice for the users, that is if you are going to include the user picture. One call to streams and one call to channels…


#3

You can try:

'https://api.twitch.tv/kraken/streams?channel=' + twitchUsers + '&callback='

but you need to handle the response differently because it is an array of channels that are online. Then you need to iterate through the original list you searched for in order to get the offline streamers. You need a different api call to check if any data comes back for each one. If it gives you an error, you know that it isn’t a valid channel name.