Created
July 14, 2016 10:46
-
-
Save noestreich/d7411b623c7a50d11042b3be8bde5dd8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
#-*- coding: utf-8 -*- | |
import pygame, soco | |
from pygame.locals import * | |
### Sonos im Netzwerk finden | |
speakers = soco.discover() | |
### Sonos Status und Playlist-Infos | |
def is_playing(transport_info): | |
state = transport_info['current_transport_state'] | |
if state == 'PLAYING': | |
return True | |
elif state == 'PAUSED_PLAYBACK': | |
return False | |
elif state == 'STOPPED': | |
return False | |
def get_sonos_playlist(sonos, title): | |
playlists = sonos.get_sonos_playlists() | |
for playlist in playlists: | |
if playlist.title == title: | |
return playlist | |
return None | |
### Radio-Metadaten Swissgroove | |
swiss_media_uri = 'x-sonosapi-stream:s48811?sid=254&flags=32' | |
swiss_media_metadata = '<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="-1" parentID="-1" restricted="true"><dc:title>SwissGroove Radio</dc:title><upnp:class>object.item.audioItem.audioBroadcast</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON65031_</desc></item></DIDL-Lite>' | |
### Radio-Metadaten Deutschlandradio | |
dradio_media_uri = 'x-sonosapi-stream:s15028?sid=254&flags=32&sn=0' | |
dradio_media_metadata = '<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="-1" parentID="-1" restricted="true"><dc:title>Deutschlandfunk</dc:title><upnp:class>object.item.audioItem.audioBroadcast</upnp:class><desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON65031_</desc></item></DIDL-Lite>' | |
### Hauptprogramm | |
def main(): | |
pygame.init() | |
clock = pygame.time.Clock() | |
joysticks = [] | |
for i in range(0, pygame.joystick.get_count()): | |
joysticks.append(pygame.joystick.Joystick(i)) | |
joysticks[-1].init() | |
print "Detected joystick '",joysticks[-1].get_name(),"'" | |
while 1: | |
clock.tick(60) | |
for event in pygame.event.get(): | |
if event.type == JOYBUTTONDOWN and event.button == 1: | |
print "Play Swissgroove (A)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
speaker.stop() | |
speaker.clear_queue() | |
speaker.partymode() | |
speaker.play_uri(swiss_media_uri, swiss_media_metadata, start=True) | |
speaker.volume = 18 | |
elif event.type == JOYBUTTONDOWN and event.button == 2: | |
print "Play Deutschlandradio (B)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
speaker.stop() | |
speaker.clear_queue() | |
speaker.partymode() | |
speaker.play_uri(dradio_media_uri, dradio_media_metadata, start=True) | |
speaker.volume = 18 | |
elif event.type == JOYBUTTONDOWN and event.button == 9: | |
print "Play/Pause (START)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
if not is_playing(speaker.get_current_transport_info()): | |
speaker.play() | |
else: | |
speaker.pause() | |
elif event.type == JOYBUTTONDOWN and event.button == 8: | |
print "Play padplaylist (SELECT)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
speaker.partymode() | |
speaker.stop() | |
speaker.clear_queue() | |
speaker.volume = 18 | |
playlist = get_sonos_playlist(speaker, 'padplaylist') | |
print playlist | |
if playlist: | |
speaker.add_to_queue(playlist) | |
speaker.play_from_queue(0) | |
elif event.type == JOYAXISMOTION and event.axis == 1 and event.value == 1: | |
#print "pfeil unten",event.value | |
print "Leiser (RUNTER)" | |
for speaker in speakers: | |
speaker.volume -= 2 | |
elif event.type == JOYAXISMOTION and event.axis == 1 and event.value < 0: | |
print "Lauter (HOCH)" | |
for speaker in speakers: | |
speaker.volume += 2 | |
elif event.type == JOYAXISMOTION and event.axis == 0 and event.value == 1: | |
print "Vor (RECHTS)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
speaker.next() | |
elif event.type == JOYAXISMOTION and event.axis == 0 and event.value < 0: | |
print "Zurueck (LINKS)" | |
for speaker in speakers: | |
if speaker.group.coordinator is speaker: | |
speaker.previous() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment