使用user.getRecentTracks,尽管我将限制设置为1,Last.fm仍会发送2条轨道。由于一些聪明的人,如果我当前未在听曲目,它只会发送一条轨道。

例如:http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=yeppaul&api_key=b25b959554ed76058ac220b7b2e0a026&limit=1

<lfm status="ok">
<recenttracks user="yeppaul" page="1" perPage="1" totalPages="1474" total="1474">
<track nowplaying="true">
<artist>
<name>The Offspring</name>
<mbid>23a03e33-a603-404e-bcbf-2c00159d7067</mbid>
<url>The Offspring</url>
<image size="small">http://userserve-ak.last.fm/serve/34/319822.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/319822.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/319822.jpg</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/319822.jpg</image>
</artist>
<loved>0</loved>
<name>Original Prankster</name>
<streamable>1</streamable>
<mbid>07933833-f6df-45eb-80ab-870e1d19b151</mbid>
<album mbid="05a93768-e317-338a-882e-fbe83790d8cd">Conspiracy of One</album>
<url>
http://www.last.fm/music/The+Offspring/_/Original+Prankster
</url>
<image size="small">http://userserve-ak.last.fm/serve/34s/47828469.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64s/47828469.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/47828469.png</image>
<image size="extralarge">
http://userserve-ak.last.fm/serve/300x300/47828469.png
</image>
</track>
<track>
<artist>
<name>The Offspring</name>
<mbid>23a03e33-a603-404e-bcbf-2c00159d7067</mbid>
<url>The Offspring</url>
<image size="small">http://userserve-ak.last.fm/serve/34/319822.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/319822.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/319822.jpg</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/319822.jpg</image>
</artist>
<loved>0</loved>
<name>Smash</name>
<streamable>0</streamable>
<mbid>1c6f6ba6-ea2e-4dcf-bef5-20f29d70611d</mbid>
<album mbid="021b8436-4c5f-3aa0-ac08-3ebf6470ada3">Smash</album>
<url>http://www.last.fm/music/The+Offspring/_/Smash</url>
<image size="small">http://userserve-ak.last.fm/serve/34s/60481175.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64s/60481175.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/60481175.jpg</image>
<image size="extralarge">
http://userserve-ak.last.fm/serve/300x300/60481175.jpg
</image>
<date uts="1347066244">8 Sep 2012, 01:04</date>
</track>
</recenttracks>
</lfm>


这是我的jQuery:

$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=yeppaul&api_key=61d9a12fccfc085e5ddc70beccdbeaae&limit=5",
        dataType: "xml",
        success: showListens
    });
});

function showListens(xml) {
    $(xml).find("track").each(function()
    {
        $(".listen").append("<a href=" + $(this).find("url").text() + ">" + $(this).find("artist").text() + " &mdash; " + $(this).find("name").text() + "</a><br /><br />");
    });
}

最佳答案

由于正在监听的track被标记为属性nowplaying="true",因此可以使用not([nowplaying])过滤这些音轨:

$(xml).find("track:not([nowplaying])").each(function()...

10-04 20:13