0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 13:52:00 +01:00
This commit is contained in:
KubaPro010
2025-11-13 15:54:44 +01:00
parent 26122a374a
commit b863d3c124
6 changed files with 25 additions and 35 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -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()

View File

@@ -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)

View File

@@ -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")

View File

@@ -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