This is my first post, so I needed some data to play with. I’ve been wanting to learn more about APIs so tackling the Spotify API seemed like a great place to start. I soon came across the related artists function in the API and that gave me a great idea. What if you could map out and visualise how your favourite artists relate to each other according to Spotify. It could be a useful way to discover new similar artists. A visual recommendation engine.
I needed some way to systematically access the API. I’ve recently been learning to code in R. So that seemed like a sensible place to start. After some initial success on a PC using the readLines() function I discovered some issues with this on a Mac. With the help of a colleague I learnt on a Mac you need to use the download.file() function initially and then apply readLines to the downloaded file. Additionally to get the download.file function to work you need to install “wget” and apply the “wget” method in the function. For help on installing wget firstly install homebrew (help from my colleague here). Then in the terminal input brew install wget.
Once I had that working I started to build a SpotifyAPI package for R. I felt the Spotify API could be a useful resource for this blog so having a object orientated package full of the functions I create would save me time in the long run and would be enjoyable to create.
You can find and clone my SpotifyAPI package from my github here. It currently contains three functions.
The first; getArtists, accesses the Spotify API and pulls the best match for artist searches and returns their unique spotify id for use in later functions and some basic info on them. This relies on the XML package on CRAN. So make sure you have that installed.
The second; getRelatedArtists, takes the output from the getArtists function and pulls from the Spotify API twenty related artists and stores some basic information on them. This relies on the rjson package. So again make sure you have that installed from CRAN.
Finally the fun visualisation bit. visRelatedArtists uses both the previous functions and churns out an html file containing a force directed layout d3.js visualisation. For the visualisation I took the basic structure from here. Then started to customise taking bits from this blog on the A-Z of extra features for the Force Directed Layout. The function also requires you to decide how many steps away you want to go. One step takes the search artist in this case ‘Shuggie Otis’ and finds his related artists and creates the layout below. As you can see the size of the node relates to the popularity of the artist according to Spotify and the distance between nodes relates to the position in the related artists list; the related artist in the first position will be closer.
If you go a second step you find all the related artists to those related artists and so on. This will be around 400 artists. The colours show the step at which the artist was added.
A third step and things get a bit noisy with up to 8000 artists displayed. Its probably a bit too much
You can also throw in more than one initial artist. This is a one step result for a couple of artists I’ve recently discovered ‘Hurray for the Riff Raff’ and ‘Lady Lamb the Beekeeper’. Although there are no direct links there are a couple of linking artists I will definitely check out.
Finally I ran one version with a number of my favourite artists in to see how they all connect and if there are any new recommendations I should have a listen to.
Thanks for reading. As I mentioned you can access it from my git hub here and map out your own favourite artists and get some visual recommendations.