import os import signal import subprocess import sqlite3 import time import json get_active_video = r''' SELECT video_url, video_arguments FROM buttons WHERE is_active = 1 LIMIT 1 ''' def play_video(previous_process, video_player_path, player_arguments, video_url): 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 ) def get_active_video_url(db): cursor = db.execute(get_active_video) return cursor.fetchall()[0] with open("config.json", "r") as config_file: config_json = json.loads(config_file.read()) current_process = None current_video = None db = sqlite3.connect(config_json["db_name"]) while True: if current_process is None: current_video = get_active_video_url(db) current_process = play_video( current_process, config_json["video_tool"], current_video[0], current_video[1] ) active_video = get_active_video_url(db) if current_video[0] != active_video[0]: current_video = active_video current_process = play_video( current_process, config_json["video_tool"], current_video[0], current_video[1] ) time.sleep(1)