Twitch Streamer By Farahmand Moslemi

In this example below you will see how to do a Twitch Streamer By Farahmand Moslemi with some HTML / CSS and Javascript

A Twitch Streamer By Farahmand Moslemi!

Thumbnail
This awesome code was written by _Farahmand, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright _Farahmand ©
  • HTML
  • CSS
  • JavaScript
    <!-- Author: Farahmand Moslemi -->
<div class="container">
  <h1>Twitch <span id="tv">&#x1f4fa</span> Streamer</h1>
  
  <div id="resultWrapper" class="row">
    <div class="col-md-6 col-md-offset-3 col-sm-12">
      <div id="messages"></div>
      <div id="result">
        <div id="actions">
          <!--div data-toggle="buttons-radio"-->
          <div>
            <button class="btn btn-default">All</button>
            <button class="btn btn-default">Online</button>
            <button class="btn btn-default">Offline</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="footer">
  <hr>
  <p id="copyright">&copy; 2017 <a href="https://www.freecodecamp.com/FarahmandM" title="FCC/~FarahmandM" target="_blank" rel="nofollow">Farahmand Moslemi</a> - All rights reserved.</p>
</div>

/*Downloaded from https://www.codeseek.co/_Farahmand/twitch-streamer-by-farahmand-moslemi-MowggO */
    /*!
 * @author: Farahmand Moslemi
*/
@import "compass/reset";
@import "compass/css3";

body {
  background-color: #4b367c;
  color: #fff;
  font-family: 'Anton', sans-serif;
  font-size:18px;
  line-height: 20px;
}

a{
  color: #ddffff;
  text-decoration:none;
}
a:focus,a:hover{
  color: #eeffff;
}
a:focus{
  outline:thin dotted #333;
  outline:5px auto -webkit-focus-ring-color;
  outline-offset:-2px;
}
a:active, a:hover{
  outline:0;
}
a:link, a:visited, a:hover, a:active {
	text-decoration:none;
}

strong {
  font-weight: bold;
}

sup {
  vertical-align: super;
  font-size: 40%;
  font-weight: bold;
}

h1 {
  margin: 20px 0;
  font-size:80px;
  font-weight: bold;
  line-height:100px;
  text-align:center;

  #tv {
    font-size: 180%;
    font-weight: normal;
  }
}

#result {
  display: table;
  width:100%;
  
  & > div#actions {
    display: table-caption;
    text-align: center;
    margin: 14px 0;
  }
  
  & > div {
    display: block;
  }
  
  & > a.item {
    display: table-row;
    margin-bottom: 4px;
    padding:12px;
    width: 100%;
    clear:both;
    color: #333;
    background-color: #f4f4f4;
    @include transition (all 200ms ease-in-out);
    
    &:hover {
      color: #fff;
      background-color: #333;
      border-color: #ddd;
      @include transform(translateX(10px) scale(1.02));
    }
    
    &.online {
      color: #fff;
      background-color: #1abc9c;
      
      &:hover {
        background-color: #16a085;
      }
    }
    
    & > div {
      display: block;
      margin-bottom:8px;
      padding: 0px;
      
      & > div {
        display: table-cell;
        padding:10px;
        vertical-align: middle;
        
        &:last-child {
          text-align:right;
          width:100%;
        }
      }
    }
    
    h2.title {
      margin-bottom: 4px;
      font-weight: bold;
      font-size: 20px;
    }
  }
  
  img {
    width: 64px;
    height: 64px;
    border: 4px solid #fff;
    @include border-radius(10px 2px 10px 2px);
    vertical-align: middle;
  }
}

.sep {
  margin: 10px 0;
  font-size: 20px;
  font-weight: bold;
}
#footer {
  margin-top: 24px;
  padding: 12px 0;
  
  p {
    text-align:center;
  }
}


/*Downloaded from https://www.codeseek.co/_Farahmand/twitch-streamer-by-farahmand-moslemi-MowggO */
    /*
 * @author: Farahmand Moslemi
*/
var usernames = ["freecodecamp", "brunofin", " comster404", "ESL_SC2", "OgamingSC2", "cretetion", "test_channel", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];

var generateAlert = function(message, type = "info", closeBtn = true) { // success | info | warning | danger
  html = '<div class="alert alert-' + type + ' alert-dismissible fade in" role="alert">';
  if (closeBtn) {
    html += '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
  }
  html += message + '</div>';
  
  return html;
}

var offlineUsernames = [];

var generateDataHtml = function(url, logoUrl, name, game = "Offline") {
  var offline = game.toLowerCase() === "offline";
  logoUrl = logoUrl == null ? "https://dummyimage.com/64x64/ddd/000000.jpg&text=" + name : logoUrl;
  return '<a href="' + url + '" title="' + name + '" class="item ' + (offline ? '' : 'online') + '" rel="nofollow" target="_blank"><div><div><img src="' + logoUrl + '" alt="' + name + '"></div><div>' + name + '</div><div>' + game + '</div></div></a><div class="sep"></div>';
}

jQuery(document).ready(function($) {
  var usernames2 = [];
  usernames.forEach(function(username) {
    $.getJSON("https://wind-bow.gomix.me/twitch-api/streams/" + encodeURI($.trim(username)) + "?callback=?").done(function(data) {
      usernames2.push(username);
      
      if(data.error != null) {
        $("#messages").append(generateAlert(data.error + ": " + data.message, "danger"));
        return;
      }
      if(data.stream !== null) {
        $('#result').append(generateDataHtml(data.stream.channel.url, data.stream.channel.logo, data.stream.channel.display_name, data.stream.game + ': ' + data.stream.channel.status));
      } else {
        offlineUsernames.push(username);
      }

      if(usernames.length  === usernames2.length) {
        offlineUsernames.forEach(function(username) {
          $.getJSON("https://wind-bow.gomix.me/twitch-api/channels/" + encodeURI($.trim(username)) + "?callback=?").done(function(data) {
            if(data.error != null) {
              $("#messages").append(generateAlert(data.error + ": " + data.message, "danger"));
              return;
            }
            if(data.stream !== null) {
              $('#result').append(generateDataHtml(data.url, data.logo, data.display_name));
            } else {
              offlineUsernames.push(username);
            }
          }).fail(function(jqxhr, textStatus, error) {
            var err = textStatus + ", " + error;
            $("#messages").append(generateAlert(err, "danger"));
          });
        });
      }
      
    }).fail(function(jqxhr, textStatus, error) {
      var err = textStatus + ", " + error;
      $("#messages").append(generateAlert(err, "danger"));
    });
  });
  
  $('#actions .btn').on('click', function() {
    var $this = $(this);
    $('#actions .btn').each(function() {
      if ($(this).text() === $this.text()) {
        $(this).addClass('btn-primary');
      } else  {
        $(this).removeClass('btn-primary');
      }
    });
    switch($this.text()) {
      case "All":
        $('a.item').show();
        break;
      case "Online":
        $('a.item:not(.online)').hide();
        $('a.item.online').show();
        break;
      case "Offline":
        $('a.item:not(.online)').show();
        $('a.item.online').hide();
        break;
    }
  });
  
});

Comments