def execute_match(cmd): log.debug("Executing...") # try: # check if command exists as python script if commands.__contains__(cmd["name"]): log.debug("Replying...") commands.get(cmd["name"]).ex(cmd) elif cmd["name"] == "google": log.debug("Already executed...") else: log.error("Couldn't match command script") # type error: no command found except TypeError: tts.say( replying.get_reply(["matching", "fail"], system=True, module=True))
def ex(cmd): # weather request url url = "http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&appid={1}&lang={2}&units=metric&cnt=1".format(settings.LOCATION, settings.WEATHER_API_KEY, settings.LANGUAGE_SHORT) # get the source code from the url data = requests.get(url) # convert json content = json.loads(data.text) # get data try: # test if there's an error message log.error(content["message"]) except: temp = content["main"]["temp"] temp_max = content["main"]["temp_max"] desc = content["weather"][0]["description"] tts.say(replying.get_reply("weather").format(settings.LOCATION, temp, desc, temp_max))
def recognize(audio): output = None # recognize microphone input using selected speech engine log.debug("Recognizing audio...") # check speech engine if settings.STT_ENGINE == "google": # try recognizing audio from google try: # settings query output = recognizer.recognize_google(audio, language=settings.LANGUAGE) # return output as text except sr.UnknownValueError: log.debug("Speech engine couldn't resolve audio") except sr.RequestError: log.error("You need a WiFi connection for Google STT") except: traceback.print_exc() log.error("Unkown exception") finally: return output
def run(self): # greeting self.greet() # global loop while True: # check if quit if self.stop: break try: # listen for keyword # wake up on recognized keyword if stt.listen_for_keyword(): log.debug("Back in loop...") # listen for text input audio = stt.listen() # try resolving input audio_input = stt.recognize(audio) # check if text input received if not audio_input: log.info("Couldn't resolve audio...") continue else: log.info("Catched input '{}'...".format(audio_input)) # find match cmd = matching.get_match(audio_input) # execute match matching.execute_match(cmd) # user interrupted program except KeyboardInterrupt: log.info("Detected keyboard interruption...") self.quit() break except: log.error("Unexpected error") traceback.print_exc() break
def recognize_for_keyword(): global keyword_detected global new_process audio = listen() # start new process new_process = True log.debug("Recognizing keyword...") try: # recognize input input = recognizer.recognize_google(audio, language=settings.LANGUAGE) # check if keyword in input if settings.KEYWORD in input.lower(): log.debug("Keyword detected") # to stop listening keyword_detected = True else: log.debug("Keyword not detected in '{}'".format(input)) except sr.UnknownValueError: log.debug("Speech engine couldn't resolve audio") except sr.RequestError: log.error("You need a WiFi connection for Google STT") except: log.error("Unkown exception") traceback.print_exc()
def setup(): global engine # tts engine setup # detect OS if platform.system() == "Windows" and settings.TTS_AUTODETECT: try: # import module import win32com.client as win32com engine = win32com.Dispatch("SAPI.SpVoice") except ModuleNotFoundError: log.error("Couldn't find a module named 'win32com.client'") log.error( "Check installation or install via 'pip install pypiwin32'") else: try: # import module from gtts import gTTS engine = gTTS except ModuleNotFoundError: log.error("Couldn't find a module named 'gtts'") log.error("Check installation or install via 'pip install gtts'") log.info("(!) Using slow TTS engine on your OS")
def play_mp3(file): if file.endswith(".mp3"): playsound(file, True) else: log.error("Is not a mp3 file")