Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get favorites from sonos #177

Closed
gupsho opened this issue Aug 8, 2017 · 1 comment
Closed

Get favorites from sonos #177

gupsho opened this issue Aug 8, 2017 · 1 comment

Comments

@gupsho
Copy link
Contributor

gupsho commented Aug 8, 2017

Is it possible to get a list of favorites from sonos?

@bencevans
Copy link
Owner

Appears to have been implemented for radio but that is all. PR welcome 😸

node-sonos/lib/sonos.js

Lines 951 to 1019 in 230a6b8

/**
* Get Favorites Radio Stations
* @param {Object} options Optional - default {start: 0, total: 100}
* @param {Function} callback (err, result) result - {returned: {String}, total: {String}, items:[{title:{String}, uri: {String}}]}
*/
Sonos.prototype.getFavoritesRadioStations = function (options, callback) {
this.getFavoritesRadio('stations', options, callback)
}
/**
* Get Favorites Radio Shows
* @param {Object} options Optional - default {start: 0, total: 100}
* @param {Function} callback (err, result) result - {returned: {String}, total: {String}, items:[{title:{String}, uri: {String}}]}
*/
Sonos.prototype.getFavoritesRadioShows = function (options, callback) {
this.getFavoritesRadio('shows', options, callback)
}
/**
* Get Favorites Radio for a given radio type
* @param {String} favoriteRadioType Choice - stations, shows
* @param {Object} options Optional - default {start: 0, total: 100}
* @param {Function} callback (err, result) result - {returned: {String}, total: {String}, items:[{title:{String}, uri: {String}}]}
*/
Sonos.prototype.getFavoritesRadio = function (favoriteRadioType, options, callback) {
var radioTypes = {
'stations': 'R:0/0',
'shows': 'R:0/1'
}
var defaultOptions = {
BrowseFlag: 'BrowseDirectChildren',
Filter: '*',
StartingIndex: '0',
RequestedCount: '100',
SortCriteria: '',
ObjectID: 'R:0/0'
}
var opts = {
ObjectID: radioTypes[favoriteRadioType]
}
if (options.start !== undefined) opts.StartingIndex = options.start
if (options.total !== undefined) opts.RequestedCount = options.total
opts = _.extend(defaultOptions, opts)
var contentDirectory = new Services.ContentDirectory(this.host, this.port)
return contentDirectory.Browse(opts, function (err, 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 (!Array.isArray(resultcontainer)) {
return callback(new Error('Cannot parse DIDTL result'), data)
}
_.each(resultcontainer, function (item) {
items.push({
'title': Array.isArray(item['dc:title']) ? item['dc:title'][0] : null,
'uri': Array.isArray(item.res) ? item.res[0]._ : null
})
})
var result = {
returned: data.NumberReturned,
total: data.TotalMatches,
items: items
}
return callback(null, result)
})
})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants