Skip to content

Instantly share code, notes, and snippets.

@lawrenceakka
Last active September 2, 2015 13:30
Show Gist options
  • Save lawrenceakka/c8597d2533b6cc3e0d18 to your computer and use it in GitHub Desktop.
Save lawrenceakka/c8597d2533b6cc3e0d18 to your computer and use it in GitHub Desktop.
Demonstration of adding items back to the songs queue with correct metadata
# -*- coding: utf-8 -*-
""" Demo to duplicate the first track in the queue.
We can now recreate metadata from a track in the queue. Pick a track you like
and put it at the start of a queue. Run this and enjoy!
"""
# pylint: disable=invalid-name
from __future__ import unicode_literals, print_function
from soco import SoCo
from soco.data_structures import to_didl_string, DidlMusicTrack
from soco.utils import prettify
from soco.music_services import desc_from_uri
from soco.compat import urlparse
s = SoCo('192.168.1.102') # <------- Your IP here
queue = s.get_queue()
if len(queue) == 0:
raise Exception("You must have at least one track in the queue")
# Get the first track
track = queue[0]
# Get its URI
uri = track.resources[0].uri
# Using the uri, get the value of the desc element
desc = desc_from_uri(uri)
# Now we need to create a DIDL item id. It seems to be based on the uri
path = urlparse(uri).path
# Strip any extensions, eg .mp3, from the end of the path
path = path.rsplit('.', 1)[0]
# The ID has an 8 (hex) digit prefix. But it doesn't seem to matter what it is!
track_id = '11111111{0}'.format(path)
print ("uri:", uri)
print ("track_id", track_id)
print ("desc", desc)
# Now we have everything we need
didl = DidlMusicTrack(
title='DUMMY', # This is ignored. Sonos gets the title from the item_id
parent_id='DUMMY', # Ditto
item_id=track_id,
desc=desc
)
meta = to_didl_string(didl)
print (prettify(meta))
for i in range(5):
response = s.avTransport.AddURIToQueue([
('InstanceID', 0),
('EnqueuedURI', uri),
('EnqueuedURIMetaData', meta),
('DesiredFirstTrackNumberEnqueued', 0),
('EnqueueAsNext', 1)
])
s.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment