fix no shuffle and also add reload

This commit is contained in:
Kuba
2025-07-27 17:22:57 +02:00
committed by GitHub
parent cd2efd7122
commit 70757da31d
+59 -18
View File
@@ -96,6 +96,18 @@ def get_newest_track(tracks):
return newest_track return newest_track
def check_control_files():
"""Check for control files and return action to take"""
if can_delete_file("/tmp/radioPlayer_quit"):
os.remove("/tmp/radioPlayer_quit")
return "quit"
if can_delete_file("/tmp/radioPlayer_reload"):
os.remove("/tmp/radioPlayer_reload")
return "reload"
return None
def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=False, do_shuffle=True): def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=False, do_shuffle=True):
last_modified_time = get_playlist_modification_time(playlist_path) last_modified_time = get_playlist_modification_time(playlist_path)
tracks = load_playlist(playlist_path) tracks = load_playlist(playlist_path)
@@ -113,6 +125,7 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
if do_shuffle: random.shuffle(tracks) if do_shuffle: random.shuffle(tracks)
tracks.insert(0, newest_track) tracks.insert(0, newest_track)
else: else:
if do_shuffle:
random.shuffle(tracks) random.shuffle(tracks)
for track in tracks: for track in tracks:
@@ -152,9 +165,13 @@ def play_playlist(playlist_path, custom_playlist: bool=False, play_newest_first=
subprocess.run(['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet', track_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet', track_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if can_delete_file("/tmp/radioPlayer_quit"): # Check control files after each song
os.remove("/tmp/radioPlayer_quit") action = check_control_files()
if action == "quit":
exit() exit()
elif action == "reload":
print("Reload requested, restarting with new arguments...")
return "reload"
def can_delete_file(filepath): def can_delete_file(filepath):
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
@@ -162,7 +179,8 @@ def can_delete_file(filepath):
directory = os.path.dirname(os.path.abspath(filepath)) or '.' directory = os.path.dirname(os.path.abspath(filepath)) or '.'
return os.access(directory, os.W_OK | os.X_OK) return os.access(directory, os.W_OK | os.X_OK)
def main(): def parse_arguments():
"""Parse command line arguments and return configuration"""
arg = sys.argv[1] if len(sys.argv) > 1 else None arg = sys.argv[1] if len(sys.argv) > 1 else None
play_newest_first = False play_newest_first = False
do_shuffle = True do_shuffle = True
@@ -176,19 +194,26 @@ def main():
if arg: if arg:
if arg.lower() == "-h": if arg.lower() == "-h":
print("/tmp/radioPlayer_quit") print("Control files:")
print("/tmp/radioPlayer_arg") print(" /tmp/radioPlayer_quit - Quit the player")
print(" /tmp/radioPlayer_reload - Reload arguments from /tmp/radioPlayer_arg")
print(" /tmp/radioPlayer_arg - Contains arguments to use")
print()
print("Arguments:")
print(" n - Play newest song first")
print(" list:playlist;options - Play custom playlist with options")
print(" /path/to/file - Play specific file first")
exit(95) exit(95)
elif arg.lower() == "n": elif arg.lower() == "n":
play_newest_first = True play_newest_first = True
print("Newest song will be played first") print("Newest song will be played first")
elif arg.startswith("list:"): elif arg.startswith("list:"):
selected_list = arg.removeprefix("list:") selected_list = arg.removeprefix("list:")
print(f"The list {arg} will be played instead of the daily section lists.") print(f"The list {selected_list.split(';')[0]} will be played instead of the daily section lists.")
for arg in selected_list.split(";"): for option in selected_list.split(";"):
if arg == "n": if option == "n":
play_newest_first = True play_newest_first = True
elif arg == "ns": elif option == "ns":
do_shuffle = False do_shuffle = False
selected_list = selected_list.split(";")[0] selected_list = selected_list.split(";")[0]
elif os.path.isfile(arg): elif os.path.isfile(arg):
@@ -197,19 +222,32 @@ def main():
else: else:
print(f"Invalid argument or file not found: {arg}") print(f"Invalid argument or file not found: {arg}")
return arg, play_newest_first, do_shuffle, pre_track_path, selected_list
def main():
while True: # Main reload loop
arg, play_newest_first, do_shuffle, pre_track_path, selected_list = parse_arguments()
if pre_track_path: if pre_track_path:
track_name = os.path.basename(pre_track_path) track_name = os.path.basename(pre_track_path)
print(f"Now playing: {track_name}") print(f"Now playing: {track_name}")
update_rds(track_name) update_rds(track_name)
subprocess.run(['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet', pre_track_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet', pre_track_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if can_delete_file("/tmp/radioPlayer_quit"):
os.remove("/tmp/radioPlayer_quit")
exit()
while True: action = check_control_files()
if action == "quit":
exit()
elif action == "reload":
print("Reload requested, restarting with new arguments...")
continue # Restart the main loop
playlist_loop_active = True
while playlist_loop_active:
if selected_list: if selected_list:
print("Playing custom list") print("Playing custom list")
play_playlist(selected_list, True, play_newest_first, do_shuffle) result = play_playlist(selected_list, True, play_newest_first, do_shuffle)
if result == "reload":
playlist_loop_active = False # Break out to reload
continue continue
current_hour = get_current_hour() current_hour = get_current_hour()
@@ -248,16 +286,19 @@ def main():
if DAY_START <= current_hour < DAY_END: if DAY_START <= current_hour < DAY_END:
print(f"Playing {current_day} day playlist...") print(f"Playing {current_day} day playlist...")
play_playlist(day_playlist, False, play_newest_first, do_shuffle) result = play_playlist(day_playlist, False, play_newest_first, do_shuffle)
elif MORNING_START <= current_hour < MORNING_END: elif MORNING_START <= current_hour < MORNING_END:
print(f"Playing {current_day} morning playlist...") print(f"Playing {current_day} morning playlist...")
play_playlist(morning_playlist, False, play_newest_first, do_shuffle) result = play_playlist(morning_playlist, False, play_newest_first, do_shuffle)
elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END: elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END:
print(f"Playing {current_day} late_night playlist...") print(f"Playing {current_day} late_night playlist...")
play_playlist(late_night_playlist, False, play_newest_first, do_shuffle) result = play_playlist(late_night_playlist, False, play_newest_first, do_shuffle)
else: else:
print(f"Playing {current_day} night playlist...") print(f"Playing {current_day} night playlist...")
play_playlist(night_playlist, False, play_newest_first, do_shuffle) result = play_playlist(night_playlist, False, play_newest_first, do_shuffle)
if result == "reload":
playlist_loop_active = False # Break out to reload
if __name__ == '__main__': if __name__ == '__main__':