Skip to content

Instantly share code, notes, and snippets.

@funzoneq
Created July 3, 2014 14:12
Show Gist options
  • Save funzoneq/973d6a90eb93f3c9c947 to your computer and use it in GitHub Desktop.
Save funzoneq/973d6a90eb93f3c9c947 to your computer and use it in GitHub Desktop.
22tracks playlists with a sonos queue
#!/usr/bin/env python
import soco
import requests
from pprint import pprint
playlistnr = 20
path = 128
def get_playlist(playlistnr):
pl = requests.get("http://22tracks.com/api/tracks/%s" % playlistnr)
return pl.json()
def get_token(path, file):
t = requests.get("http://app01.22tracks.com/token.php?desktop=true&u=/%s/%s" % (path, file))
return t.json()
def make_url(path, file, st, e):
return "http://audio.22tracks.com/%s/%s?st=%s&e=%s" % (path, file, st, e)
if __name__ == '__main__':
speakers = list(soco.discover())
if not speakers:
print 'no speakers found, exiting.'
exit(1)
sonos = speakers[0] # Pass in the IP of your Sonos speaker
# Clear the play queue
sonos.clear_queue()
# Place all the speakers in party mode
sonos.partymode()
# Fetch the playlist from 22tracks
playlist = get_playlist(playlistnr)
for item in playlist:
file = item['filename']
# Get a token and make the URL
token = get_token(path, file)
url = make_url(path, file, token['st'], token['e'])
# Pass in a URI to a media file to have it streamed through the Sonos speaker
sonos.add_uri_to_queue(url)
track = sonos.get_current_track_info()
print track['title']
# Play a stopped or paused track
sonos.play_from_queue(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment