0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 21:53:54 +01:00
This commit is contained in:
2025-12-23 22:35:11 +01:00
parent 6c2558e684
commit c0aa1c8346

View File

@@ -1,8 +1,5 @@
import random
from . import log95, PlaylistModifierModule, Track, Path 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 _log_out: log95.TextIO
assert _log_out # pyright: ignore[reportUnboundVariable] 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) key, value = line.split(':', 1)
counts[key.strip()] = int(value.strip()) counts[key.strip()] = int(value.strip())
except ValueError: except ValueError:
# Ignore lines with invalid formats
continue continue
return counts return counts
except FileNotFoundError: except FileNotFoundError:
# It's okay if the file doesn't exist yet, means no counts have been recorded.
return {} return {}
class PopularitySorterModule(PlaylistModifierModule): class PopularitySorterModule(PlaylistModifierModule):
@@ -34,40 +29,52 @@ class PopularitySorterModule(PlaylistModifierModule):
""" """
def __init__(self) -> None: def __init__(self) -> None:
self.logger = log95.log95("PopSort", output=_log_out) 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() self.play_counts_file = Path(__file__, "..", "..", "play_counter").resolve()
def modify(self, global_args: dict, playlist: list[Track]) -> list[Track]: def modify(self, global_args: dict, playlist: list[Track]) -> list[Track]:
self.logger.info("Applying popularity-based sorting to the playlist...") self.logger.info("Applying popularity-based sorting to the playlist...")
play_counts = load_play_counts(self.play_counts_file) play_counts = load_play_counts(self.play_counts_file)
if not play_counts: if not play_counts:
self.logger.info("Play counter file not found or is empty. No sorting will be applied.") self.logger.info("Play counter file not found or is empty. No sorting will be applied.")
return playlist 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 i = 0
while i < len(playlist) - 1: while i < len(playlist) - 1:
track1 = playlist[i] track1 = playlist[i]
track2 = playlist[i+1] track2 = playlist[i+1]
if not (track1.official and track2.official): if not (track1.official and track2.official):
i += 2 i += 2
continue continue
# Get play counts for both tracks, defaulting to 0 if not found.
count1 = play_counts.get(track1.path.as_posix(), 0) count1 = play_counts.get(track1.path.as_posix(), 0)
count2 = play_counts.get(track2.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: if count1 > count2:
playlist[i], playlist[i+1] = track2, track1 playlist[i], playlist[i+1] = track2, track1
# Move to the next pair of tracks.
i += 2 i += 2
self.logger.info("Popularity sorting complete.") self.logger.info("Popularity sorting complete.")
return playlist return playlist
# The radioPlayer will look for a 'playlistmod' variable to load the module.
playlistmod = (PopularitySorterModule(), 2) playlistmod = (PopularitySorterModule(), 2)