I am quite pleased with my app that uses the Spotify Web API for creating data visualizations using the popularity data of artists, albums, and tracks. That popularity score, however, is a relative number so I can’t truly compare much in a useful way, however pretty my UX is.
Now is the time on Sprockets where we start a new project using the Last.fm API to get real, quantifiable data — the number of listeners for each entity (artist, album, track) and playcount for each. I’m very interested to find how those numbers differ.
Spotify and the Spotify Web API make it very easy to get ID numbers for each artist, album IDs for each artist and track IDs for each album. In fact, the documentation shows they might even be making an effort to be stupid. The example URL in their documentation for getting info about an artist is:
So any spelling errors or similar names will make your life miserable. Fortunately, their database uses (for the most part–I’ll get to that in a moment) the mbid or MusicBrainz ID for everything so you can, instead, write the above like this:
Unfortunately, there’s no Last.fm equivalent to Spotify‘s utterly delightful endpoint for getting all albums by an artist:
Fortunately, the MusicBrainz API has such an animal but it’s a little more complicated and it took me a bit of trial and error to figure out quite how it works. I’d like to share it with you. Here are the steps I’ll need to complete to get all of the information about an artist, their albums, and the tracks from those albums with an extra curveball just to make it fun.
Step #1 – Search MusicBrainz.org for the desired artist
In this case, I chose Alice Cooper.
For the uninititated, not only would using the search term “alice cooper” bring up tribute bands but the fans (I count myself among them) who insist on differentiating between the group and the solo artist mean I have to use not one but two MBID numbers for this single artist.
Step #2 – Get the Artist’s MBID from MusicBrainz
Click the artist name in the search results then click the Details tab on the artist page.
Step #3 – Get the Artist’s release-groups from MusicBrainz
Copy the artist MBID and paste it into a MusicBrainz web service URL using the artist entity and a ?inc=release-groups parameter like so:
to get a list of “release groups.”
I broke up with Dreamweaver a while back, dumping it for the far smarter, cuter, cooler VS Code but ol’ DW will always have a special place in my heart because of cool commands like Edit > Code > Apply Source Formatting which does this:
Remember, you have to do that twice for Alice Cooper. Because we, his fans, are pricks like that.
Step #4 – Get the Artist’s Releases from MusicBrainz
A release is an album — like Zipper Catches Skin or Billion Dollar Babies. A release group is all editions and versions of that album. To see exactly what that means, we copy the id for the Constrictor release-group from line 26 in the above image and construct the URL below using the release-group entity and inc=releases parameter.
That gets us all six releases in the Constrictor release-group:
- United States with “packaging”: “Cardboard/Paper Sleeve” (a vinyl record)
- United States with “packaging”: “Jewel Case” (a CD)
- Japan CD
- United States with “packaging”: “” (digital?)
- Germany with “packaging”: null
- United Kingdom with “packaging”: null
I don’t know what null packaging means.
The first time I get data from Last.fm, I’ll try all of these and hypothesize “Cardboard/Paper Sleeve” will have a zero playcount.
Step #5 – Get a Release’s Recordings from MusicBrainz
A recording is a “track.”
Construct a URL using the individual release entity and inc=recordings parameter.
The UK release of Constrictor has 10 tracks. There will probably never be a deluxe or remastered edition. Which is a damn shame.
Step #6 – Get an Artist’s data from Last.fm
Using the artist MBID, method=artist.getinfo, and an API Key as parameters, assemble a Last.fm URL like so:
The resulting JSON for Alice Cooper the man includes a bio, birthday, similar artists, genres, and–for me–the important bit:
You’re dying to know how that compares to Alice Cooper the group, amiright?
Well, check that shizzle out! We only need to run what will be this query once for Alice Cooper! Both MBIDs get the exact same data. Wanna know something nerdy and cool?
The artist entity URLs I used earlier to get the release groups differ …
- Alice Cooper the person has a gender of “male.” Alice Cooper the band has a gender of “null.”
- The person has a “begin_area” of Detroit, MI while the band has Pheonix, AZ
- The band has a life-span of 1964-1975 with “ended”: true while the person’s life-span begins with “1948-02-04” and ends with “null” and “ended” is false.
- Type properties are “group” and “person”
Step #7 – Get Album Info from Last.fm
We’ll go with Constrictor again. Using album.getinfo this time.
- listeners: 64,771
- playcount: 545,070
For comparison (the entire purpose of this whole exercise), let’s also get Billion Dollar Babies:
Well, I’ll be horn-swaggled! There were three Billion Dollar Babies release-groups in my JSON file but none of their IDs worked so I searched at MusicBrainz and got the actual mbid for the release-group which had 13 releases in it! How do you pick? Eventually, I’ll get all of them but …
First choice didn’t work … this is going to get tedious …
Okay, second try gets me some stats
- listeners: 169,768
- playcount: 1,222,112
Yeah, see that’s a significant difference.
Step #8 – Get Track Info from Last.fm
I went for “The World Needs Guts” from Constrictor using the track.getInfo method as a parameter.
- listeners: 11,332
- playcount: 38,656
Which I’m sure is tiny crumbs compared to even one instance of “School’s Out” let alone adding all of the recordings of “School’s Out” together.
It will take me a lot longer to get the data from Last.fm that I need than it does to get data from Spotify but it will also be data I can use!