projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix a bug in device.py around the type of keywords, add logging and
[python_utils.git]
/
google_assistant.py
diff --git
a/google_assistant.py
b/google_assistant.py
index b34197a3e60d0db9eff5c9cc6609ebe04e581bab..ec5f6a4c85e17ed0fb2eaaf4f2f22d8ee2ebf300 100644
(file)
--- a/
google_assistant.py
+++ b/
google_assistant.py
@@
-1,7
+1,9
@@
#!/usr/bin/env python3
import logging
#!/usr/bin/env python3
import logging
-from typing import NamedTuple
+import sys
+import warnings
+from typing import NamedTuple, Optional
import requests
import speech_recognition as sr # type: ignore
import requests
import speech_recognition as sr # type: ignore
@@
-19,14
+21,14
@@
parser.add_argument(
type=str,
default="http://kiosk.house:3000",
metavar="URL",
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",
)
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"
,
)
)
@@
-34,7
+36,7
@@
class GoogleResponse(NamedTuple):
success: bool
response: str
audio_url: str
success: bool
response: str
audio_url: str
- audio_transcription:
str
+ audio_transcription:
Optional[str] # None if not available.
def __repr__(self):
return f"""
def __repr__(self):
return f"""
@@
-65,7
+67,7
@@
def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
success = False
response = ""
audio = ""
success = False
response = ""
audio = ""
- audio_transcription = ""
+ audio_transcription
: Optional[str]
= ""
if r.status_code == 200:
j = r.json()
success = bool(j["success"])
if r.status_code == 200:
j = r.json()
success = bool(j["success"])
@@
-92,15
+94,23
@@
def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse:
logger.debug(f"Transcription: '{audio_transcription}'")
except sr.UnknownValueError as e:
logger.exception(e)
logger.debug(f"Transcription: '{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
audio_transcription = None
+ return GoogleResponse(
+ success=success,
+ response=response,
+ audio_url=audio,
+ audio_transcription=audio_transcription,
+ )
else:
else:
- logger.error(
- f'HTTP request to {url} with {payload} failed; code {r.status_code}'
+ message = f'HTTP request to {url} with {payload} failed; code {r.status_code}'
+ logger.error(message)
+ return GoogleResponse(
+ success=False,
+ response=message,
+ audio_url=audio,
+ audio_transcription=audio_transcription,
)
)
- return GoogleResponse(
- success=success,
- response=response,
- audio_url=audio,
- audio_transcription=audio_transcription,
- )
+ sys.exit(-1)