error handling for curl

This commit is contained in:
cpu
2023-12-19 19:31:56 +01:00
parent 2e5639726f
commit 97e77892f9
3 changed files with 31 additions and 19 deletions

View File

@@ -12,6 +12,7 @@ else:
TTS_API_URL = "https://tts.virtonline.eu/api/tts"
BUTTON_TIMEOUT = 30 # button release timeout terminates playing
FILENAME = "presny_cas.wav"
PIN = 27 # listen to changes on this GPIO pin
GPIO.setmode(GPIO.BCM) # use BCM pin layout
GPIO.setwarnings(False)
@@ -29,8 +30,8 @@ def terminate_playing(proc):
def play_presny_cas():
print("Playing presny_cas.wav")
proc = subprocess.Popen(["aplay", "presny_cas.wav"])
print(f"Playing {FILENAME}")
proc = subprocess.Popen(["aplay", FILENAME])
wait_for_button_release_or_timeout(PIN, BUTTON_TIMEOUT)
print("GPIO went HIGH - terminating playing")
terminate_playing(proc)
@@ -51,23 +52,32 @@ def get_credentials():
def generate_presny_cas():
text = get_datetime_as_slovak_sentence(datetime.now())
print(f"Generating presny_cas.wav for '{text}' using a remote API call")
print(f"Calling remote TTS API for '{text}'")
username, password = get_credentials()
subprocess.call(
[
"curl",
"--silent",
"-u",
f"{username}:{password}",
"--get",
"--data-urlencode",
f"text={text}",
TTS_API_URL,
"--output",
"presny_cas.wav",
]
command = [
"curl",
"-s",
"-w",
"%{http_code}",
"-u",
f"{username}:{password}",
"--get",
"--data-urlencode",
f"text={text}",
TTS_API_URL,
"--output",
FILENAME,
]
# print(f"Executing: {' '.join(command)}")
result = subprocess.run(
command,
check=True,
capture_output=True,
text=True,
)
print("Generated presny_cas.wav")
if result.stdout != "200":
raise Exception(f"API call failed with status code {result.stdout}")
print(f"Generated {FILENAME}")
def wait_for_button_release_or_timeout(pin, timeout):