Skip to content

Instantly share code, notes, and snippets.

@relevitt
Last active June 9, 2016 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relevitt/976234b221719706b1abd564e86f2f70 to your computer and use it in GitHub Desktop.
Save relevitt/976234b221719706b1abd564e86f2f70 to your computer and use it in GitHub Desktop.
A function for adding a spotify playlist to sonos favorites
def add_spotify_playlist_to_favorites(device, service, spotify_playlist_id):
'''
device: a soco device
e.g. import soco
device = list(soco.discover())[0].group.coordinator
service: an instance of MusicService('Spotify')
e.g. from soco.music_services import MusicService
service = MusicService('Spotify')
spotify_playlist_id: e.g. 'spotify:user:bigclot:playlist:5X3yf8MBN94kDk98Ak2j0h'
'''
didl = '<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/"><container id="{0}" parentID="DUMMY" restricted="true"><dc:title>{1}</dc:title><res protocolInfo="x-rincon-cpcontainer:*:*:*">{0}</res><upnp:class>object.container.playlistContainer</upnp:class><r:type>instantPlay</r:type><r:description>Spotify Playlist</r:description><r:resMD>&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;{0}&quot; parentID=&quot;DUMMY&quot; restricted=&quot;true&quot;&gt;&lt;dc:title&gt;{1}&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;{2}&lt;/desc&gt;&lt;/item&gt;&lt;/DIDL-Lite&gt;</r:resMD><upnp:albumArtURI>{3}</upnp:albumArtURI></container></DIDL-Lite>'
## The 8 digit prefix can't be just anything. For Spotify Playlists
## I've had success with 10060a6c and 10062a6c. I haven't looked further.
item_id ='10060a6c{0}'.format(quote_url(spotify_playlist_id))
metadata = service.get_extended_metadata(spotify_playlist_id)
title = metadata['mediaCollection']['title']
desc = service.desc
albumArtURI = metadata['mediaCollection']['albumArtURI']
didl_string = didl.format(item_id, title, desc, albumArtURI)
try:
device.contentDirectory.CreateObject([
('ContainerID', 'FV:2'),
('Elements', didl_string)
])
except Exception as e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment