Skip to content

Instantly share code, notes, and snippets.

@timetocode
Last active December 31, 2015 14:09
Show Gist options
  • Save timetocode/7998194 to your computer and use it in GitHub Desktop.
Save timetocode/7998194 to your computer and use it in GitHub Desktop.
Query Twitch.tv to see if a channel is streaming! Assumes a div of id = 'streaming' to put the results, and a couple css classes to style the results.
<script type="text/javascript">
$(document).ready(function() {
// your twitch channel name
var twitchName = 'yourchannelname'
// an element on your page to put the widget into
var elementSelector = '#streaming'
// HTML to display while waiting for data
var loadingHTML = "<b class='twitchLoading'>Querying twitch.tv...</b> <a href='http://www.twitch.tv/" + twitchName + "'>http://www.twitch.tv/" + twitchName + "</a>"
// HTML to display while online
var onlineHTML = "<b class='twitchOnline'>*ONLINE*</b> <a href='http://www.twitch.tv/" + twitchName + "'>http://www.twitch.tv/" + twitchName + "</a>"
// HTML to display while offline
var offlineHTML = "<b class='twitchOffline'>*OFFLINE*</b> <a href='http://www.twitch.tv/" + twitchName + "'>http://www.twitch.tv/" + twitchName + "</a>"
$(elementSelector).html(loadingHTML)
$.getJSON(
'http://api.justin.tv/api/stream/list.json?channel=' + twitchName + '&jsonp=?',
function (twitchData) {
if (twitchData && twitchData[0]) {
if (twitchData[0].stream_type && twitchData[0].stream_type === 'live') {
// live
$(elementSelector).html(onlineHTML)
} else {
// something other than live
$(elementSelector).html(offlineHTML)
}
} else {
// no data or invalid data from twitch
$(elementSelector).html(offlineHTML)
}
}
)
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment