From b863d3c124fdc236a0f64b00c6d8555a49f4006f Mon Sep 17 00:00:00 2001 From: KubaPro010 <132459354+KubaPro010@users.noreply.github.com> Date: Thu, 13 Nov 2025 15:54:44 +0100 Subject: [PATCH] bruh --- modules/active_modifier.py | 7 ++----- modules/advisor.py | 31 ++++++++++++++----------------- modules/progress.py | 3 +-- modules/shuffler.py | 2 +- modules/write_playlists.py | 9 +++------ radioPlayer.py | 8 ++++---- 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/modules/active_modifier.py b/modules/active_modifier.py index 02a5f9c..c64cea5 100644 --- a/modules/active_modifier.py +++ b/modules/active_modifier.py @@ -34,10 +34,9 @@ class Module(ActiveModifier): official = True if song.startswith("!"): song = song[1:] - official = False # NOT FLOATINGPOINTERROR + official = False - song = Path(song).absolute() - return song, official + return Path(song).absolute(), official if len(songs): song, official = get_song() @@ -61,7 +60,6 @@ class Module(ActiveModifier): logger.info(f"Playing {song.name} instead, as instructed by toplay") - if len(songs): # There are more tracks on the temp list new_song, new_official = get_song(False) @@ -93,7 +91,6 @@ class Module(ActiveModifier): elif future.day > now.day: # late night goes mid day, as it starts at midnight logger.warning("Skipping track as it the next day") return (None, None), None - return (self.last_track, next_track), False activemod = Module() \ No newline at end of file diff --git a/modules/advisor.py b/modules/advisor.py index bf9593e..ed0cc84 100644 --- a/modules/advisor.py +++ b/modules/advisor.py @@ -5,8 +5,7 @@ DAY_END = 18 LATE_NIGHT_START = 0 LATE_NIGHT_END = 5 -from modules import BaseIMCModule, InterModuleCommunication -from . import PlaylistAdvisor, log95, Path +from . import BaseIMCModule, InterModuleCommunication, PlaylistAdvisor, log95, Path import os, datetime from typing import TextIO @@ -24,25 +23,25 @@ class Time: except OSError: return 0 def check_if_playlist_modifed(playlist_path: Path) -> bool: - current_day, current_hour = datetime.datetime.now().strftime('%A').lower(), datetime.datetime.now().hour - morning_playlist_path = Path(playlist_dir, current_day, "morning").absolute() - day_playlist_path = Path(playlist_dir, current_day, "day").absolute() - night_playlist_path = Path(playlist_dir, current_day, "night").absolute() - late_night_playlist_path = Path(playlist_dir, current_day, "late_night").absolute() + current_day, current_hour = (time := datetime.datetime.now()).strftime('%A').lower(), time.hour if DAY_START <= current_hour < DAY_END: + day_playlist_path = Path(playlist_dir, current_day, "day").absolute() if playlist_path != day_playlist_path: logger.info("Time changed to day hours, switching playlist...") return True elif MORNING_START <= current_hour < MORNING_END: + morning_playlist_path = Path(playlist_dir, current_day, "morning").absolute() if playlist_path != morning_playlist_path: logger.info("Time changed to morning hours, switching playlist...") return True elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END: + late_night_playlist_path = Path(playlist_dir, current_day, "late_night").absolute() if playlist_path != late_night_playlist_path: logger.info("Time changed to late night hours, switching playlist...") return True else: + night_playlist_path = Path(playlist_dir, current_day, "night").absolute() if playlist_path != night_playlist_path: logger.info("Time changed to night hours, switching playlist...") return True @@ -53,6 +52,7 @@ class Module(PlaylistAdvisor): self.last_mod_time = 0 self.last_playlist = None self.class_imc = None + self.custom_playlist = None self.custom_playlist_path = Path("/tmp/radioPlayer_list") self.custom_playlist_last_mod = 0 @@ -66,7 +66,7 @@ class Module(PlaylistAdvisor): return self.custom_playlist elif self.custom_playlist: self.custom_playlist = None - current_day, current_hour = datetime.datetime.now().strftime('%A').lower(), datetime.datetime.now().hour + current_day, current_hour = (time := datetime.datetime.now()).strftime('%A').lower(), time.hour morning_playlist = Path(playlist_dir, current_day, "morning").absolute() day_playlist = Path(playlist_dir, current_day, "day").absolute() @@ -92,35 +92,32 @@ class Module(PlaylistAdvisor): logger.info(f"Playing {current_day} day playlist...") self.last_mod_time = Time.get_playlist_modification_time(day_playlist) self.last_playlist = day_playlist - return day_playlist elif MORNING_START <= current_hour < MORNING_END: logger.info(f"Playing {current_day} morning playlist...") self.last_mod_time = Time.get_playlist_modification_time(morning_playlist) self.last_playlist = morning_playlist - return morning_playlist elif LATE_NIGHT_START <= current_hour < LATE_NIGHT_END: - logger.info(f"Playing {current_day} late_night playlist...") + logger.info(f"Playing {current_day} late night playlist...") self.last_mod_time = Time.get_playlist_modification_time(late_night_playlist) self.last_playlist = late_night_playlist - return late_night_playlist else: logger.info(f"Playing {current_day} night playlist...") self.last_mod_time = Time.get_playlist_modification_time(night_playlist) self.last_playlist = night_playlist - return night_playlist + return self.last_playlist def new_playlist(self) -> bool: if self.custom_playlist and self.custom_playlist_path.exists(): - mod_time = Time.get_playlist_modification_time(self.custom_playlist) - if mod_time > self.custom_playlist_last_mod: + if Time.get_playlist_modification_time(self.custom_playlist) > self.custom_playlist_last_mod: logger.info("Custom playlist changed on disc, reloading...") self.custom_playlist = None return True return False elif self.custom_playlist_path.exists(): return True + if not self.last_playlist: return True + if check_if_playlist_modifed(self.last_playlist): return True - mod_time = Time.get_playlist_modification_time(self.last_playlist) - if mod_time > self.last_mod_time: + if Time.get_playlist_modification_time(self.last_playlist) > self.last_mod_time: logger.info("Playlist changed on disc, reloading...") return True return False diff --git a/modules/progress.py b/modules/progress.py index 716fd55..108a224 100644 --- a/modules/progress.py +++ b/modules/progress.py @@ -9,8 +9,7 @@ def format_time(seconds) -> str: class Module(PlayerModule): def progress(self, index: int, track: Track, elapsed: float, total: float, real_total: float) -> None: if track.official: - data = f"{track.path.name}: {format_time(elapsed)} / {format_time(total)}" - # print(data, end="\r", flush=True) + data = f"{track.path.name}: {format_time(elapsed)} / {format_time(total)}\n" Path("/tmp/radioPlayer_progress").write_text(data) module = Module() \ No newline at end of file diff --git a/modules/shuffler.py b/modules/shuffler.py index e8e196e..297ca53 100644 --- a/modules/shuffler.py +++ b/modules/shuffler.py @@ -5,6 +5,6 @@ from . import PlaylistModifierModule, Track class Module(PlaylistModifierModule): def modify(self, global_args: dict, playlist: list[Track]): if int(global_args.get("no_shuffle", 0)) == 0: random.shuffle(playlist) - return None + return playlist playlistmod = (Module(), 0) \ No newline at end of file diff --git a/modules/write_playlists.py b/modules/write_playlists.py index 2dd2085..51a08fc 100644 --- a/modules/write_playlists.py +++ b/modules/write_playlists.py @@ -8,10 +8,8 @@ assert _log_out # pyright: ignore[reportUnboundVariable] logger = log95.log95("PlayView", output=_log_out) class Module(PlayerModule): - def __init__(self) -> None: - self.playlist = [] - def on_new_playlist(self, playlist: list[Track]): - self.playlist = [str(t.path.absolute()) for t in playlist] + def __init__(self) -> None: self.playlist = [] + def on_new_playlist(self, playlist: list[Track]): self.playlist = [str(t.path.absolute()) for t in playlist] def progress(self, index: int, track: Track, elapsed: float, total: float, real_total: float) -> None: if os.path.exists("/tmp/radioPlayer_skip"): self._imc.send(self, "procman", {"op": 2}) @@ -22,8 +20,7 @@ class Module(PlayerModule): # discrepancy, which means that the playing file was modified by the active modifier # we are playing a file that was not determined in the playlist, that means it was chosen by the active modifier and made up on the fly lines = self.playlist[:index] + [f"> ({track.path})"] + [self.playlist[index]] + self.playlist[index+1:] - else: - lines = self.playlist[:index] + [f"> {self.playlist[index]}"] + self.playlist[index+1:] + else: lines = self.playlist[:index] + [f"> {self.playlist[index]}"] + self.playlist[index+1:] with open("/tmp/radioPlayer_playlist", "w") as f: for line in lines: try: f.write(line + "\n") diff --git a/radioPlayer.py b/radioPlayer.py index 35b5c4e..3469951 100644 --- a/radioPlayer.py +++ b/radioPlayer.py @@ -170,10 +170,9 @@ class RadioPlayer: for (spec, module, module_name) in self.modules: assert spec.loader try: - start = time.monotonic() - time.perf_counter() + start = time.perf_counter() spec.loader.exec_module(module) - time_took = time.monotonic() - start + time_took = time.perf_counter() - start if time_took > 0.2: self.logger.warning(f"{module_name} took {time_took:.1f}s to start") except Exception as e: traceback.print_exc(file=self.logger.output) @@ -215,7 +214,8 @@ class RadioPlayer: playlist: list[Track] = [] [playlist.extend(Track(Path(line).absolute(), True, True, True, args) for line in lns) for (lns, args) in parsed] # i can read this, i think - for module in self.playlist_modifier_modules: playlist = module.modify(global_args, playlist) or playlist # id one liner this but the assignement is stopping me + [(playlist := module.modify(global_args, playlist) or playlist) for module in self.playlist_modifier_modules if module] # yep + prefetch(playlist[0].path) [mod.on_new_playlist(playlist) for mod in self.simple_modules + [self.active_modifier] if mod] # one liner'd everything