def handle(self): """ @see Handler.handle() """ try: # Make the change, capping at min and max cur = get_volume() new = max(MIN_VOLUME, min(MAX_VOLUME, cur + self._delta)) # Any change? if cur != new: # Acknowledge that it happened set_volume(new) direction = "Up" if self._delta > 0 else "Down" return Result(self, direction, False, True) else: # Nothing to do return None except Exception: LOG.error("Problem setting changing the volume by %s:\n%s" % (self._delta, traceback.format_exc())) return Result(self, "Sorry, there was a problem changing the volume", False, True)
def handle(self): ''' @see Handler.handle() ''' try: LOG.info("Querying Wikipedia for '%s'" % (self._thing, )) summary = wikipedia.summary(self._thing) except Exception as e: LOG.error("Failed to query Wikipedia about '%s': %s" % (self._thing, e)) return Result( self, "Sorry, there was a problem asking Wikipedia about %s" % (self._thing, ), False, True) # Anything? if summary is None or len(summary.strip()) == 0: return None # Strip the summary down a little, some of these can be pretty # long. First just grab the first paragraph. Next, stop after about # 400 chars. shortened = summary.split('\n')[0] if '. ' in shortened and len(shortened) > 400: index = shortened.index('. ') shortened = shortened[:index + 1] # And give it back. We use a period after "says" here so that the speech # output will pause appropriately. It's not good gramma though. Since we # got back a result then we mark ourselves as exclusive; there is # probably not a lot of point in having others also return information. return Result(self, "Wikipedia says.\n%s" % shortened, False, True)
def handle(self): """ @see Handler.handle() """ try: result = self.service.meaning(self._word) except: return Result(self, "Sorry, I don't know the word %s" % self._word, False, True) # The result will contain lists keyed by word type response = "The word %s has the following definitions: " % self._word for type_ in result.keys(): # Get back the list of definitlions, making it no more than the # given limit defns = result[type_][:self._limit] for defn in defns: # Make some tweaks to the definition defn = defn.lower() defn = defn.replace('e.g.', ' for example ') # And add it, along with its type response += "%s: %s. " % (type_, defn) # And give it all back return Result(self, response, False, True)
def handle(self): ''' @see Handler.handle() ''' try: value = parse_number(self._volume) LOG.info("Got value of %s from %s" % (value, self._volume)) if value < 0 or value > 11: # Bad value return Result( self, "Sorry, volume needs to be between zero and eleven", False, True) else: # Acknowledge that it happened set_volume(value) return Result(self, "Okay, volume now %s" % (value, ), False, True) except Exception: LOG.error("Problem parsing volume '%s':\n%s" % (self._volume, traceback.format_exc())) return Result( self, "Sorry, I don't know how to set the volume to %s" % (self._volume, ), False, True)
def handle(self): """ @see Handler.handle() """ try: value = parse_number(self._volume) LOG.info("Got value of %s from %s" % (value, self._volume)) if value < MIN_VOLUME or value > MAX_VOLUME: # Bad value return Result( self, "Sorry, volume needs to be between %d and %d" % (MIN_VOLUME, MAX_VOLUME), False, True) else: # Acknowledge that it happened set_volume(value) return Result(self, "Okay, volume now %s" % (value, ), False, True) except Exception: LOG.error("Problem parsing volume '%s':\n%s" % (self._volume, traceback.format_exc())) return Result( self, "Sorry, I don't know how to set the volume to %s" % (self._volume, ), False, True)
def handle(self): """ @see Handler.handle()` """ try: self.service.unpause() return Result(self, '', False, True) except: return Result(self, '', False, False)
def handle(self): """ @see Handler.handle()` """ try: was_playing = self.service.is_playing() self.service.pause() return Result(self, '', False, was_playing) except: return Result(self, '', False, False)
def handle(self): """ @see Handler.handle() """ # Just cancel them all for now try: self.service.cancel_timer() return Result(self, "All timers cancelled", False, True) except Exception as e: return Result(self, "Sorry, I don't know how to cancel that timer", False, True)
def handle(self): """ @see Handler.handle()` """ try: if self.service.is_playing(): self.service.pause() else: self.service.unpause() return Result(self, '', False, True) except Exception as e: return Result(self, '', False, False)
def handle(self): """ @see Handler.handle() """ data = self._get_data() where = data.get('DEVICE_LOCATIONTYPE', '') # We can look to derive the AQI from this value = data.get('PM2_5Value') if value is not None: # This is a very rough approximation to the AQI from the PM2_5Value value = float(value) aqi = value * value / 285 if self._raw: what = "The air quality index %s is %d." % (where, aqi) else: if aqi < 50: quality = "okay" elif aiq < 100: quality = "acceptable" elif aiq < 150: quality = "poor" elif aiq < 200: quality = "bad" elif aiq < 250: quality = "hazardous" else: quality = "extremely hazardous" what = "The air quality %s is %s" % (where, quality) else: what = "The air quality %s is unknown" % (where, ) # And give it back return Result(self, what, False, True)
def handle(self): ''' @see Handler.handle()` ''' LOG.info('Playing %s' % (self._what)) self.service.play(self._filenames) return Result(self, '', False, True)
def handle(self): ''' @see Handler.handle()` ''' was_playing = self.service.is_playing() self.service.pause() return Result(self, '', False, was_playing)
def handle(self): ''' @see Handler.handle() ''' # Get the time in the local timezone and render the component parts that # we care about now = time.localtime(time.time()) hh = time.strftime("%I", now) mm = time.strftime("%M", now) p = time.strftime("%p", now) # We strip any leading zero from the HH, which you expect for a HH:MM # format. For MM we replace it with 'oh' for a more natural response. if hh.startswith('0'): hh = hh.lstrip('0') hh = number_to_words(int(hh)) if mm == '00': mm = '' elif mm.startswith('0'): mm = 'oh %s' % number_to_words(int(mm)) else: mm = number_to_words(int(mm)) # Now we can hand it back return Result(self, "The current time is %s %s %s" % (hh, mm, p), False, True)
def handle(self): """ @see Handler.handle() """ # Get the time in the local timezone and render the component parts that # we care about now = time.localtime(time.time()) hh = time.strftime("%I", now) mm = time.strftime("%M", now) p = time.strftime("%p", now) # We strip any leading zero from the HH, which you expect for a HH:MM # format. For MM we replace it with 'oh' for a more natural response. AM # and PM need to be spelt out so that speech output doesn't say "am" (as # in "I am he!") instead of "ay em". if hh.startswith('0'): hh = hh.lstrip('0') hh = number_to_words(int(hh)) if mm == '00': mm = '' elif mm.startswith('0'): mm = 'oh %s' % number_to_words(int(mm)) else: mm = number_to_words(int(mm)) if p == "AM": p = "ay em" elif p == "PM": p = "pee em" # Now we can hand it back return Result(self, "The current time is %s %s %s" % (hh, mm, p), False, True)
def handle(self): """ @see Handler.handle()` """ was_paused = self.service.is_paused() self.service.unpause() return Result(self, '', False, was_paused)
def handle(self): """ @see Handler.handle()` """ LOG.info('Playing %s' % (self._what)) self.service.play(self._token) return Result(self, '', False, True)
def handle(self): ''' @see Handler.handle() ''' return Result( self, "You said: %s" % ' '.join([token.element for token in self._tokens if token.verbal]), False, False)
def handle(self): """ @see Handler.handle() """ return Result( self, "You got a %d" % random.randint(1, self._sides), False, True )
def handle(self): """ @see Handler.handle() """ return Result( self, "%d" % random.randint(self._start, self._end), False, True )
def handle(self): """ @see Handler.handle() """ result = "heads" if random.randint(0, 1) else "tails" return Result( self, "You got %s" % result, False, True )
def handle(self): """ @see Handler.handle() """ # Just join all the spellings together response = "" for word in self._words: response += "The word %s is spelt %s. " % (word, ' '.join(word)) # And give it all back return Result(self, response, False, True)
def handle(self): """ @see Handler.handle() """ data = self._get_data() where = data.get('DEVICE_LOCATIONTYPE', '') humidity = data.get('humidity') if humidity is not None: what = "%s percent" % (humidity, ) else: what = "unknown" return Result(self, "The humidity %s is %s." % (where, what), False, True)
def handle(self): """ @see Handler.handle()` """ if self.service.is_playing(): self.service.pause() handled = True elif self.service.is_paused(): self.service.unpause() handled = True else: handled = False return Result(self, '', False, handled)
def handle(self): """ @see Handler.handle() """ # This comes in Fahrenheit, one day we'll convert it depending on user # tastes... data = self._get_data() where = data.get('DEVICE_LOCATIONTYPE', '') temperature = data.get('temp_f') if temperature is not None: what = "%s degrees fahrenheit" % (temperature, ) else: what = "unknown" return Result(self, "The temperature %s is %s." % (where, what), False, True)
def handle(self): """ @see Handler.handle() """ result = list() for (phrase, words, (start, end, score)) in self._matches: result.append( "'%s' from [%d:%d] with '%s' scoring %d" % ( ' '.join(phrase), start, end, ' '.join(words), score ) ) return Result( self, "You matched: %s" % '; and '.join(result), False, False )
def handle(self): """ @see Handler.handle() """ # If we got nothing then grumble in a vaguely (un)helpful way if len(self._times) == 0: return Result(self, "I'm sorry, I didn't catch that", False, True) # Look for the times in the words try: # Try to find the various units of time in something like: # seven hours and fifteen days indices = [] for (words, seconds) in _PERIODS: for word in words: if word in self._times: index = self._times.index(word) LOG.info("Found '%s' at index %d" % (word, index)) indices.append((index, seconds)) # Anything? if len(indices) == 0: raise ValueError("Found no units in %s" % ' '.join(self._times)) # Put them in order and look for the numbers, accumulating into the # total total = 0 indices = sorted(indices, key=lambda pair: pair[0]) prev = 0 for (index, seconds) in indices: # Get the words leading up to the time unit, these should be the # number (possibly with other bits) value = self._times[prev:index] # Strip off any "and"s while value[0] == "and": value = value[1:] # Now try to parse it LOG.info("Parsing %s" % (value, )) amount = parse_number(' '.join(value)) # Accumulate total = amount * seconds LOG.info("Got value of %s seconds from %s" % (total, self._times)) # Handle accordingly if total > 0: # That's a valid timer value. Set the timer and say we did it. self.service.add_timer(total) return Result( self, "Okay, timer set for %s" % (' '.join(self._times), ), False, True) else: # We can't have an alarm in the past return Result( self, "%s was not a time in the future" % (' '.join(self._times), ), False, True) except Exception: LOG.error("Problem parsing timer '%s':\n%s" % (self._times, traceback.format_exc())) return Result( self, "Sorry, I don't know how to set the timer for %s" % (' '.join(self._times), ), False, True)
def handle(self): """ @see Handler.handle() """ return Result(self, self._reply, True, False)
def handle(self): """ @see Handler.handle() """ return Result(self, self._fortune, True, False)