[resolved] How to play recommended mbids?

I’m using troi to get a list of recommended mbids for a user. Is there any already implemented way to have spotify or youtube playlist of these recommended songs ?

I’m trying to generate a playlist with the listenbrainz api but I’m struggling to get meaningful response to know why jspf playlist is not understood. '{"code":400,"error":"The browser (or proxy) sent a request that this server could not understand."}'Is there some python code used somewhere to generate these jspf playlists followinf listenbrainz specification ?

Thx ! :slight_smile:

1 Like

I just found https://listenbrainz.org/player/ listenbrainz-server/player.py at 28d2ac31c311a906fb9f5d5ec2966e69430fd79d · metabrainz/listenbrainz-server · GitHub but it’s not easy to implement wih python

Maybe that solves your JSPF question (in javascript)

About getting a playlist, it can be done. Problem is there is no current exposed API to provide youtube links, so even if you manage to get a JSPF playlist, content resolution by Artist, title and MBID must be done outside listenbrainz. See this:

That’s said, if a solution is given, I’m also interested :slight_smile:

Btw, if you are asking how a JSPF playlist looks:
https://api.listenbrainz.org/1/playlist/8abf5d92-f4c2-41d1-9378-6380b5703616

So you should be able to replicate it yourself, there was a JSPF library somewhere.
(the repository at top has a JSPF/XSPF library in javascript)

I generated the jspf playlist :

{'extension': {'https://musicbrainz.org/doc/jspf#playlist': {'creator': 'me'}},
  'title': 'test',
  'tracks': [
   {'identifier': 'https://musicbrainz.org/recording/2b3cab04-1f35-423e-bb56-cfc9f31738cf',
    'extension': {'https://musicbrainz.org/doc/jspf#track': {'added_by': 'Moi'}}},
   {'identifier': 'https://musicbrainz.org/recording/d54669e6-1acd-4848-acf0-59a20c0972dd',
    'extension': {'https://musicbrainz.org/doc/jspf#track': {'added_by': 'Moi'}}}]},

but the backend (https://api.listenbrainz.org/1/playlist/{playlist_mbid}/item/add) raise {"code":400,"error":"The browser (or proxy) sent a request that this server could not understand."} Seems like a flask error. Idk where the error is coming from :s

Hi! Can you share the code and logs of some of the HTTP requests (including headers) it is making? I can then take a look at it and try to debug.

On a quick search, it seems you maybe missing the Content-Type: application/json header.

thxxx :slight_smile:

def add_mbid_to_playlist(mbids):
    playlist_mbid = "783d06d9-7ce8-4b2e-aa4e-1827bd3f5bd3"
    tracks = []
    for mbid in mbids:
        track_namespace_data = {"added_by" : "Moi"}
        track = { "identifier" : f"https://musicbrainz.org/recording/{mbid}", "extension" : { "https://musicbrainz.org/doc/jspf#track" : track_namespace_data} }
        tracks.append(track)

    plt_ext = {"https://musicbrainz.org/doc/jspf#playlist" : {"creator": "me"}}
    playlist_data = {"extension" : plt_ext, "title" : "test", "tracks" : tracks}
    playlist = {"playlist" : playlist_data }
    
    headers={"Authorization": "Token mytoken", "Content-Type": "application/json"}
    response = requests.post(f"https://api.listenbrainz.org/1/playlist/{playlist_mbid}/item/add", data=playlist, headers=headers)    
    
    return response.text

validate_playlist from listenbrainz-server/listenbrainz/webserver/views/playlist_api.py at 4aa44b6caf561ac77e35a59966f7e75781223ee3 · metabrainz/listenbrainz-server · GitHub do not seem trigerred

We intend to add support for exporting playlists to spotify. Its a WIP, see Add spotify submission support by amCap1712 · Pull Request #66 · metabrainz/troi-recommendation-playground · GitHub for details. I have lightly tested it and it works for me. You can try it out and report any issues that you face.

The major thing missing is how to get spotify track ids from MBIDs or otherwise. We are working actively to kind of build a mapping from MBIDs to spotify ids that troi can query. troi can then add spotify tracks ids to the recordings in a playlist and submit it to spotify.

We currently haven’t explored exporting playlists to youtube.

1 Like

okey thx you this is the main question I had. Do not bother trying to find out what went wrong in my code :s

Thanks a lot ! :slight_smile:

I tried out the code anyway to make sure there wasn’t something wrong on the server side. The issue is here"

requests.post(f"https://api.listenbrainz.org/1/playlist/{playlist_mbid}/item/add", data=playlist, headers=headers)

requests encodes the dict data as URL form and the body becomes playlist=extension&playlist=title&playlist=track.

You should dump the dict to json before, so

requests.post(f"https://api.listenbrainz.org/1/playlist/{playlist_mbid}/item/add", data=json.dumps(playlist), headers=headers)

There’s also a typo in playlist_data dict, tracks key should be track. Fixing these two issues should make the code work if you need it.

2 Likes

thx a looot :slight_smile: It’s workiiiings o/ <3

I’ve been testing the recommendations and I think they are great !

  • there is diversity, but you can group the list with similar artist which allow to have some coherence in the styles and mood. This way you can play the playlist ordered but from a random point of the playlist

  • there is a good ratio of known and unknown songs

Maybe there is two much very famous songs, for me its okey I like it but maybe you could add an option to clean the playlist from some of them.

Thx ! :slight_smile:

3 Likes