import os import signal import subprocess import sqlite3 import time import json get_active_video_sql = r''' SELECT video_url, video_arguments, button_name FROM buttons WHERE is_active = 1 LIMIT 1 ''' def play_video(previous_process, video_player_path, player_arguments, video_url, env): if not previous_process is None: os.killpg(os.getpgid(previous_process.pid), signal.SIGTERM) return subprocess.Popen( "{video_player} {arguments} {video_url}".format( video_player = video_player_path, arguments = player_arguments, video_url = video_url ), stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True, preexec_fn = os.setsid, env = env ) def get_active_video(db): cursor = db.execute(get_active_video_sql) return cursor.fetchall()[0] with open("config.json", "r") as config_file: config_json = json.loads(config_file.read()) my_env = os.environ.copy() my_env["DISPLAY"] = config_json["display_config"] current_process = None current_video = None db = sqlite3.connect(config_json["db_name"]) while True: active_video = get_active_video(db) if current_video is None or current_video[2] != active_video[2]: current_video = active_video if current_video[2] == config_json["disabled_name"] and not current_process is None: os.killpg(os.getpgid(current_process.pid), signal.SIGTERM) current_process = None else: current_process = play_video( current_process, config_json["video_tool"], current_video[0], current_video[1], my_env ) time.sleep(1)