user4606459
user4606459

Reputation:

How to add last.fm now playing into my site using Api

Hi guys i been trying to figure out for a long time but i suck at this, i found this code on google and i added it adn changed what i need but still doesnt work i really need this for my site: http://www.balkan-party.cf/

I found code here: http://www.samkitson.co.uk/using-json-to-access-last-fm-recently-played-tracks/

My last.fm username i need in js: alicajdin AND Api key: 24f6b03517ad9984de417be5d10e150b

This is what i did:

$(document).ready(function() {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=alicajdin&api_key=24f6b03517ad9984de417be5d10e150b&limit=2&format=json&callback=?", function(data) {
        var html = ''; // we declare the variable that we'll be using to store our information
        var counter = 1; // we declare a counter variable to use with the if statement in order to limit the result to 1
        $.each(data.recenttracks.track, function(i, item) {
            if(counter == 1) {
                html += 'Currently listening to: <span><a href="' + item.url + '" target="_blank">' + item.name + '</a> - ' + item.artist['#text'] + '</span>';
            } // close the if statement
            counter++ // add 1 to the counter variable each time the each loop runs
        }); // close each loop
        $('.listening-to h5').append(html); // print the information to the document - here I look for the h5 tag inside the div with a class of 'listening-to' and use the jQuery append method to insert the information we've stored in the html variable inside the h5 tag.
    }); // close JSON call
});

I created that file and i tried to add on head section, footer section but it wont show recent tracks. And yea i have scroblr installed on google crome

Upvotes: 1

Views: 1838

Answers (1)

busainet
busainet

Reputation: 21

below </script> add the following code:

<div class="listening-to"></div>

then remove "h5" on

"$('.listening-to h5').append(html);"

so your code like this:

<script type="text/javascript">
    $(document).ready(function() {
        $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=YOUR_USERNAME&api_key=YOUR_API_KEY&limit=2&format=json&callback=?", function(data) {
            var html = '';
            var counter = 1;
            $.each(data.recenttracks.track, function(i, item) {
                if(counter == 1) {
                    html += 'Currently listening to: <span><a href="' + item.url + '" target="_blank">' + item.name + '</a> - ' + item.artist['#text'] + '</span>';
                }
                counter++
            });
            $('.listening-to').append(html);
        });
    });
</script>
<div class="listening-to"></div>

Hope you can help. Sorry, my English is very Bad (Google Translate)

Upvotes: 2

Related Questions