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

2
.gitignore vendored
View File

@@ -1 +1,3 @@
__pycache__ __pycache__
credentials.txt
presny_cas.wav

View File

@@ -97,10 +97,10 @@ Install the systemd service file
> `sudo mv kukuc-clock.service /etc/systemd/system/` > `sudo mv kukuc-clock.service /etc/systemd/system/`
Reload systemd manager configuration Reload systemd manager configuration
> `systemctl daemon-reload` > `sudo systemctl daemon-reload`
Enable the `kukuc-clock` service at boot time and also run it Enable the `kukuc-clock` service at boot time and also run it
> `systemctl enable --now kukuc-clock` > `sudo systemctl enable --now kukuc-clock`
If it does not work check logs If it does not work check logs
> `journalctl -f -u kukuc-clock` > `journalctl -f -u kukuc-clock`

View File

@@ -12,6 +12,7 @@ else:
TTS_API_URL = "https://tts.virtonline.eu/api/tts" TTS_API_URL = "https://tts.virtonline.eu/api/tts"
BUTTON_TIMEOUT = 30 # button release timeout terminates playing BUTTON_TIMEOUT = 30 # button release timeout terminates playing
FILENAME = "presny_cas.wav"
PIN = 27 # listen to changes on this GPIO pin PIN = 27 # listen to changes on this GPIO pin
GPIO.setmode(GPIO.BCM) # use BCM pin layout GPIO.setmode(GPIO.BCM) # use BCM pin layout
GPIO.setwarnings(False) GPIO.setwarnings(False)
@@ -29,8 +30,8 @@ def terminate_playing(proc):
def play_presny_cas(): def play_presny_cas():
print("Playing presny_cas.wav") print(f"Playing {FILENAME}")
proc = subprocess.Popen(["aplay", "presny_cas.wav"]) proc = subprocess.Popen(["aplay", FILENAME])
wait_for_button_release_or_timeout(PIN, BUTTON_TIMEOUT) wait_for_button_release_or_timeout(PIN, BUTTON_TIMEOUT)
print("GPIO went HIGH - terminating playing") print("GPIO went HIGH - terminating playing")
terminate_playing(proc) terminate_playing(proc)
@@ -51,23 +52,32 @@ def get_credentials():
def generate_presny_cas(): def generate_presny_cas():
text = get_datetime_as_slovak_sentence(datetime.now()) 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() username, password = get_credentials()
subprocess.call( command = [
[ "curl",
"curl", "-s",
"--silent", "-w",
"-u", "%{http_code}",
f"{username}:{password}", "-u",
"--get", f"{username}:{password}",
"--data-urlencode", "--get",
f"text={text}", "--data-urlencode",
TTS_API_URL, f"text={text}",
"--output", TTS_API_URL,
"presny_cas.wav", "--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): def wait_for_button_release_or_timeout(pin, timeout):