Exemplo n.º 1
0
 def on_play_random_mix(self, intent, session, pid=None):
     server = self._server
     try:
         slots = [
             v.get('value') for k, v in intent['slots'].items()
             if k.endswith('Genre')
         ]
         print_d("Extracted genre slots: {slots}", slots=slots)
     except KeyError:
         print_d("Couldn't process genres from: {intent}", intent=intent)
     else:
         lms_genres = self._genres_from_slots(slots, server.genres)
         if lms_genres:
             server.play_genres(lms_genres, player_id=pid)
             gs = english_join(sanitise_text(g) for g in lms_genres)
             text = _("Playing mix of {genres}").format(genres=gs)
             return self.smart_response(text=text, speech=text)
         else:
             genres_text = english_join(slots, _("or"))
             text = _("Don't understand requested genres {genres}").format(
                 genres=genres_text)
             speech = _("Can't find genres: {genres}").format(
                 genres=genres_text)
             return self.smart_response(text=text, speech=speech)
     err_text = "Don't understand intent '{intent}'".format(intent=intent)
     raise ValueError(err_text)
Exemplo n.º 2
0
    def on_launch(self, launch_request, session):

        print_d("Entering interactive mode for sessionId={}",
                session['sessionId'])
        speech_output = _("Squeezebox is online. Please try some commands.")
        reprompt_text = _("Try resume, pause, next, previous, play some jazz, "
                          "or ask Squeezebox to turn it up or down")
        return speech_response("Welcome", speech_output, reprompt_text,
                               end=False)
Exemplo n.º 3
0
 def handle(self, event, context=None):
     request = event['request']
     req_type = request['type']
     if req_type.startswith('AudioPlayer'):
         print_d("Ignoring %s callback %s" %
                 (request['type'], request['requestId']))
         self.touch_audio()
         return _build_response({})
     return super(SqueezeAlexa, self).handle(event, context)
Exemplo n.º 4
0
    def on_intent(self, intent_request, session):
        intent = intent_request['intent']
        intent_name = intent['name']
        pid = self.player_id_from(intent)
        print_d("Received %s: %s (for player %s)" % (intent_name, intent, pid))

        intent_handler = handler.for_name(intent_name)
        if intent_handler:
            return intent_handler(self, intent, session, pid=pid)
        return self.language_response('unknown_intent', [intent_name])
Exemplo n.º 5
0
 def genres_from(g):
     if not g:
         return set()
     res = process.extract(g, genres)[:MAX_GUESSES_PER_SLOT]
     print_d("Raw genre results: {}", res)
     for g, c in res:
         # Exact(ish) matches shouldn't allow other genres
         if c > MinConfidences.SINGLE_GENRE:
             return {g}
     return {g for g, c in res
             if g and int(c) >= MinConfidences.MULTI_GENRE}
Exemplo n.º 6
0
    def on_intent(self, intent_request, session):
        intent = intent_request['intent']
        intent_name = intent['name']
        pid = self.player_id_from(intent)
        print_d("Received {intent_name}: {intent} (for player {pid})",
                **locals())

        intent_handler = handler.for_name(intent_name)
        if intent_handler:
            return intent_handler(self, intent, session, pid=pid)
        speech = _("Sorry, I don't know how to process a \"{intent}\"").format(
            intent=intent_name)
        text = _("Unknown intent: '{intent}'").format(intent=intent_name)
        return self.smart_response(speech=speech, text=text)
Exemplo n.º 7
0
 def player_id_from(self, intent, defaulting=True):
     srv = self.get_server()
     try:
         player_name = intent['slots']['Player']['value']
     except KeyError:
         pass
     else:
         by_name = {s.name: s for s in srv.players.values()}
         result = process.extractOne(player_name, by_name.keys())
         print_d("%s was the best guess for '%s' from %s" %
                 (result, player_name, by_name.keys()))
         if result and int(result[1]) >= MinConfidences.PLAYER:
             return by_name.get(result[0]).id
     return srv.cur_player_id if defaulting else None
Exemplo n.º 8
0
 def player_id_from(self, intent, defaulting=True):
     srv = self._server
     try:
         player_name = intent['slots']['Player']['value']
     except KeyError:
         pass
     else:
         by_name = {s.name: s for s in srv.players.values()}
         choices = by_name.keys()
         result = process.extractOne(player_name, choices)
         print_d("{guess} was the best guess for '{value}' from {choices}",
                 guess=result,
                 value=player_name,
                 choices=set(choices))
         if result and int(result[1]) >= MinConfidences.PLAYER:
             return by_name.get(result[0]).id
     return srv.cur_player_id if defaulting else None
Exemplo n.º 9
0
 def on_set_vol_percent(self, intent, session, pid=None):
     try:
         vol = int(float(intent['slots']['Volume']['value']))
         print_d("Extracted volume slot: %d".format(vol))
     except KeyError:
         print_d("Couldn't process volume from: {}", intent)
         desc = _("Select a volume value between 0 and 100 percent")
         heading = _("Invalid volume value")
         return self.smart_response(text=heading, speech=desc)
     if (vol > 100) or (vol < 0):
         desc = _("Select a volume value between 0 and 100 percent")
         heading = _("Volume value out of range: %d percent").format(vol)
         return self.smart_response(text=heading, speech=desc)
     self._server.set_volume(vol, pid)
     desc = _("OK")
     heading = _("Set Volume to %d percent").format(vol)
     return self.smart_response(text=heading, speech=desc)
Exemplo n.º 10
0
 def on_set_vol(self, intent, session, pid=None):
     try:
         vol = float(intent['slots']['Volume']['value'])
         print_d("Extracted volume slot: {:1f}", vol)
     except KeyError:
         print_d("Couldn't process volume from: {!s}", intent)
         desc = _("Select a volume value between 0 and 10")
         heading = _("Invalid volume value")
         return self.smart_response(text=heading, speech=desc)
     if (vol > 10) or (vol < 0):
         desc = _("Select a volume value between 0 and 10")
         heading = _("Volume value out of range: {volume}").format(vol)
         return self.smart_response(text=heading, speech=desc)
     self._server.set_volume(vol * 10, pid)
     desc = "OK"
     vol_out = vol if (vol != int(vol)) else int(vol)
     heading = _("Set volume to {}").format(vol_out)
     return self.smart_response(text=heading, speech=desc)
Exemplo n.º 11
0
 def get_server(cls):
     """
     :return a Server instance
     :rtype Server
     """
     if not cls._server or cls._server.is_stale():
         sslw = SslSocketWrapper(hostname=SERVER_HOSTNAME,
                                 port=SERVER_SSL_PORT,
                                 ca_file=CA_FILE_PATH,
                                 cert_file=CERT_FILE_PATH,
                                 verify_hostname=VERIFY_SERVER_HOSTNAME)
         cls._server = Server(sslw,
                              user=SERVER_USERNAME,
                              password=SERVER_PASSWORD,
                              cur_player_id=DEFAULT_PLAYER,
                              debug=DEBUG_LMS)
         print_d("Created %r" % cls._server)
     else:
         print_d("Reusing cached %r" % cls._server)
     return cls._server
Exemplo n.º 12
0
 def on_play_artist(self, intent, session, pid=None):
     server = self._server
     try:
         artist = intent['slots']['Artist']['value']
         print_d("Extracted artist slot: {}".format(artist))
     except KeyError:
         print_d("Couldn't process artist from: {intent}", intent=intent)
         return speech_response(text=_("Couldn't process that artist. "))
     else:
         matched_artists = server.search_for_artist(artist)
         print_d(
             "Found Matching artists on squeezebox server "
             "{matched_artists}",
             matched_artists=matched_artists)
         try:
             server.play_artist(matched_artists[0], player_id=pid)
             artist_name = sanitise_text(matched_artists[0])
             text = _(
                 "Playing albums by \"{artist_name}\" on squeezebox".format(
                     artist_name=artist_name))
             return self.smart_response(text=text, speech=text)
         except KeyError:
             return speech_response(
                 text=_("Couldn't find the artist on squeezebox. "))
     raise ValueError("Don't understand intent '{}'".format(intent))
Exemplo n.º 13
0
 def on_play_playlist(self, intent, session, pid=None):
     server = self._server
     try:
         slot = intent['slots']['Playlist']['value']
         print_d("Extracted playlist slot: {slot}", slot=slot)
     except KeyError:
         print_d("Couldn't process playlist from: {intent}", intent=intent)
         if not server.playlists:
             return speech_response(text=_("There are no playlists"))
         pl = random.choice(server.playlists)
         text = _("Didn't hear a playlist there. "
                  "You could try the \"{name}\" playlist?").format(name=pl)
         return speech_response(text=text)
     else:
         if not server.playlists:
             return speech_response(text=_("No Squeezebox playlists found"))
         result = process.extractOne(slot, server.playlists)
         print_d("{guess} was the best guess for '{slot}' from {choices}",
                 guess=str(result),
                 slot=slot,
                 choices=server.playlists)
         if result and int(result[1]) >= MinConfidences.PLAYLIST:
             pl = result[0]
             server.playlist_resume(pl, player_id=pid)
             name = sanitise_text(pl)
             return self.smart_response(
                 speech=_("Playing \"{name}\" playlist").format(name=name),
                 text=_("Playing \"{name}\" playlist").format(name=name))
         pl = random.choice(server.playlists)
         template = _("Couldn't find a playlist matching \"{name}\"."
                      "How about the \"{suggestion}\" playlist?")
         return speech_response(template.format(name=slot, suggestion=pl))
Exemplo n.º 14
0
 def on_play_playlist(self, intent, session, pid=None):
     server = self.get_server()
     try:
         slot = intent['slots']['Playlist']['value']
         print_d("Extracted playlist slot: %s" % slot)
     except KeyError:
         print_d("Couldn't process playlist from: %s" % intent)
         if not server.playlists:
             return self.language_response('play_playlist_nh_none')
         return self.language_response('play_playlist_nh',
                                       [random.choice(server.playlists)])
     else:
         if not server.playlists:
             return self.language_response('play_playlist_none')
         result = process.extractOne(slot, server.playlists)
         print_d("%s was the best guess for '%s' from %s" %
                 (result, slot, server.playlists))
         if result and int(result[1]) >= MinConfidences.PLAYLIST:
             pl = result[0]
             server.playlist_resume(pl, player_id=pid)
             name = sanitise_text(pl)
             return self.language_response('play_playlist', [name])
         return self.language_response(
             'play_playlist_nf',
             [slot, random.choice(server.playlists)])
Exemplo n.º 15
0
 def on_play_random_mix(self, intent, session, pid=None):
     server = self.get_server()
     try:
         slots = [
             v.get('value') for k, v in intent['slots'].items()
             if k.endswith('Genre')
         ]
         print_d("Extracted genre slots: %s" % slots)
     except KeyError:
         print_d("Couldn't process genres from: %s" % intent)
         pass
     else:
         lms_genres = self._genres_from_slots(slots, server.genres)
         if lms_genres:
             server.play_genres(lms_genres, player_id=pid)
             gs = english_join(sanitise_text(g) for g in lms_genres)
             return self.language_response('play_random_mix', [gs])
         else:
             genres_text = english_join(slots, "or")
             return self.language_response('play_random_mix_nf',
                                           [genres_text])
     raise ValueError("Don't understand intent '%s'" % intent)
Exemplo n.º 16
0
 def on_set_vol(self, intent, session, pid=None):
     srv = self.get_server()
     srv.refresh_status()
     try:
         vol = float(intent['slots']['Volume']['value'])
         print_d("Extracted playlist slot: %s" % vol)
     except KeyError:
         print_d("Couldn't process volume from: %s" % intent)
         return self.language_response('set_vol_nf')
     if (vol > 10) or (vol < 0):
         print_d("Volume value out of range: %f" % vol)
         return self.language_response('set_vol_nf')
     self.get_server().set_volume(vol * 10, pid)
     return self.language_response('set_vol', [int(vol)])
Exemplo n.º 17
0
 def on_session_started(self, request, session):
     print_d("Starting new session %s for request %s" %
             (session['sessionId'], request['requestId']))
Exemplo n.º 18
0
 def on_session_started(self, request, session):
     print_d("Starting new session {session} for request {request}",
             session=session['sessionId'],
             request=request['requestId'])
Exemplo n.º 19
0
 def on_session_ended(self, intent, session):
     print_d("Session {id} ended", id=session['sessionId'])
     speech_output = _("Hasta la vista. Baby.")
     return speech_response("Session Ended", speech_output, end=True)
Exemplo n.º 20
0
    def on_launch(self, launch_request, session):

        print_d("Entering interactive mode for sessionId=%s" %
                session['sessionId'])
        return self.language_response('launch', end=False)
Exemplo n.º 21
0
 def __init__(self, server=None, app_id=None):
     super(SqueezeAlexa, self).__init__(app_id)
     if server:
         print_d("Overriding class server for testing")
         SqueezeAlexa._server = server
Exemplo n.º 22
0
 def on_session_ended(self, intent, session):
     print_d("Session %s ended" % session['sessionId'])
     return self.language_response('session_ended')