Currently Playing on Last.fm JavaScript Widget

Playing around with jQuery and Last.fm’s JavaScript API.

Published: | Tags: , , , and

This is a simple JavaScript script that access Last.fm's JSON API and get's the current track, artist, and album image.

function getLatestTrack(user, apiKey) {
    'use strict';

    var url = "http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=" + user + "&api_key=" + apiKey + "&format=json";

    $.getJSON(url, function (data) {
        var track = data.recenttracks.track[0],
            albumImageURL;

        $(".track").html(track.name);
        $(".artist").html(track.artist['#text']);

        albumImageURL = track.image[3]['#text'];

        $("img").attr({
            'src': albumImageURL,
            'title': track.name + " / " + track.artist['#text']
        });
    });
}

$(document).ready(function () {
    'use strict';

    var user = "mylesb",
        apiKey = "";

    getLatestTrack(user, apiKey);

    setInterval(function () {
        getLatestTrack(user, apiKey);
    }, 210000);
});
Demo (Open in New Window).