Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amjohnson38/49a1dd14f948c199284474d912519242 to your computer and use it in GitHub Desktop.
Save amjohnson38/49a1dd14f948c199284474d912519242 to your computer and use it in GitHub Desktop.
FreeCodeCamp Twitchtv JSON API Zipline Project :Who's On Twitchtv?

FreeCodeCamp Twitchtv JSON API Zipline Project :Who's On Twitchtv?

The objective of this project is to build an app that is functionally similar to this: https://codepen.io/FreeCodeCamp/full/Myvqmo/. Also, it needed to address the following parameters: 1. The user can see whether Free Code Camp is currently streaming on Twitch.tv. 2. The user can click the status output and be sent directly to the Free Code Camp's Twitch.tv channel. 3. If a Twitch user is currently streaming, I can see additional details about what they are streaming. 4. The user will see a placeholder notification if a streamer has closed their Twitch account (or the account never existed).

A Pen by Angela Johnson on CodePen.

License.

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Who's On Twitchtv?</title>
</head>
<body>
<div class="container text-center">
<h1>Who's On Twitchtv?</h1>
<div class="outerbox">
<button type="button" class="btn btn-default btn-circle btn-xl btn-all">All</button>
<button type="button" class="btn btn-default btn-circle btn-xl btn-online">Online</button>
<button type="button" class="btn btn-default btn-circle btn-xl btn-offline">Offline</button>
<div class="innerbox">
<div class="imageLogo">
<img class=twitchLogo src="https://res.cloudinary.com/angiemjohnson/image/upload/c_scale,w_199/v1465069734/Twitch_bcmopr.png" alt="twitchlogo">
</div>
<div class="input-group col-md-4">
<input type="text" class="search-query form-control" placeholder="Search User ID" />
<span class="input-group-btn">
<button class="btn btn-default searchInput" type="button">
<span class=" glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div id="twitchUsersContainer" " class="row "></div>
</div>
<h2> By Angela M. Johnson</h2>
</div>
</div>
</body>
</html>
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck",
"habathcx", "RobotCaleb", "noobs2ninjas", "brunofin", "comster404"];
/*************************************************************************************
* Calls the apis, registers the button handlers on load
**************************************************************************************/
$(document).ready(function () {
createUserDivs();
//calls the twitch stream and channel apis
retrieveChannelInfo();
retrieveStreamInfo();
//register the button handlers
//gets the user id, which is the same as the div id. Allows selection of individual divs that
//corresponds to the user id because the div id corresponds to user id, which allows you to
//show individual divs.
$(".btn-all").click(function () {
for (var i = 0; i < users.length; i++) {
$("#" + users[i]).show();
}
console.log("show all clicked");
});
$(".btn-online").click(function () {
for (var i = 0; i < users.length; i++) {
var status = $("#" + users[i] + " .userStreamInfo").text();
if (status === "Online") {
$("#" + users[i]).show();
} else{
$("#" + users[i]).hide();
}
}
});
$(".btn-offline").click(function () {
for (var i = 0; i < users.length; i++) {
var status = $("#" + users[i] + " .userStreamInfo").text();
if (status === "Offline") {
$("#" + users[i]).show();
} else{
$("#" + users[i]).hide();
}
}
});
//allows the user to search for a specific user's ID
$(".searchInput").click(function () {
var text = $("input").val();
$(".twitchUser").hide();
$("#" + text).show();
if (text === ""){
alert("Please enter user's ID");
}
});
});
/*************************************************************************************
* Creates Div and other elements for each user
**************************************************************************************/
var createUserDivs = function () {
for (var i = 0; i < users.length; i++) {
// create the parent div
var div = document.createElement("DIV");
div.id = users[i];
div.className = "twitchUser text-center col-md-2";
// create image child
var image = document.createElement("IMG");
image.className = "channelImage";
div.appendChild(image);
// create h3 for userName
var userName = document.createElement("DIV");
userName.className = "userName";
div.appendChild(userName);
// create p for userStreamInfo
var userStreamInfo = document.createElement("DIV");
userStreamInfo.className = "userStreamInfo";
div.appendChild(userStreamInfo);
//create a link for userStatus
var userStatus = document.createElement("A");
userStatus.className = "userStatus ";
div.appendChild(userStatus);
//create a p for user game status
var game = document.createElement("P");
game.className = "game";
div.appendChild(game);
document.getElementById("twitchUsersContainer").appendChild(div);
}
}
/********************************************************************************************
* Calls the Twitch streams api to get information about the user's streaming status
**********************************************************************************************/
function retrieveStreamInfo() {
var streamApiUrl = "https://api.twitch.tv/kraken/streams/";
for (var i = 0; i < users.length; i++) {
$.ajax({
type: 'GET',
url: streamApiUrl + users[i],
userId: users[i],
dataType: "jsonp",
success: function (data) {
console.log(this.userId);
console.log(data);
var streamInfo = {
userStreamInfo: data.stream,
userStatus: data.status
};
displayStreamData(streamInfo, this.userId);
},
error: function (errorDisplay) {
var streamInfo = {
userStatus: '400'
};
displayStreamData(streamInfo, this.userId);
}
});
}
}
/********************************************************************************************
* Calls the Twitch channels api to retrieve information about each user
**********************************************************************************************/
function retrieveChannelInfo() {
var channelApiUrl = "https://api.twitch.tv/kraken/channels/";
for (var i = 0; i < users.length; i++) {
$.ajax({
type: 'GET',
url: channelApiUrl + users[i],
userId: users[i],
dataType: "jsonp",
success: function (data) {
console.log(this.userId);
//console.log(data);
var channelInfo = {
userStatus: data.status,
userName: data.display_name,
userLogo: data.logo,
message: data.message,
userUrl: data.url,
game: data.game
};
//console.log(users[i]);
//console.log(channelInfo);
displayChannelData(channelInfo, this.userId);
},
error: function (errorDisplay) {
alert("Error! Could not retrieve information");
}
});
}
}
/********************************************************************************************
* Displays the users status, logo, and url based on their Twitch status
**********************************************************************************************/
function displayChannelData(channelInfo, userId) {
// gets the image element by the document.get method to retrieve info in said element id.
var logoElementArray = document.getElementById(userId).getElementsByClassName("channelImage");
var userStatusInfo = document.getElementById(userId).getElementsByClassName("userStatus");
var att = document.createAttribute("href");
att.value = channelInfo.userUrl;
userStatusInfo[0].setAttributeNode(att);
//display the username into the assigned div and class
$('#' + userId + " .userName").text(userId);
if (channelInfo.userStatus === null) {
$('#' + userId + " .userStatus").text();
$('#' + userId + " .game").text();
logoElementArray[0].src = channelInfo.userLogo;
} else if (channelInfo.userStatus === 422 || channelInfo.userStatus === 404) {
logoElementArray[0].src = "https://res.cloudinary.com/angiemjohnson/image/upload/v1465090995/delete_x_red_globe_89_u0brnq.jpg";
} else {
$('#' + userId + " .userStatus").text(channelInfo.userStatus);
$('#' + userId + " .game").text("stream:" + channelInfo.game);
logoElementArray[0].src = channelInfo.userLogo;
}
if (channelInfo.game === null) {
$("#" + userId + " .game").hide();
}
}
/********************************************************************************************
* Displays whether the user is streaming or not
**********************************************************************************************/
function displayStreamData(streamInfo, userId) {
if (streamInfo.userStreamInfo === null) {
$('#' + userId + " .userStreamInfo").text("Offline");
} else if (streamInfo.userStatus === 422 ||
streamInfo.userStatus === 404 ||
streamInfo.userStatus === '400') {
$('#' + userId + " .userStreamInfo").text("Account doesn't exist");
} else {
$('#' + userId + " .userStreamInfo").text("Online");
}
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
.outerbox {
border:5px solid black;
width:100%;
padding:1em;
background-color:black;
-webkit-box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
-moz-box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
}
.innerbox {
border:4px solid black;
width:100%;
padding: .5em;
background-color:lightgray;
}
.searchInput{
margin-left:3em;
}
.imageLogo{
margin-bottom:1em;
}
.twitchLogo{
height:6em;
width:auto;
}
.twitchUser
{
min-height: 18em;
background-color: white;
margin:1em;
padding:1em;
-webkit-box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
-moz-box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
box-shadow: 0px 3px 5px 6px rgba(0,0,0,0.72);
}
#twitchUsersContainer{
padding: .5em;
}
.channelImage{
height:25%;
width:25%;
margin-right:1em;
margin-top:.5em;
}
.btn{
background-color: rebeccapurple;
color: lightgray;
}
.btn-xl{
margin-top:-1em;
}
.userStreamInfo{
font-family: 'Montserrat', sans-serif;
}
.userStatus{
font-family: 'Merriweather', serif;
}
.userName{
font-family: 'Bitter', serif;
}
h2{
color:lightgray;
font-family: 'Norican', cursive;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Montserrat:700" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Merriweather:400italic" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Bitter:700" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Norican" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment