Last active
June 9, 2016 08:07
-
-
Save relevitt/976234b221719706b1abd564e86f2f70 to your computer and use it in GitHub Desktop.
A function for adding a spotify playlist to sonos favorites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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><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="{0}" parentID="DUMMY" restricted="true"><dc:title>{1}</dc:title><upnp:class>object.container.playlistContainer</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">{2}</desc></item></DIDL-Lite></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