error handling for curl
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
__pycache__
|
||||
credentials.txt
|
||||
presny_cas.wav
|
||||
|
||||
@@ -97,10 +97,10 @@ Install the systemd service file
|
||||
> `sudo mv kukuc-clock.service /etc/systemd/system/`
|
||||
|
||||
Reload systemd manager configuration
|
||||
> `systemctl daemon-reload`
|
||||
> `sudo systemctl daemon-reload`
|
||||
|
||||
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
|
||||
> `journalctl -f -u kukuc-clock`
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user