I am playing around with http RPC calls using python. Is my understanding that correct that to play a playlist I have created using Iris, I have to grab it with core.playlists.get_items(playlistURI) and then iterate through that and add those items to the tracklist?
If this is correct, does anyone know if this logic exists somwhere I can steal from?
Yes, as per the docs, get_items
gives you the playlist contents as a list of Ref
objects per track, each of which includes the uri that you can pass to tracklist.add
. You should also be able to use core.playlists.lookup(playlist_uri)
.
However, last time I checked, Iris config instructions recommended setting the Mopidy config spotify/allow_playlists = false
. If you followed that advice, and it’s a Spotify playlist you are taking about, then Mopidy won’t be able to get the items. I’d strongly suggest not setting that config to false, it’s a bad idea for those using Mopidy outside of Iris and the speedup gain is only realised during startup i.e. once.
I don’t think we have any example client code using rpc calls. The javascript webclients implement the same logic, musicbox-webclient does it a simple manner, you could look at that.
Alternatively, there is a popular Python MPD client library you could use.
1 Like
Thank you Nick. I thought that was what I was reading, I just didn’t want to go down this rabbit hole and then find out I missed something.
Here’s some ugliness that works. I am integrating with some software that uses an ancient version of python with custom http and json libraries, so the json load/decode and http post will have to be fixed, but this is the gist. Needs error checking and whatnot.
addr = "http://localhost:6680/mopidy/rpc"
message = '{"jsonrpc": "2.0", "id": 1, "method": "core.playlists.as_list"}'
results= some_http_post(addr, "application/json", message)
playlistDict = some_json_load(results).get("result")
plName = "asdf"
print playlistDict
for list in playlistDict:
if list.get("name") == plName:
message = '{"jsonrpc": "2.0", "id": 1, "method": "core.playlists.get_items", "params": {"uri": "'+ list.get("uri")+'"}}'
results = some_http_post(addr, "application/json", message)
trackDict = some_json_load(results).get("result")
for track in trackDict:
message = '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": {"uris": ["'+ track.get("uri")+'"]}}'
results = some_http_post(addr, "application/json", message)
message = '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}'
results = some_http_post(addr, "application/json", message)
else:
print "not found"