Add endpoint /random

* selects a random sound and plays it
This commit is contained in:
clemens 2018-01-16 10:08:32 +01:00
parent 5d42b32a94
commit 3a8822784f

View File

@ -1,8 +1,10 @@
import os
import random
import sys
import subprocess
import sqlite3
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, g
import config
@ -147,3 +149,9 @@ ON tag.id = checked.id""", (sound,))
@app.route("/sounds/<path:name>")
def sounds(name):
return send_from_directory(config.path, name)
@app.route("/random")
def play_random():
sounds = [os.fsencode(file).decode() for file in os.listdir(config.path)]
random_sound = random.sample(sounds, 1)
subprocess.Popen(["omxplayer", os.path.join(config.path, random_sound.encode("utf-8")], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)