HTTP API - Play Tracklist

I’ve installed the api_explorer extension and am testing the API with the examples given therein.
I’ve created a tracklist. The following

{
  "method": "core.tracklist.get_length",
  "jsonrpc": "2.0",
  "params": {},
  "id": 1
}

returns: 3. Also, the following

{
  "method": "core.tracklist.get_tl_tracks",
  "jsonrpc": "2.0",
  "params": {},
  "id": 1
}

returns

{"jsonrpc": "2.0", "id": 1, "result": [{"track": {"album": {"date": 2008, "__model__": "Album", "artists": [{"__model__": "Artist", "name": "The Royal Schubert Orchestra", "uri": "spotify:artist:4TzMCWYmKGLdQ7YgJ3n6Ce"}], "uri": "spotify:album:66Exlq5cx3D5PKjt6CGr8B", "name": "Musica Clasica - Franz Schubert"}, "__model__": "Track", "name": "Ave Maria D.389 - Schubert", "uri": "spotify:track:0wYnWuQtmtC4szKYLGyglC", "length": 319000, "track_no": 1, "artists": [{"__model__": "Artist", "name": "The Royal Schubert Orchestra", "uri": "spotify:artist:4TzMCWYmKGLdQ7YgJ3n6Ce"}], "date": 2008, "bitrate": 320}, "__model__": "TlTrack", "tlid": 56}, {"track": {"album": {"date": 2006, "__model__": "Album", "artists": [{"__model__": "Artist", "name": "Hans Zimmer", "uri": "spotify:artist:0YC192cP3KPCRWx8zr8MfZ"}], "uri": "spotify:album:1ibEFICF3urEo7NtEMxgHK", "name": "The Da Vinci Code"}, "__model__": "Track", "name": "Chevaliers De Sangreal", "uri": "spotify:track:4VaOecssBTF02dO0n2u8BS", "length": 247000, "track_no": 13, "artists": [{"__model__": "Artist", "name": "Hans Zimmer", "uri": "spotify:artist:0YC192cP3KPCRWx8zr8MfZ"}], "date": 2006, "bitrate": 320}, "__model__": "TlTrack", "tlid": 57}, {"track": {"album": {"date": 1996, "__model__": "Album", "artists": [{"__model__": "Artist", "name": "Andrea Bocelli", "uri": "spotify:artist:3EA9hVIzKfFiQI0Kikz2wo"}], "uri": "spotify:album:5bwuR4PSrTRlCgRuNS5ZMB", "name": "Romanza"}, "__model__": "Track", "name": "Con Te Partir\u00f2", "uri": "spotify:track:7r3EaAW4FiPCRnPYZ7azfl", "length": 251000, "track_no": 1, "artists": [{"__model__": "Artist", "name": "Andrea Bocelli", "uri": "spotify:artist:3EA9hVIzKfFiQI0Kikz2wo"}, {"__model__": "Artist", "name": "Francesco Sartori", "uri": "spotify:artist:0VtVDsLKIsyiBa1DeAdKbU"}], "date": 1996, "bitrate": 320}, "__model__": "TlTrack", "tlid": 58}]}

My Question: How do I play the tracklist?

I’ve tried:

{
  "method": "core.playback.play",
  "jsonrpc": "2.0",
  "params": {
    "tl_track": "spotify:track:0wYnWuQtmtC4szKYLGyglC",
    "on_error_step": 1
  },
  "id": 1
}

which returns an error:

{"jsonrpc": "2.0", "id": 1, "error": {"message": "Application error", "code": 0, "data": {"message": "", "traceback": "Traceback (most recent call last):\n  File \"/usr/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py\", line 130, in _handle_single_request\n    result = self._unwrap_result(result)\n  File \"/usr/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py\", line 219, in _unwrap_result\n    result = result.get()\n  File \"/usr/lib/python2.7/dist-packages/pykka/future.py\", line 299, in get\n    exec('raise exc_info[0], exc_info[1], exc_info[2]')\n  File \"/usr/lib/python2.7/dist-packages/pykka/actor.py\", line 200, in _actor_loop\n    response = self._handle_receive(message)\n  File \"/usr/lib/python2.7/dist-packages/pykka/actor.py\", line 294, in _handle_receive\n    return callee(*message['args'], **message['kwargs'])\n  File \"/usr/lib/python2.7/dist-packages/mopidy/core/playback.py\", line 224, in play\n    assert tl_track in self.core.tracklist.tl_tracks\nAssertionError\n", "type": "AssertionError"}}}

and I’ve tried:

{
  "method": "core.playback.change_track",
  "jsonrpc": "2.0",
  "params": {
    "tl_track":"spotify:track:0wYnWuQtmtC4szKYLGyglC",
    "on_error_step": 1
  },
  "id": 1
}

which also returns an error:

{"jsonrpc": "2.0", "id": 1, "error": {"message": "Application error", "code": 0, "data": {"message": "'int' object has no attribute 'track'", "traceback": "Traceback (most recent call last):\n  File \"/usr/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py\", line 130, in _handle_single_request\n    result = self._unwrap_result(result)\n  File \"/usr/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py\", line 219, in _unwrap_result\n    result = result.get()\n  File \"/usr/lib/python2.7/dist-packages/pykka/future.py\", line 299, in get\n    exec('raise exc_info[0], exc_info[1], exc_info[2]')\n  File \"/usr/lib/python2.7/dist-packages/pykka/actor.py\", line 200, in _actor_loop\n    response = self._handle_receive(message)\n  File \"/usr/lib/python2.7/dist-packages/pykka/actor.py\", line 294, in _handle_receive\n    return callee(*message['args'], **message['kwargs'])\n  File \"/usr/lib/python2.7/dist-packages/mopidy/core/playback.py\", line 140, in change_track\n    self.stop()\n  File \"/usr/lib/python2.7/dist-packages/mopidy/core/playback.py\", line 305, in stop\n    backend = self._get_backend()\n  File \"/usr/lib/python2.7/dist-packages/mopidy/core/playback.py\", line 28, in _get_backend\n    uri = self.current_tl_track.track.uri\nAttributeError: 'int' object has no attribute 'track'\n", "type": "AttributeError"}}}

I have also installed the Mopidy-Mopify web extension

pip install Mopidy-Mopify

to test that my server setup was working as expected. This extension is working brilliantly.

What must I execute to get the tracklist to play? Is there a “load” method I’m not calling? I’ve read nearly all of the API documentation to find a solution, but I have obviously missed something.

Thanks for any insights.

tl_track needs to be a tl_track object. If you run mopidy in verbose mode and then use mopify you should be able to see the difference.

Ah, I was sending the uri, not the track. Simple solution - thank you for the help. This is what I’m sending now:

{
  "method": "core.playback.play",
  "jsonrpc": "2.0",
  "params": {
    "tl_track": {"track": {"album": {"date": 2008, "__model__": "Album", "artists": [{"__model__": "Artist", "name": "The Royal Schubert Orchestra", "uri": "spotify:artist:4TzMCWYmKGLdQ7YgJ3n6Ce"}], "uri": "spotify:album:66Exlq5cx3D5PKjt6CGr8B", "name": "Musica Clasica - Franz Schubert"}, "__model__": "Track", "name": "Ave Maria D.389 - Schubert", "uri": "spotify:track:0wYnWuQtmtC4szKYLGyglC", "length": 319000, "track_no": 1, "artists": [{"__model__": "Artist", "name": "The Royal Schubert Orchestra", "uri": "spotify:artist:4TzMCWYmKGLdQ7YgJ3n6Ce"}], "date": 2008, "bitrate": 320}, "__model__": "TlTrack", "tlid": 0},
    "on_error_step": 1
  },
  "id": 1
}

Can someone help me? I can successfully add a track to the tracklist, but I have an error

Here is what I am doing:

  1. Sending a requests to add the track with the python library Requests

    response_data2=requests.post(url, data=json.dumps(data2), headers=header)
    b=yaml.load(response_data2.text)[‘result’][0]
    and when i print b I can see the tl_track:
    {'track': {'album': {'date': 2014, '__model__': 'Album', 'artists': [{'__model__': 'Artist', 'name': 'Wayne Shorter', 'uri': 'spotify:artist:0ZqhrTXYPA9DZR527ZnFdO'}], 'uri': 'spotify:album:2IXMBRgrZgTcKjMCcJHkQv', 'name': 'Night Dreamer'}, '__model__': 'Track', 'name': 'Oriental Folk Song', 'uri': 'spotify:track:6GsR4NEuVi0gPyUWvuv3dy', 'length': 411000, 'track_no': 2, 'artists': [{'__model__': 'Artist', 'name': 'Wayne Shorter', 'uri': 'spotify:artist:0ZqhrTXYPA9DZR527ZnFdO'}], 'date': 2014}, '__model__': 'TlTrack', 'tlid': 1}

  2. When i send this result

    response_data3=requests.post(url, data=json.dumps(data3), headers=header)
    where data3={"method": "core.playback.play", "jsonrpc": "2.0", "params": { "tl_track": b, "on_error_step": 1 }, "id": 1}
    I have this:

    {‘jsonrpc’: ‘2.0’, ‘id’: 1, ‘error’: {‘message’: ‘Application error’, ‘code’: 0, ‘data’: {‘message’: ‘’, ‘traceback’: ‘Traceback (most recent call last):\n File “/usr/local/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py”, line 130, in _handle_single_request\n result = self._unwrap_result(result)\n File “/usr/local/lib/python2.7/dist-packages/mopidy/utils/jsonrpc.py”, line 219, in _unwrap_result\n result = result.get()\n File “/usr/lib/python2.7/dist-packages/pykka/future.py”, line 299, in get\n exec(‘raise exc_info[0], exc_info[1], exc_info[2]’)\n File “/usr/lib/python2.7/dist-packages/pykka/actor.py”, line 200, in _actor_loop\n response = self._handle_receive(message)\n File “/usr/lib/python2.7/dist-packages/pykka/actor.py”, line 294, in _handle_receive\n return callee(*message[‘args’], **message[‘kwargs’])\n File “/usr/local/lib/python2.7/dist-packages/mopidy/core/playback.py”, line 228, in play\n assert tl_track in self.core.tracklist.tl_tracks\nAssertionError\n’, ‘type’: ‘AssertionError’}}}

In MusicBox I can see the added track in the tracklist

I’ve solved sending as data3

data3={"method": "core.playback.play", "jsonrpc": "2.0", "params": { "tl_track": None, "on_error_step": 1 }, "id": 1}

which is fine to me, I don’t need to select a track. Anyway if anyone can explain what was wrong (i suppose it was tl_track json encoding) it would be great.