function update ( ) {
var xhttp = new XMLHttpRequest ( ) ;
xhttp . onreadystatechange = function ( ) {
if ( xhttp . readyState == 4 && xhttp . status == 200 ) {
var json _text = xhttp . responseText ;
var json _obj = JSON . parse ( json _text ) ;
var playing = '' ;
var first = getFirstPlaying ( json _obj ) ;
if ( first == null ) {
playing = 'Mitte munnigi ei mängi prg' ;
}
else if ( first != null && first . artist != undefined ) {
playing = first . artist + ' - ' ;
playing += first . title ;
} else {
playing += first . title ;
}
var nowPlaying = playing ;
update _field ( 'on-air' , playing ) ;
update _listeners ( json _obj ) ;
}
} ;
xhttp . open ( "GET" , "http://kahtlane.eu:8000/status-json.xsl" , true ) ;
xhttp . send ( ) ;
}
function update _field ( field , data ) {
document . getElementById ( field ) . innerHTML = data ;
return 'ok' ;
}
function update _listeners ( json _obj ) {
var nowPlaying = getFirstPlaying ( json _obj ) ;
if ( nowPlaying != null ) {
update _field ( 'listeners1' , nowPlaying . listeners ) ;
}
}
function autoupdate ( ) {
update ( ) ;
setInterval ( function ( ) { update ( ) ; } , 30000 ) ;
}
function change _bg ( id ) {
document . body . style . backgroundImage = "url('http://i.imgur.com/" + id + ".jpg')" ;
}
function reset _bg ( ) {
document . body . style . backgroundImage = "url('http://i.imgur.com/BkSL2KJ.png')" ;
}
function getFirstPlaying ( jsonObj ) {
var sources = jsonObj . icestats . source ;
var masterStreams = [
'/live' ,
'/sai' ,
'/siinus' ,
'/viido'
]
for ( o = 0 ; o < sources . length ; o ++ ) {
if ( sources [ o ] . title != undefined && masterStreams . some ( stream => sources [ o ] . listenurl . endsWith ( stream ) ) ) { //check if source has a title key AND belongs to masterStreams
return sources [ o ] ;
}
}
return null ;
}