Friday, 10 August 2018

Simple PHP Code To Get last.fm Last Played Tracks

The other day I was approached by a friend (Julie Cheung) and asked if I could create some code that would display a list of last played tracks from last.fm. Julie isn't a PHP developer and so the code I gave her had to be easily understandable so that she could edit it herself if needed. The following code is what I came up with.
  1. $username = 'philipnorton42';
  2. $scrobbler_url = "http://ws.audioscrobbler.com/2.0/user/" . $username . "/recenttracks";
  3.  
  4. if ($scrobbler_xml = file_get_contents($scrobbler_url)) {
  5. $scrobbler_data = simplexml_load_string($scrobbler_xml);
  6.  
  7. echo '
    '; foreach ($scrobbler_data->track as $track) { $string = '
  • '; $string .= '
    '; $string .= '
    ' . $track->artist . '
    ' . $track->name . '
    '; $string .= '
    Played: ' . $track->date . '
    '; $string .= '
  • '; echo $string; } echo '
'; }
What this code does is to grab the XML feed containing the latest played songs from last.fm and convert them into a set of list elements containing some information about the tracks. This is done very simply by using the simple_xml_load_string() function, which works quite well for most situations and in this case only destroys a small amount of information that isn't really needed. Each track contains information about the artist and track that was played, including album artwork. Here is a breakdown of the information available in the feed, with the latest song in my own playlist as an example.
  • artist : The name of the artist (e.g. "The Black Dahlia Murder").
  • name : This is the name of the track (e.g. "Deathmask Divine").
  • streamable : This is a boolean value (as a 1 or a 0) that states if the track can be streamed.
  • mbid : The mbid is an ID of the song, but is lost when the XML feed is passed through the simplexml_load_string() function. This doesn't matter, however, as we probably won't need it when printing out the results.
  • album : This is the name of the album that the track belongs to (e.g. "Nocturnal").
  • url : This is a URL that links back to the track information on the last.fm site. For the example I gave above this is http://www.last.fm/music/The+Black+Dahlia+Murder/_/Deathmask+Divine.
  • image : This is an array if different images for the album cover. Although the sizes are lost in the translation from XML the array translates to the following sizes:
    • 0 : Small image size, 34px by 34px.
    • 1 : Medium image size, 64px by 64px.
    • 2 : Large image size, 128px by 128px.
    • 3 : Extra large image size, 300px by 300px.
    You can see in the example above that I have selected the small image size in the array by using $track->image[0].
  • date : This is the date and time that the track was played on (e.g. "21 Nov 2011, 15:28").
One addition to this code would be to stop it pulling in the entire feed on every page load, which isn't really needed. The addition of a small amount of caching code is ideal to keep page load times down (especially as we are relying on a third party service). We only need to store the data in a separate file for about 3 minutes or so (the average playtime of a song) so this time check is also added to the code.
  1. <?php $username = 'philipnorton42';
  2. $scrobbler_url = "http://ws.audioscrobbler.com/2.0/user/" . $username . "/recenttracks";
  3.  
  4. $scrobbler_cache_file = 'scrobbler_' . $username . '_data.cache';
  5.  
  6. if (file_exists($scrobbler_cache_file)) {
  7. if (time() - filemtime($scrobbler_cache_file) ?> 180) {
  8. // if the file was created more than 3 minutes ago then delete.
  9. unlink($scrobbler_cache_file);
  10. } else {
  11. $scrobbler_url = realpath('./' . $scrobbler_cache_file);
  12. }
  13. }
  14.  
  15. if ($scrobbler_xml = file_get_contents($scrobbler_url)) {
  16. $scrobbler_data = simplexml_load_string($scrobbler_xml);
  17.  
  18. if (!file_exists($scrobbler_cache_file)) {
  19. file_put_contents($scrobbler_cache_file, $scrobbler_xml);
  20. }
  21.  
  22. echo '
    '; foreach ($scrobbler_data->track as $track) { $string = '
  • '; $string .= '
    '; $string .= '
    ' . $track->artist . '
    ' . $track->name . '
    '; $string .= '
    Played: ' . $track->date . '
    '; $string .= '
  • '; echo $string; } echo '
'; }

0 comments:

Post a Comment