Getting Into ListenBrainz
Last week, while reading about Libre.fm, an open source alternative to Last.fm, I (re)discovered ListenBrainz, a similar project by MusicBrainz, which is an online database for music releases. I know they’re a lot of names but, in short, I was fascinated by ListenBrainz, and I decided to create a profile to have a history of what music I listen to.
Until May 2020, I had a Last.fm account with thousands of scrobbles1, but I rarely used since 2010 or so. Also, the website had changed a lot from the first years, and it did not make sense to me to keep my data there. So, one day… I deleted the profile, but I didn’t download my stats because I thought they’d be useless to me.
In fact, my music tastes changed a bit, and too many years have passed with no scrobbles. I thought that a fresh new start would be good, since I love listening to music at every hour of the day. In the past years, I had already noticed ListenBrainz (LB from now on), but didn’t give it too much attention. Last week, however, I finally signed up.
The Horror
Only after I had logged in, I found out that there was no way to scrobble data from Apple Music, the app that I use for the homonymous subscription! On LB there are supported plug-ins for Windows and Linux, for other media player; there’s also the option to actually use your Apple Music account to listen the songs through their website… but if I listened to a song from my Mac, that didn’t appear not listed on my profile. I looked in every corner of several search engines, but I couldn’t find anything.
Eventually, without thinking too much, I started an experiment: a Bash script to send my scrobbles.
Years ago, I had already found a very good resource for AppleScripts, and there were some for the old iTunes2 too.
They work perfectly also for Music.app, so in a few minutes I was able to get the playing track.
Luckily, AppleScript scripts can be called by Bash using the osascript
command and, with some help from StackOverflow, I was able to use this data to build a JSON file.
Then, using curl
, the info is easily sent to the LB API, and the song appears on the profile!.
I added a few checks to manage various cases that can happen (e.g., the music is paused, the app is not open, the same song has been already scrobbled), and in a couple of days I think I’ve built a quite stable script.
While working on it, I learned some new flags for echo
and grep
(I guess they’re infinite), and the Bash’s trap
, without which I don’t know how I could live before… I’m pretty satisfied!
Here it is, in case you want to try it: ListenBrainz macOS Scrobbler for Music.app.
I’ve also found a way to incorporate it in an actual “app” using a workaround, but I prefer to run it manually:
$ ./scrobbler.sh
Music not playing. Waiting.
Scrobbling Foo Fighters - Wind Up (The Colour And The Shape)
Sleeping 30 seconds.
Same song as before.
Same song as before.
The Joy
I posted on Mastodon that I was working on the project, and I tagged MetaBrainz’s profile. I’m glad I immediately gained their attention, and I hope that someone will find it useful.
Maybe this script will be handy for 0.01% of their user base, but for me, it will be more than enough. I’ve always wanted to contribute to some open source projects, but I know only a little of the modern programming languages.
In the future, it would be nice to write a proper macOS app… we’ll see!
🎮 No time for that!
📖 Still on NOS4A2
Last week, while reading about Libre.fm, an open source alternative to Last.fm, I (re)discovered ListenBrainz, a similar project by MusicBrainz, which is an online database for music releases. I know they’re a lot of names but, in short, I was fascinated by ListenBrainz, and I decided to create a profile to have a history of what music I listen to.
Until May 2020, I had a Last.fm account with thousands of scrobbles1, but I rarely used since 2010 or so. Also, the website had changed a lot from the first years, and it did not make sense to me to keep my data there. So, one day… I deleted the profile, but I didn’t download my stats because I thought they’d be useless to me.