Skip to content

Commit

Permalink
Added getQueue and addSpotifyQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
htilly committed Jul 13, 2016
1 parent 6bdb55b commit bfb9956
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions lib/sonos.js
Expand Up @@ -958,6 +958,86 @@ Sonos.prototype.getFavoritesRadio = function (favoriteRadioType, options, callba
})
}


// Add Spotify track to the queue.

Sonos.prototype.addSpotifyQueue = function(track_id, callback) {
var rand = '00030020'
var uri = 'x-sonos-spotify:spotify%3atrack%3a' + track_id
var 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="' + rand + 'spotify%3atrack%3a' + track_id + '" restricted="true"><dc:title></dc:title><upnp:class>object.item.audioItem.musicTrack</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON2311_X_#Svc2311-0-Token</desc></item></DIDL-Lite>'

this.queue({
uri: uri,
metadata: meta
}, callback)
}


// Get queue

Sonos.prototype.getQueue = function (callback) {
var self = this

var defaultOptions = {
BrowseFlag: 'BrowseDirectChildren',
Filter: '*',
StartingIndex: '0',
RequestedCount: '1000',
SortCriteria: ''
}

var opts = {
ObjectID: 'Q:0'
}

opts = _.extend(defaultOptions, opts)

var contentDirectory = new Services.ContentDirectory(this.host, this.port)
return contentDirectory.Browse(opts, function (err, data) {
console.log(err)
console.log(data)
if (err) return callback(err)
return (new xml2js.Parser()).parseString(data.Result, function (err, didl) {
if (err) return callback(err, data)
var items = []
if ((!didl) || (!didl['DIDL-Lite'])) {
return callback(new Error('Cannot parse DIDTL result'), data)
}
var resultcontainer = didl['DIDL-Lite'].item
if (!util.isArray(resultcontainer)) {
return callback(new Error('Cannot parse DIDTL result'), data)
}
_.each(resultcontainer, function (item) {
var albumArtURL = null
if (util.isArray(item['upnp:albumArtURI'])) {
if (item['upnp:albumArtURI'][0].indexOf('http') !== -1) {
albumArtURL = item['upnp:albumArtURI'][0]
} else {
albumArtURL = 'http://' + self.host + ':' + self.port + item['upnp:albumArtURI'][0]
}
}
items.push({
'title': util.isArray(item['dc:title']) ? item['dc:title'][0] : null,
'artist': util.isArray(item['dc:creator']) ? item['dc:creator'][0] : null,
'albumArtURL': albumArtURL,
'album': util.isArray(item['upnp:album']) ? item['upnp:album'][0] : null,
'uri': util.isArray(item.res) ? item.res[0]._ : null
})
})
var result = {
returned: data.NumberReturned,
total: data.TotalMatches,
items: items
}
return callback(null, result)
})
})
}





/**
* Search "Class"
* Emits 'DeviceAvailable' on a Sonos Component Discovery
Expand Down

0 comments on commit bfb9956

Please sign in to comment.