Sunday, December 2, 2012

How to find more artists on Last.fm

When you've used Last.fm for a while you'll no doubt have found some friends, people with similar tastes or interesting genres (tags). Some of the music inside those libraries might be really good, but if you want to find the ones that you don't know yet it's a lot of work to find those.

In this post I'm going to show you how to make list of artists including the  information if you've played them before and include links to listen to the artists. For this you'll need access to a web server that works with PHP, that can be your hosting provider or your own local WAMP-server. Make sure it supports PHP 5+ since we'll be using the function simplexml_load_file. I'll make a separate post how to install a local server (don't worry, it's really easy).

Luckily there are a couple of methods in the Last.fm API that can help with finding artists and their information. Note that you'll need your own free API key for this (you can get one here). The methods that I'm using here don't require authentication so it'll be a lot easier getting the info.
  • user.getTopArtists - This methods fetches the list of artists that somebody has been playing. You can get a list for different time periods (including overall) and set the number of results per page. This is convenient for restricting the number of things the script has to check, if it's too much then it'll take too long to finish and the server will timeout. Setting it to a maximum of 25 works best for standard servers.
  • tag.getTopArtists - Almost the same as the previous method but without the time periods and with a limit of 1000 artists.
  • artist.getInfo - This one does most of the heavy lifting: for each of the artists that are listed by the previous two methods it checks it for their information and (since we also add a username) how many times we've played if.
There are more methods that provide lists of artists based on charts, location or groups but for now we'll only use these.