Search code examples
javascriptjquerystreamstatustwitch

Simple Twitch status


I am looking for Twitch status toggle for my forum. I have a list with streamers and I want green/red point for online/offline streams or just ONLINE or OFFLINE. I found what I need but was unable to make it work. Every help will be welcome. I dont have any javascript knowledge, so please dont blame me for stupid mistakes.

Here is the code I found:

jQuery(document).ready(function($) {
    $('.ltwitch').each(function () {
        var tnick = $(this).data('tnick');
        var span = $(this).next();
	    $.getJSON("https://api.twitch.tv/kraken/streams/"+tnick+".json?callback=?", function(c) {
	    	if (c.stream == null) {
    			span.html("Offline");
		    } else {
			    span.html("Online");
		    }
	    });
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="ltwitch" href="#" data-tnick="alohadancetv">Aloha</a> (<span>...</span>)

http://jsfiddle.net/LYv3R/5/

Regards


Solution

  • You need to send the request with your client_id

    Example: ?client_id=48728413r2fdmq4i4otfjrtu9f8z2ou

    You can Register an application and receive a client_id here (at the bottom of the page)

    In your code (this will only work if you use your own client id ofc):

    jQuery(document).ready(function($) {
        $('.ltwitch').each(function () {
            var tnick = $(this).data('tnick');
            var span = $(this).next();
            $.getJSON("https://api.twitch.tv/kraken/streams/"+tnick+"?client_id=48728413r2fdmq4i4otfjrtu9f8z2ou", function(c) {
                if (c.stream == null) {
                    span.html("Offline");
                } else {
                    span.html("Online");
                }
            });
        });
    });