Fixing the ListenBrainz Jellyfin Plugin to work with multiple artists.
Hi everyone,
This week, I installed the ListenBrainz and Last.FM plugins to my local Jellyfin server, where I have my Music CDs ripped and listen to them using the Finamp Android app.
It seems that the Last.FM plugin is working well, but I noticed that the ListenBrainz plugin had a problem. Whenever I listened to songs with multiple artists, those would not be submitted nor would be shown as “Now Playing”. So, I took some time to find out the problem.
My music is mostly stored in Monkey’s Audio .ape files, as these compresses better than FLAC. I thought it may have been a problem with APEv2 tags. After investigating on Github, there was an issue for a similar problem, which gave me a starting point.
Once I played with Mutagen on Python, I thought the problem was the separator. APEv2 files seems to use \x00 NULL character for the separator. To my surprise, this was not the case, as the C# plugin code was parsing the artists with a comma and a space.
So, I modified the AudioExtensions.cs file to split the artists MBIDs and then perform a trim. The resulting code is below.
Basically, the line 56 changed from:
ArtistMbids = item.ProviderIds.GetValueOrDefault("MusicBrainzArtist")?.Split(';', '/'),
to:
ArtistMbids = item.ProviderIds.GetValueOrDefault("MusicBrainzArtist")?.Split(';', '/', ',').Select(s => s.Trim()).ToArray(),
This makes sure that the artist_mbids
field is splitted by a comma, and then trims the elements to remove the whitespace after it.
Originally, this plugin would fail to submit the listen to ListenBrainz due to a BadRequest error:
[2024-09-21 14:04:49.618 +00:00] [DBG] [20] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Response status is BadRequest, will not retry
[2024-09-21 14:04:49.619 +00:00] [DBG] [20] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Got response:
Data: "{\"code\":400,\"error\":\"artist_mbids MBID format invalid.\",\"track_metadata\":{\"additional_info\":{\"artist_mbids\":[\"17f0182e-146a-4115-8c83-aa291b4e3acc, bb53bece-e3ed-4a9e-9348-265745dcb239\"],\"duration_ms\":205000,\"isrc\":\"USAT29700167\",\"media_player\":\"Jellyfin\",\"recording_mbid\":\"c5a61a3a-46e4-4c7b-b511-b9a67912b86d\",\"release_group_mbid\":\"2c5b5cf7-d6fc-32e9-b68f-8e8fb8747aaa\",\"release_mbid\":\"9a7768c5-7a5a-4a80-9f3e-0a86ec174c8c\",\"submission_client\":\"ListenBrainz plugin for Jellyfin\",\"submission_client_version\":\"1.0.0.0\",\"tags\":[],\"track_mbid\":\"8f4f0b0e-3729-398a-a2bd-1ad76021a07c\",\"tracknumber\":1},\"artist_name\":\"Jonathan Dokuchitz, Kelsey Grammer and Townspeople\",\"release_name\":\"Anastasia\",\"track_name\":\"A Rumor in St. Petersburg\"}}
[2024-09-21 14:04:49.619 +00:00] [DBG] [20] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Did not hit any rate limits, all OK
[2024-09-21 14:04:49.619 +00:00] [DBG] [20] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Request has been processed, freeing up resources
[2024-09-21 14:04:49.619 +00:00] [INF] [23] Jellyfin.Plugin.ListenBrainz: Failed to send 'playing now' listen: "Sending now playing failed"
With the comma splitting added, it failed due to the whitespace:
[2024-09-21 14:17:44.825 +00:00] [DBG] [16] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Response status is BadRequest, will not retry
[2024-09-21 14:17:44.825 +00:00] [DBG] [16] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Got response:
Data: "{\"code\":400,\"error\":\"artist_mbids MBID format invalid.\",\"track_metadata\":{\"additional_info\":{\"artist_mbids\":[\"17f0182e-146a-4115-8c83-aa291b4e3acc\",\" bb53bece-e3ed-4a9e-9348-265745dcb239\"],\"duration_ms\":168000,\"isrc\":\"USAT29700169\",\"media_player\":\"Jellyfin\",\"recording_mbid\":\"e8330f3b-aff9-48f1-912e-156de9e12ca7\",\"release_group_mbid\":\"2c5b5cf7-d6fc-32e9-b68f-8e8fb8747aaa\",\"release_mbid\":\"9a7768c5-7a5a-4a80-9f3e-0a86ec174c8c\",\"submission_client\":\"ListenBrainz plugin for Jellyfin\",\"submission_client_version\":\"1.0.0.0\",\"tags\":[],\"track_mbid\":\"7208f8b5-3541-3a39-b220-67e9757aac11\",\"tracknumber\":3},\"artist_name\":\"Liz Callaway\",\"release_name\":\"Anastasia\",\"track_name\":\"Once Upon a December\"}}
[2024-09-21 14:17:44.827 +00:00] [DBG] [16] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Did not hit any rate limits, all OK
[2024-09-21 14:17:44.828 +00:00] [DBG] [16] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Request has been processed, freeing up resources
[2024-09-21 14:17:44.849 +00:00] [INF] [21] Jellyfin.Plugin.ListenBrainz: Failed to send 'playing now' listen: "Sending now playing failed"
[2024-09-21 14:17:44.849 +00:00] [DBG] [21] Jellyfin.Plugin.ListenBrainz: Failed to send 'playing now' listen
Now, after trimming, it sent successfully:
[2024-09-21 14:31:18.480 +00:00] [DBG] [25] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Sending request:
Data: "{\"listen_type\":\"playing_now\",\"payload\":[{\"track_metadata\":{\"artist_name\":\"Kelsey Grammer, Liz Callaway and Jonathan Dokuchitz\",\"track_name\":\"Learn to Do It\",\"release_name\":\"Anastasia\",\"additional_info\":{\"artist_mbids\":[\"17f0182e-146a-4115-8c83-aa291b4e3acc\",\"bb53bece-e3ed-4a9e-9348-265745dcb239\"],\"release_group_mbid\":\"2c5b5cf7-d6fc-32e9-b68f-8e8fb8747aaa\",\"release_mbid\":\"9a7768c5-7a5a-4a80-9f3e-0a86ec174c8c\",\"recording_mbid\":\"59542aed-9689-4e29-8ee8-70db2c18b2b9\",\"track_mbid\":\"a5762a61-6b06-3a94-9f1a-c7180747df10\",\"tracknumber\":5,\"isrc\":\"USAT29700182\",\"tags\":[],\"media_player\":\"Jellyfin\",\"submission_client\":\"ListenBrainz plugin for Jellyfin\",\"submission_client_version\":\"1.0.0.0\",\"duration_ms\":156000}}}]}"
[2024-09-21 14:31:18.482 +00:00] [INF] [23] Jellyfin.Plugin.ListenBrainz: Attempting to sync favorite status for track "In the Dark of the Night" and user "moisespr123"
[2024-09-21 14:31:18.482 +00:00] [INF] [23] Jellyfin.Plugin.ListenBrainz: Recording MBID is available, using it for favorite sync
[2024-09-21 14:31:18.487 +00:00] [DBG] [23] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Waiting for previous request to complete (if any)
[2024-09-21 14:31:18.642 +00:00] [DBG] [25] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Response status is OK, will not retry
[2024-09-21 14:31:18.642 +00:00] [DBG] [25] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Got response:
[2024-09-21 14:31:18.642 +00:00] [DBG] [25] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Did not hit any rate limits, all OK
[2024-09-21 14:31:18.642 +00:00] [DBG] [25] Jellyfin.Plugin.ListenBrainz.ListenBrainzApi: Request has been processed, freeing up resources
[2024-09-21 14:31:18.642 +00:00] [INF] [21] Jellyfin.Plugin.ListenBrainz: Successfully sent 'playing now' listen
I made a Pull Request on GitHub and hope this is merged soon, as this will help others having similar issues.
You can check out my Last.FM and ListenBrainz profiles below: