node-sonos icon indicating copy to clipboard operation
node-sonos copied to clipboard

Spotify

Open bencevans opened this issue 12 years ago • 7 comments

AddSpotifyToQueue -https://github.com/DjMomo/sonos/blob/9833a311832d59d0946b30d20f7f85918dedf023/sonos.class.php#L473

bencevans avatar Jan 11 '14 05:01 bencevans

I try to play a Spotify playlist with

sonos.play('x-rincon-cpcontainer:1006006cspotify%3auser%3aspotify%3aplaylist%3a7BixMZxL4bhgULJQ5wPbUz', function(  ...

and it doesn't work (HTTP response 500). Would this enhancement be the missing piece or are I'm doing something wrong?

HarlekinX avatar Mar 19 '15 21:03 HarlekinX

Not 100% sure without looking into it further put it's quite likely

bencevans avatar Mar 19 '15 21:03 bencevans

I did a little experimentation on how to play a Spotify playlist, although it may be the wrong place here because the link in your initial post shows how to add a Spotify track to the queue.

First I used Wireshark to sniff the communication between the Sonos app and the speakers. The body it sends looks like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <s:Body>
    <u:AddURIToQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
      <InstanceID>0</InstanceID>
      <EnqueuedURI>x-rincon-cpcontainer:1006006cspotify%3auser%3abaretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC</EnqueuedURI>
      <EnqueuedURIMetaData>
        &lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;&lt;item id=&quot;1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC&quot; parentID=&quot;10080664playlists%3afolder%3a6954091786839755846&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;Enter The Dub&lt;/dc:title&gt;&lt;upnp:class&gt;object.container.playlistContainer&lt;/upnp:class&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON2311_X_#Svc2311-0-Token&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;
      </EnqueuedURIMetaData>
      <DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued>
      <EnqueueAsNext>0</EnqueueAsNext>
    </u:AddURIToQueue>
  </s:Body>
</s:Envelope> 

Required to successfully queue a Spotify playlist are the URI and the metadata. Unfortunately there seems to be no possibility to get the information without a tool like Wireshark, but maybe (hopefully) I'm wrong. But for me this is no problem, I sniff the information of all playlists I want to use and store them in variables.

I used your Sonos.prototype.queue function and modified it a little bit. It already has everything needed except a parameter to hand over the metadata.

var Sonos = require('sonos').Sonos;

// Extend with the ability to add Spotify and other playlists to queue

Sonos.prototype.queuePlaylist = function (uri, metadata, positionInQueue, callback) {
  if (typeof positionInQueue === 'function') {
    callback = positionInQueue;
    positionInQueue = 0;
  }
  var action = '"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue"';
  var body = '<u:AddURIToQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><EnqueuedURI>' + uri + '</EnqueuedURI><EnqueuedURIMetaData>' + metadata + '</EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>0</EnqueueAsNext></u:AddURIToQueue>';

  this.request(this.options.endpoints.transport, action, body, 'u:AddURIToQueueResponse', function(err, data) {
    return callback(err, data);
  });
}

// Stored playlist information

var sonosPlaylist = {
  'Enter The Dub': {
    'uri': 'x-rincon-cpcontainer:1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC',
    'meta': '&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot; xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot; xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;&lt;item id=&quot;1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC&quot; parentID=&quot;10080664playlists%3afolder%3a6954091786839755846&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;Enter The Dub&lt;/dc:title&gt;&lt;upnp:class&gt;object.container.playlistContainer&lt;/upnp:class&gt;&lt;desc id=&quot;cdudn&quot; nameSpace=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;&gt;SA_RINCON2311_X_#Svc2311-0-Token&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;'
  }
};

// Queue the playlist

var sonos = new Sonos('192.168.2.204');

sonos.queuePlaylist(sonosPlaylist['Enter The Dub'].uri, sonosPlaylist['Enter The Dub'].meta, function(err, data){});

// Play the Spotify playlist :-)

sonos.play(function(err, data){});

I think this should work with every music service on our Sonos.

HarlekinX avatar Mar 21 '15 22:03 HarlekinX

Thanks for the research! If you fancy writing an abstraction to make this easier in future a pull request would be gratefully received.

On Saturday, March 21, 2015, Matthias Brünning [email protected] wrote:

I did a little experimentation on how to play a Spotify playlist, although it may be the wrong place here because the link in your initial post shows how to add a Spotify track to the queue.

First I used Wireshark to sniff the communication between the Sonos app and the speakers. The body it sends looks like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body> <u:AddURIToQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"> 0 x-rincon-cpcontainer:1006006cspotify%3auser%3abaretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC <DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"><item id="1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC" parentID="10080664playlists%3afolder%3a6954091786839755846" restricted="true"><dc:title>Enter The Dub</dc:title><upnp:class>object.container.playlistContainer</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON2311_X_#Svc2311-0-Token</desc></item></DIDL-Lite> 0 0 /u:AddURIToQueue /s:Body /s:Envelope

Required to successfully queue a Spotify playlist are the URI and the metadata. Unfortunately there seems to be no possibility to get the information without a tool like Wireshark, but maybe (hopefully) I'm wrong. But for me this is no problem, I sniff the information of all playlists I want to use and store them in variables.

I used your Sonos.prototype.queue function and modified it a little bit. It already has everything needed except a parameter to hand over the metadata.

var Sonos = require('sonos').Sonos; // Extend with the ability to add Spotify and other playlists to queue Sonos.prototype.queuePlaylist = function (uri, metadata, positionInQueue, callback) { if (typeof positionInQueue === 'function') { callback = positionInQueue; positionInQueue = 0; } var action = '"urn:schemas-upnp-org:service:AVTransport:1#AddURIToQueue"'; var body = '<u:AddURIToQueue xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">0' + uri + '' + metadata + '00/u:AddURIToQueue';

this.request(this.options.endpoints.transport, action, body, 'u:AddURIToQueueResponse', function(err, data) { return callback(err, data); }); } // Stored playlist information var sonosPlaylist = { 'Enter The Dub': { 'uri': 'x-rincon-cpcontainer:1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC', 'meta': '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:r="urn:schemas-rinconnetworks-com:metadata-1-0/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"><item id="1006006cspotify%3auser%3casretom%3aplaylist%3a50oyVNwpgWEaBSL5YDybtC" parentID="10080664playlists%3afolder%3a6954091786839755846" restricted="true"><dc:title>Enter The Dub</dc:title><upnp:class>object.container.playlistContainer</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON2311_X_#Svc2311-0-To ken</desc></item></DIDL-Lite>' } }; // Queue the playlist var sonos = new Sonos('192.168.2.204');

sonos.queuePlaylist(sonosPlaylist['Enter The Dub'].uri, sonosPlaylist['Enter The Dub'].meta, function(err, data){}); // Play the Spotify playlist :-)

sonos.play(function(err, data){});

I think this should work with every music service on your Sonos.

— Reply to this email directly or view it on GitHub https://github.com/bencevans/node-sonos/issues/23#issuecomment-84464386.

bencevans avatar Mar 23 '15 14:03 bencevans

It would be an honor for me to collaborate on your project. However, I am a Node.js novice. I need to figure out how to include this to your code in a useful manner. In the meantime I have some other components I could contribute.

HarlekinX avatar Mar 25 '15 19:03 HarlekinX

Anyone wanting to contribute is more than welcome too whatever your level! We are all novices at some point :)

On Wednesday, March 25, 2015, Matthias Brünning [email protected] wrote:

It would be an honor for me to collaborate on your project. However, I am a Node.js novice. I need to figure out how to include this to your code in a useful manner. In the meantime I have some other components I could contribute.

— Reply to this email directly or view it on GitHub https://github.com/bencevans/node-sonos/issues/23#issuecomment-86181138.

bencevans avatar Mar 27 '15 13:03 bencevans

Added by #89

bencevans avatar Oct 02 '15 17:10 bencevans