error handling for curl
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
__pycache__
|
__pycache__
|
||||||
|
credentials.txt
|
||||||
|
presny_cas.wav
|
||||||
|
|||||||
@@ -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`
|
||||||
|
|||||||
@@ -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,12 +52,13 @@ 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",
|
||||||
"--silent",
|
"-s",
|
||||||
|
"-w",
|
||||||
|
"%{http_code}",
|
||||||
"-u",
|
"-u",
|
||||||
f"{username}:{password}",
|
f"{username}:{password}",
|
||||||
"--get",
|
"--get",
|
||||||
@@ -64,10 +66,18 @@ def generate_presny_cas():
|
|||||||
f"text={text}",
|
f"text={text}",
|
||||||
TTS_API_URL,
|
TTS_API_URL,
|
||||||
"--output",
|
"--output",
|
||||||
"presny_cas.wav",
|
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):
|
||||||
|
|||||||
Reference in New Issue
Block a user