function loop() {
    getCurrentPlaying().then(result => {
        updateCurrentPlaying(dJSON.parse(result));
        if (songStorage.current === undefined) {
            //Player is running for the first time.
            console.log("Running for the first time.")
            app.emit('trayUpdate', dJSON.parse(result), false)
            songStorage.time = 5;
            songStorage.ready = false;
            setTimeout(function() {
                loop();
            }, 5000);
        } else if (songStorage.current != result && songStorage.current != undefined) {
            //Song has changed.
            songStorage.previous = songStorage.current;
            app.emit('trayUpdate', dJSON.parse(result), false);
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(songStorage.previous).duration)) / thresold) {
                //User has listened for more than half of the whole song.
                scrobblePrevious(dJSON.parse(songStorage.previous));
            }
            songStorage.time = 5; //Reset timer. New song playing.
            songStorage.ready = false;
            setTimeout(function() {
                loop();
            }, 5000);
        } else {
            songStorage.time += 5; //Add 5 seconds to timer
            console.log(songStorage.time + "/" + Math.round(parseInt(dJSON.parse(result).duration)))
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(result).duration)) / thresold && songStorage.ready == false) {
                //Song is ready to be scrobbled.
                console.log("Song ready to scrobble");
                updateCurrentPlaying(dJSON.parse(result));
                app.emit('trayUpdate', dJSON.parse(result), true);
                songStorage.ready = true;
            } else if (songStorage.time >= Math.round(parseInt(dJSON.parse(result).duration))) {
                console.log("Loop mode - scrobble");
                scrobblePrevious(dJSON.parse(result));
                app.emit("trayUpdate", dJSON.parse(result), false);
                songStorage.time = 5;
                songStorage.ready = false;
            }
            setTimeout(function() {
                loop();
            }, 5000);
        }
        songStorage.current = result;
    }).catch(error => {
        //Player is paused or stopped.
        console.log(error);
        apple.getPlayerState().then(state => {
            console.log(state);
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(songStorage.current).duration)) / thresold && state == "Stopped") {
                //Player is not playing anything nor is paused (EMPTY PLAYER, State: Stopped.).
                scrobblePrevious(dJSON.parse(songStorage.current));
                songStorage.time = 5;
                songStorage.ready = false;
            }
            setTimeout(function() {
                loop();
            }, 5000);
        }).catch(err => {
            console.log(err);
            setTimeout(function() {
                loop();
            }, 5000);
        });
        if (!typeof songStorage.current === undefined) {
            apr.emit('trayUpdate', false, false);
        }
    });
}
/*
Main loop - updates data.
*/
Main loop - updates data.
Main loop - updates data .
function loop() {
    getCurrentPlaying().then(result => {
        updateCurrentPlaying(dJSON.parse(result));
        if (songStorage.current === undefined) {
            console.log("Running for the first time.")
            app.emit('trayUpdate', dJSON.parse(result), false)
            songStorage.time = 5;
            songStorage.ready = false;
            setTimeout(function() {
                loop();
            }, 5000);
        } else if (songStorage.current != result && songStorage.current != undefined) {
            songStorage.previous = songStorage.current;
            app.emit('trayUpdate', dJSON.parse(result), false);
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(songStorage.previous).duration)) / thresold) {
                scrobblePrevious(dJSON.parse(songStorage.previous));
            }
            songStorage.time = 5; 
            songStorage.ready = false;
            setTimeout(function() {
                loop();
            }, 5000);
        } else {
            songStorage.time += 5; 
            console.log(songStorage.time + "/" + Math.round(parseInt(dJSON.parse(result).duration)))
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(result).duration)) / thresold && songStorage.ready == false) {
                console.log("Song ready to scrobble");
                updateCurrentPlaying(dJSON.parse(result));
                app.emit('trayUpdate', dJSON.parse(result), true);
                songStorage.ready = true;
            } else if (songStorage.time >= Math.round(parseInt(dJSON.parse(result).duration))) {
                console.log("Loop mode - scrobble");
                scrobblePrevious(dJSON.parse(result));
                app.emit("trayUpdate", dJSON.parse(result), false);
                songStorage.time = 5;
                songStorage.ready = false;
            }
            setTimeout(function() {
                loop();
            }, 5000);
        }
        songStorage.current = result;
    }).catch(error => {
        console.log(error);
        apple.getPlayerState().then(state => {
            console.log(state);
            if (songStorage.time >= Math.round(parseInt(dJSON.parse(songStorage.current).duration)) / thresold && state == "Stopped") {
                scrobblePrevious(dJSON.parse(songStorage.current));
                songStorage.time = 5;
                songStorage.ready = false;
            }
            setTimeout(function() {
                loop();
            }, 5000);
        }).catch(err => {
            console.log(err);
            setTimeout(function() {
                loop();
            }, 5000);
        });
        if (!typeof songStorage.current === undefined) {
            apr.emit('trayUpdate', false, false);
        }
    });
}
function loop ( ) { getCurrentPlaying ( ) . then ( result => { updateCurrentPlaying ( dJSON . parse ( result ) ) ; if ( songStorage . current === undefined ) { console . log ( "Running for the first time." ) app . emit ( 'trayUpdate' , dJSON . parse ( result ) , false ) songStorage . time = 5 ; songStorage . ready = false ; setTimeout ( function ( ) { loop ( ) ; } , 5000 ) ; } else if ( songStorage . current != result && songStorage . current != undefined ) { songStorage . previous = songStorage . current ; app . emit ( 'trayUpdate' , dJSON . parse ( result ) , false ) ; if ( songStorage . time >= Math . round ( parseInt ( dJSON . parse ( songStorage . previous ) . duration ) ) / thresold ) { scrobblePrevious ( dJSON . parse ( songStorage . previous ) ) ; } songStorage . time = 5 ; songStorage . ready = false ; setTimeout ( function ( ) { loop ( ) ; } , 5000 ) ; } else { songStorage . time += 5 ; console . log ( songStorage . time + "/" + Math . round ( parseInt ( dJSON . parse ( result ) . duration ) ) ) if ( songStorage . time >= Math . round ( parseInt ( dJSON . parse ( result ) . duration ) ) / thresold && songStorage . ready == false ) { console . log ( "Song ready to scrobble" ) ; updateCurrentPlaying ( dJSON . parse ( result ) ) ; app . emit ( 'trayUpdate' , dJSON . parse ( result ) , true ) ; songStorage . ready = true ; } else if ( songStorage . time >= Math . round ( parseInt ( dJSON . parse ( result ) . duration ) ) ) { console . log ( "Loop mode - scrobble" ) ; scrobblePrevious ( dJSON . parse ( result ) ) ; app . emit ( "trayUpdate" , dJSON . parse ( result ) , false ) ; songStorage . time = 5 ; songStorage . ready = false ; } setTimeout ( function ( ) { loop ( ) ; } , 5000 ) ; } songStorage . current = result ; } ) . catch ( error => { console . log ( error ) ; apple . getPlayerState ( ) . then ( state => { console . log ( state ) ; if ( songStorage . time >= Math . round ( parseInt ( dJSON . parse ( songStorage . current ) . duration ) ) / thresold && state == "Stopped" ) { scrobblePrevious ( dJSON . parse ( songStorage . current ) ) ; songStorage . time = 5 ; songStorage . ready = false ; } setTimeout ( function ( ) { loop ( ) ; } , 5000 ) ; } ) . catch ( err => { console . log ( err ) ; setTimeout ( function ( ) { loop ( ) ; } , 5000 ) ; } ) ; if ( ! typeof songStorage . current === undefined ) { apr . emit ( 'trayUpdate' , false , false ) ; } } ) ; }
Main loop - updates data.
Main loop - updates data .
//Player is running for the first time. //Song has changed. //User has listened for more than half of the whole song. //Reset timer. New song playing. //Add 5 seconds to timer //Song is ready to be scrobbled. //Player is paused or stopped. //Player is not playing anything nor is paused (EMPTY PLAYER, State: Stopped.).