From c0aa1c83462a72ccc11846740a48b4b13e71c9a4 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Tue, 23 Dec 2025 22:35:11 +0100 Subject: [PATCH] global --- modules/play_sort.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/modules/play_sort.py b/modules/play_sort.py index c7ae9ec..13a79e6 100644 --- a/modules/play_sort.py +++ b/modules/play_sort.py @@ -1,8 +1,5 @@ -import random from . import log95, PlaylistModifierModule, Track, Path -# The module loader injects this variable. -# We assert it to satisfy static analysis and inform runtime about its presence. _log_out: log95.TextIO assert _log_out # pyright: ignore[reportUnboundVariable] @@ -20,11 +17,9 @@ def load_play_counts(file_path: Path) -> dict[str, int]: key, value = line.split(':', 1) counts[key.strip()] = int(value.strip()) except ValueError: - # Ignore lines with invalid formats continue return counts except FileNotFoundError: - # It's okay if the file doesn't exist yet, means no counts have been recorded. return {} class PopularitySorterModule(PlaylistModifierModule): @@ -34,40 +29,52 @@ class PopularitySorterModule(PlaylistModifierModule): """ def __init__(self) -> None: self.logger = log95.log95("PopSort", output=_log_out) - # The play_counter file is located in the parent directory of the 'modules' folder. self.play_counts_file = Path(__file__, "..", "..", "play_counter").resolve() def modify(self, global_args: dict, playlist: list[Track]) -> list[Track]: self.logger.info("Applying popularity-based sorting to the playlist...") - + play_counts = load_play_counts(self.play_counts_file) if not play_counts: self.logger.info("Play counter file not found or is empty. No sorting will be applied.") return playlist - # We will iterate through the playlist, looking at two tracks at a time. + sorted_by_play_count = sorted(play_counts.items(), key=lambda item: item[1], reverse=True) + + top_paths = {path for path, count in sorted_by_play_count[:10]} + least_top_paths = {path for path, count in sorted_by_play_count[-10:]} + for a,b in zip(top_paths, least_top_paths): + a_track = b_track = None + a_i = b_i = 0 + for a_i, a_track in enumerate(playlist): + if not a_track.official: continue + if a_track.path == a: break + if not a_track: continue + for b_i, b_track in enumerate(playlist): + if not b_track.official: continue + if b_track.path == b: break + if not b_track: continue + if a_i < b_i: + playlist[a_i], playlist[b_i] = playlist[b_i], playlist[a_i] + i = 0 while i < len(playlist) - 1: track1 = playlist[i] track2 = playlist[i+1] - if not (track1.official and track2.official): + if not (track1.official and track2.official): i += 2 continue - # Get play counts for both tracks, defaulting to 0 if not found. count1 = play_counts.get(track1.path.as_posix(), 0) count2 = play_counts.get(track2.path.as_posix(), 0) - # If the first track has been played more than the second, swap them. if count1 > count2: playlist[i], playlist[i+1] = track2, track1 - # Move to the next pair of tracks. i += 2 self.logger.info("Popularity sorting complete.") return playlist -# The radioPlayer will look for a 'playlistmod' variable to load the module. playlistmod = (PopularitySorterModule(), 2) \ No newline at end of file