From 6c0bb15ba48724484bcf1cb313ec39e58a95e75f Mon Sep 17 00:00:00 2001 From: spartan152 <68029479+spartan152@users.noreply.github.com> Date: Mon, 14 Apr 2025 23:19:35 -0400 Subject: [PATCH] Update player.py Code has been modified to include a play count log, errors if they occur based on shorter than expected playtime or failure to play, and uses successful play count to ensure repeats don't occur until after all have played --- player.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/player.py b/player.py index 92b5d36..d453096 100644 --- a/player.py +++ b/player.py @@ -1,13 +1,34 @@ +#Code has been modified to include a play count log, errors if they occur based on shorter than expected playtime or failure to play, and uses successful play count to ensure repeats don't occur until after all have played + import os import random import time from subprocess import PIPE, Popen, STDOUT directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'videos') +play_counts_file = '/home/pi/play_counts.txt' # Path to store play counts videos = [] +def loadPlayCounts(): + """Load the play counts from a file.""" + play_counts = {} + if os.path.exists(play_counts_file): + with open(play_counts_file, 'r') as f: + for line in f: + video, count = line.strip().split(' ') + play_counts[video] = int(count) + return play_counts + + +def savePlayCounts(play_counts): + """Save the play counts to a file.""" + with open(play_counts_file, 'w') as f: + for video, count in play_counts.items(): + f.write('{} {}\n'.format(video, count)) + + def getVideos(): global videos videos = [] @@ -18,15 +39,27 @@ def getVideos(): def playVideos(): global videos + play_counts = loadPlayCounts() # Load play counts at the start + if len(videos) == 0: getVideos() time.sleep(5) return - random.shuffle(videos) + + # Sort the videos by play count (favoring those with lower counts) + videos.sort(key=lambda x: play_counts.get(x, 0)) # Default to 0 if video not in play_counts + for video in videos: + # Increment the play count of this video + play_counts[video] = play_counts.get(video, 0) + 1 + + # Play the video playProcess = Popen(['omxplayer', '--no-osd', '--aspect-mode', 'fill', video]) playProcess.wait() + # Save updated play counts after each video + savePlayCounts(play_counts) + -while (True): +while True: playVideos()