dinnerbell/dinnerbell.py

47 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python3
import os
import time
import datetime
import random
import subprocess
import argparse
DEFAULT_WATCH_DIRECTORY = "audio"
def play_audio(file_path):
try:
subprocess.run(["ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet", file_path])
except Exception as e:
print(f"Error playing {file_path}: {e}")
def play_audio_for_current_time(watch_dir):
current_time = datetime.datetime.now().strftime('%H:%M')
file_path = os.path.join(watch_dir, f"{current_time}.wav")
folder_path = os.path.join(watch_dir, current_time)
if os.path.isfile(file_path):
print(f"Playing '{file_path}'")
play_audio(file_path)
elif os.path.isdir(folder_path):
wav_files = [f for f in os.listdir(folder_path) if f.lower().endswith('.wav')]
if wav_files:
selected_file = os.path.join(folder_path, random.choice(wav_files))
print(f"Playing '{selected_file}'")
play_audio(selected_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--directory', default=DEFAULT_WATCH_DIRECTORY, help='Folder to read audio files from')
parser.add_argument('--daemon', action='store_true', help='Run in daemon mode')
args = parser.parse_args()
while True:
play_audio_for_current_time(args.directory)
now = datetime.datetime.now()
seconds_until_next_minute = 60 - now.second
if not args.daemon:
break
print(f"Using audio from path '{args.directory}'")
time.sleep(seconds_until_next_minute)