X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=google_assistant.py;h=4a3a58f5947ab335e26ac2fa1c7ce22afd841a93;hb=e8fbbb7306430478dec55d2c963eed116d8330cc;hp=572b4ccdf25644992f77f66eae800ea9e306ce50;hpb=fa4298fa508e00759565c246aef423ba28fedf31;p=python_utils.git diff --git a/google_assistant.py b/google_assistant.py index 572b4cc..4a3a58f 100644 --- a/google_assistant.py +++ b/google_assistant.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 +"""A module to serve as a local client library around HTTP calls to +the Google Assistant via a local gateway.""" + import logging -from typing import NamedTuple -import sys +import warnings +from typing import NamedTuple, Optional import requests import speech_recognition as sr # type: ignore @@ -20,22 +23,24 @@ parser.add_argument( type=str, default="http://kiosk.house:3000", metavar="URL", - help="How to contact the Google Assistant bridge" + help="How to contact the Google Assistant bridge", ) parser.add_argument( "--google_assistant_username", type=str, metavar="GOOGLE_ACCOUNT", default="scott.gasch", - help="The user account for talking to Google Assistant" + help="The user account for talking to Google Assistant", ) class GoogleResponse(NamedTuple): + """A response wrapper.""" + success: bool response: str audio_url: str - audio_transcription: str + audio_transcription: Optional[str] # None if not available. def __repr__(self): return f""" @@ -56,7 +61,7 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: is True, perform speech recognition on the audio response from Google so as to translate it into text (best effort, YMMV). """ - logging.debug(f"Asking google: '{cmd}'") + logging.debug("Asking google: '%s'", cmd) payload = { "command": cmd, "user": config.config['google_assistant_username'], @@ -66,15 +71,16 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: success = False response = "" audio = "" - audio_transcription = "" + audio_transcription: Optional[str] = "" if r.status_code == 200: j = r.json() + logger.debug(j) success = bool(j["success"]) response = j["response"] if success else j["error"] if success: logger.debug('Google request succeeded.') if len(response) > 0: - logger.debug(f"Google said: '{response}'") + logger.debug("Google said: '%s'", response) audio = f"{config.config['google_assistant_bridge']}{j['audio']}" if recognize_speech: recognizer = sr.Recognizer() @@ -90,10 +96,12 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: audio_transcription = recognizer.recognize_google( speech, ) - logger.debug(f"Transcription: '{audio_transcription}'") + logger.debug("Transcription: '%s'", audio_transcription) except sr.UnknownValueError as e: logger.exception(e) - logger.warning('Unable to parse Google assistant\'s response.') + msg = 'Unable to parse Google assistant\'s response.' + logger.warning(msg) + warnings.warn(msg, stacklevel=3) audio_transcription = None return GoogleResponse( success=success, @@ -110,4 +118,3 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: audio_url=audio, audio_transcription=audio_transcription, ) - sys.exit(-1)