How to iterate over playlist songs?

574 views
Skip to first unread message

Flavio Castelli

unread,
Aug 14, 2014, 3:46:40 AM8/14/14
to pytho...@googlegroups.com
I would like to reorganize my music library but I'm pretty sure that moving files around is going to break my playlists. I looked into how to iterate over the tracks of a sonos playlist and figure out which files are being referenced using SoCo but I have some troubles.

I've been able to list my sonos playlist (using the SoCo.get_sonos_playlist method) but I couldn't find how to inspect them. All I want is to iterate over the songs of a playlist and see which file the track is referencing.

Is that currently possible?


One last question, where did you find the Sonos' API documentation? It looks like it's available only after registering as "Sonos Music Partner"...

Could you point me to the right direction?

Thanks
Flavio

DPH

unread,
Aug 15, 2014, 5:12:24 AM8/15/14
to pytho...@googlegroups.com
Hi Flavio,

The very latest SoCo on Github has great work by @KennethNielsen allowing Browse of the Music Library, and decoding all the data returned. 
The one area I found it failed was on Sonos playlists, but I have proposed a fix which works for me. 

If you download the latest GitHub SoCo then add the following to the end of the class MLSonosPlaylist(MusicLibraryItem): in data_structures.py after line 540:

    @property
    def item_id(self):  # pylint: disable=C0103
        """Returns the id."""
        out = 'SQ:' + super(MLSonosPlaylist, self).item_id
        return out

This fixes the bug I have raises in issue #196 where to browse the Sonos queues you need to add SQ: before the item_id (playlist number).

With SoCo fixed you can now do:

"""
Demo of listing Sonos Playlists
"""

from soco import SoCo
import urllib
soc = SoCo('192.168.1.64')

playlist_info = soc.get_music_library_information('sonos_playlists')
print('Found {} Sonos playlists'.format(playlist_info['number_returned']))
playlists = playlist_info['item_list']

for playlist in playlists:
    #for each playlist
    print ' '
    print '>>>>', playlist.title
    track_list = soc.browse(playlist)['item_list']

    for item in track_list:
        #for each track in a playlist
        title = item.title
        album = item.album
        artist = item.title
        uri = item.uri
        art = item.album_art_uri

        try:
            print 'Title: {}  Album: {}  Artist: {}  Source: {}'.format(
                title, album, artist, uri)
        except UnicodeEncodeError:
            print 'UnicodeError - special characters used!'

The only reason to have the try / except on Unicode error is some tracks have foreign characters in them and these won't print properly.
I need to learn how to handles these better.....

This will give you the path and file name of each track in all you Sonos playlists
However - it is read only! So useful information, but won't really help you update it!
It will at least tell you which ones to change. 

Re documentation form Sonos - there isn't any! Sonos don't release it. So it is all by discovery using wire shark / UPNP tools for developers and learning from others. 
A useful collection of info is at https://github.com/SoCo/SoCo/wiki

Hope this all helps,
let me know you progress or any further question
Cheers David

DPH

unread,
Aug 15, 2014, 1:13:22 PM8/15/14
to pytho...@googlegroups.com
Hi again,
another way to view the playlist path and file info is to use Media Monkey.
Again read only.

Cheers David

On Thursday, 14 August 2014 08:46:40 UTC+1, Flavio Castelli wrote:

Flavio Castelli

unread,
Aug 18, 2014, 3:40:15 AM8/18/14
to pytho...@googlegroups.com
On 08/15/2014 11:12 AM, DPH wrote:
> The only reason to have the try / except on Unicode error is some tracks
> have foreign characters in them and these won't print properly.
> I need to learn how to handles these better.....

You just have to build a unicode string:

print u'Title: {} Album: {} Artist: {} Source: {}'.format(
title, album, artist, uri)

> This will give you the path and file name of each track in all you Sonos
> playlists

Yes, it works like a charm

> However - it is read only! So useful information, but won't really help you
> update it!

Is it possible to create a new playlist via API?

> Re documentation form Sonos - there isn't any! Sonos don't release it. So
> it is all by discovery using wire shark / UPNP tools for developers and
> learning from others.
> A useful collection of info is at https://github.com/SoCo/SoCo/wiki

What a shame!

Thanks for your help

Flavio

Kenneth Nielsen

unread,
Aug 18, 2014, 4:02:21 AM8/18/14
to Flavio Castelli, pytho...@googlegroups.com
I glad it works for you. I don't think we have much implemented yet for manipulating Sonos playlists, but maybe some of the others have more info to fill in. Alternatively, if you have some functionality you would like to see and that we don't have then we can certainly help you get started hacking on SoCo.

Regarding documentation. I think there is a small confusion here. Documentation on how to communicate with the Sonos devices (what we do) are as DPH says. There isn't any, we had to get the information from elsewhere.
What you have seen on their webpage is the documentation for the Sonos API for writing a plugin for the Sonos devices i.e. if you run a music service that you would like Sonos to be able to use, so it is something completely different.

Regards Kenneth




Flavio

--
You received this message because you are subscribed to the Google Groups "SoCo - Sonos Controller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-soco+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Flavio Castelli

unread,
Aug 18, 2014, 4:42:05 PM8/18/14
to Kenneth Nielsen, pytho...@googlegroups.com
On 08/18/2014 10:02 AM, Kenneth Nielsen wrote:
> I glad it works for you. I don't think we have much implemented yet for
> manipulating Sonos playlists, but maybe some of the others have more info
> to fill in. Alternatively, if you have some functionality you would like to
> see and that we don't have then we can certainly help you get started
> hacking on SoCo.

I already helped myself out :) https://github.com/SoCo/SoCo/pull/198

I'm looking for your feedback on GitHub.

Cheers
Flavio
Reply all
Reply to author
Forward
0 new messages