Ejemplo n.º 1
0
def process_message():
    j = request.json
    print("json loaded =", j)
    action = j['action']
    print("action =", action)

    if action == 'list_queue':
        q = sonos_actions.list_queue()
        return json.dumps(q)

    elif action == 'track_pos':
        track_info = sonos_actions.current()
        p = track_info['playlist_position'] if track_info else "-1"
        return p

    elif action == 'list_artists':
        return json.dumps(sonos_actions.ARTISTS)

    elif action == 'play_pause':
        sonos_actions.play_pause()

    elif action in ('quieter', 'louder'):
        sonos_actions.turn_volume(action)

    elif action == 'next':
        sonos_actions.playback('next')

    elif action.startswith("station"):
        station = action[8:]
        sonos_actions.play_station(station)

    elif action.startswith("shuffle"):
        artist = action[8:]
        sonos_actions.shuffle(artist)

    elif action.startswith("play_queue"):
        pos = action[11:]
        pos = int(pos) if pos else 0
        sonos_actions.play_from_queue(pos)

    elif action == 'mute':
        sonos_actions.mute(True)

    elif action == 'unmute':
        sonos_actions.mute(False)

    else:
        print("I don't recognize that action")

    return "OK"
Ejemplo n.º 2
0
def process_message():
    j = request.json
    print("json loaded =", j)
    action = j['action']
    print("action =", action)

    if action == 'list_queue':
        q = sonos_actions.list_queue()
        return json.dumps(q)

    elif action == 'track_pos':
        track_info = sonos_actions.current()
        p = track_info['playlist_position'] if track_info else "-1"
        return p

    elif action == 'list_artists':
        return json.dumps(sonos_actions.ARTISTS)

    elif action == 'play_pause':
        sonos_actions.play_pause()

    elif action in ('quieter','louder'):
        sonos_actions.turn_volume(action)
        
    elif action == 'next':
        sonos_actions.playback('next')

    elif action.startswith("station"):
        station = action[8:]
        sonos_actions.play_station(station)

    elif action.startswith("shuffle"):
        artist = action[8:]
        sonos_actions.shuffle(artist)

    elif action.startswith("play_queue"):
        pos = action[11:]
        pos = int(pos) if pos else 0
        sonos_actions.play_from_queue(pos)

    elif action == 'mute':
        sonos_actions.mute(True)

    elif action == 'unmute':
        sonos_actions.mute(False)

    else:
        print("I don't recognize that action")

    return "OK"
Ejemplo n.º 3
0
 def do_station(self, s):
     '''
     play station entered; if no station entered
     show available stations
     '''
     if s:
         self.msg = sonos_actions.play_station(s)
     else:
         stations = sonos_actions.STATIONS
         lst = [(z[0], z[1][0]) for z in stations.items()]
         #lst.append((0, "Do nothing"))
         station = self.select2(lst, "Which station? ")
         if station:
             sonos_actions.play_station(station)
             self.msg = self.colorize(f"I'll play {station} now", 'green')
         else:
             self.msg = self.colorize("OK, I won't play anything.", 'red')
Ejemplo n.º 4
0
 def do_station(self, s):
     '''
     play station entered; if no station entered
     show available stations
     '''
     if s:
         self.msg = sonos_actions.play_station(s)
     else:
         stations = sonos_actions.STATIONS
         lst = [(z[0], z[1][0]) for z in stations.items()]
         #lst.append((0, "Do nothing"))
         station = self.select2(lst, "Which station? ")
         if station:
             sonos_actions.play_station(station)
             self.msg = colorize(f"I'll play {station} now", 'green')
         else:
             self.msg = colorize("OK, I won't play anything.", 'red')
Ejemplo n.º 5
0
def play_station(station):
    if station.lower()=='deborah':
        s = 'album:(c)'
        result = solr.search(s, fl='uri,title,artist', rows=600) 
        count = len(result)
        print(f"Total track count for Deborah tracks was {count}")
        tracks = result.docs
        selected_tracks = random.sample(tracks, 20) # randomly decided to pick 20 songs
        uris = [t.get('uri') for t in selected_tracks]
        sonos_actions.play(False, uris)
        titles = [t.get('title', '')+'-'+t.get('artist', '') for t in selected_tracks]
        title_list = "\n".join([f"{t[0]}. {t[1]}" for t in enumerate(titles, start=1)])
        return f"I will play these songs that Deborah chose:\n{title_list}."

    else:
        sonos_actions.play_station(station)

    return f"I will try to play station {station}."
Ejemplo n.º 6
0
def play_station(station):
    if station.lower()=='deborah':
        s = 'album:(c)'
        result = solr.search(s, fl='uri,title,artist', rows=600) 
        count = len(result)
        print(f"Total track count for Deborah tracks was {count}")
        tracks = result.docs
        selected_tracks = random.sample(tracks, 20) # randomly decided to pick 20 songs
        uris = [t.get('uri') for t in selected_tracks]
        sonos_actions.play(False, uris)
        titles = [t.get('title', '')+'-'+t.get('artist', '') for t in selected_tracks]
        title_list = "\n".join([f"{t[0]}. {t[1]}" for t in enumerate(titles, start=1)])
        return f"I will play these songs that Deborah chose:\n{title_list}."

    else:
        sonos_actions.play_station(station)

    return f"I will try to play station {station}."
Ejemplo n.º 7
0
def play_station(station):
    if station.lower()=='deborah':
        s = 'album:(c)'
        result = solr.search(s, fl='uri', rows=600) 
        count = len(result)
        print("Total track count for Deborah tracks was {}".format(count))
        tracks = result.docs
        selected_tracks = random.sample(tracks, 20) # randomly decided to pick 20 songs
        uris = [t.get('uri') for t in selected_tracks]
        msg = sonos_actions.play(False, uris)
    else:
        msg = sonos_actions.play_station(station)

    print("PlayStation({}) return msg from zmq:".format(station), msg)
    return statement("I will try to play station {}.".format(station))
Ejemplo n.º 8
0
def play_station(station):
    if station.lower() == 'deborah':
        s = 'album:(c)'
        result = solr.search(s, fl='uri', rows=600)
        count = len(result)
        print("Total track count for Deborah tracks was {}".format(count))
        tracks = result.docs
        selected_tracks = random.sample(
            tracks, 20)  # randomly decided to pick 20 songs
        uris = [t.get('uri') for t in selected_tracks]
        msg = sonos_actions.play(False, uris)
    else:
        msg = sonos_actions.play_station(station)

    print("PlayStation({}) return msg from zmq:".format(station), msg)
    return statement("I will try to play station {}.".format(station))
Ejemplo n.º 9
0
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    body = msg.payload
    print("mqtt messge body =", body)

    try:
        task = json.loads(body)
    except Exception as e:
        print("error reading the mqtt message body: ", e)
        return

    action = task.get('action', '')

    if action == 'play_pause':
    
        try:
            state = master.get_current_transport_info()['current_transport_state']
        except Exception as e:
            print("Encountered error in state = master.get_current_transport_info(): ", e)
            state = 'ERROR'

        # check if sonos is playing music
        if state == 'PLAYING':
            master.pause()
        elif state!='ERROR':
            master.play()

    elif action in ('quieter','louder'):
        
        for s in sp:
            s.volume = s.volume - 10 if action=='quieter' else s.volume + 10

        print("I tried to make the volume "+action)

    elif action == "volume": #{"action":"volume", "level":70}

        level = task.get("level", 500)
        level = int(round(level/10, -1))

        if level < 70:

            for s in sp:
                s.volume = level

            print("I changed the volume to:", level)
        else:
            print("Volume was too high:", level)

    #elif action == "play_wnyc":
    #    sonos_actions.play_station('wnyc')

    elif action == 'next':
        sonos_actions.playback('next')

    elif action.startswith("station"):
        station = action[8:]
        sonos_actions.play_station(station)

    elif action.startswith("shuffle"):
        artist = action[8:]
        sonos_actions.shuffle(artist)

    elif action == "play_queue":
        queue = master.get_queue()
        if len(queue) != 0:
            master.stop()
            master.play_from_queue(0)

    else:
        print("I have no idea what you said")