From 9c61fcfa3871a9581dc7ab5a64379a2d8b002463 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sun, 14 Dec 2025 11:16:01 +0100 Subject: [PATCH] clear to play --- modules/active_modifier.py | 18 +++++++++++++----- modules/web.html | 4 ++++ modules/web.py | 4 ++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/modules/active_modifier.py b/modules/active_modifier.py index a8e632b..1178fd6 100644 --- a/modules/active_modifier.py +++ b/modules/active_modifier.py @@ -9,6 +9,8 @@ _log_out: TextIO assert _log_out # pyright: ignore[reportUnboundVariable] logger = log95.log95("AC-MOD", output=_log_out) +TOPLAY = Path("/tmp/radioPlayer_toplay") + class Module(ActiveModifier): def __init__(self) -> None: self.playlist = None @@ -34,8 +36,8 @@ class Module(ActiveModifier): if not self.playlist: return (track, next_track), False with self.file_lock: - if not os.path.exists("/tmp/radioPlayer_toplay"): open("/tmp/radioPlayer_toplay", "a").close() - with open("/tmp/radioPlayer_toplay", "r") as f: songs = [s.strip() for s in f.readlines() if s.strip()] + TOPLAY.touch() + with open(TOPLAY, "r") as f: songs = [s.strip() for s in f.readlines() if s.strip()] songs[:] = [f for s in songs for f in glob.glob(s.removeprefix("!")) if os.path.isfile(f)] # expand glob @@ -66,7 +68,7 @@ class Module(ActiveModifier): if not self.originals or self.originals[-1] != track: self.originals.append(track) with self.file_lock: - with open("/tmp/radioPlayer_toplay", "w") as f: + with open(TOPLAY, "w") as f: f.write('\n'.join(songs)) f.write("\n") @@ -118,11 +120,17 @@ class Module(ActiveModifier): songs_to_add = data.get("songs") if isinstance(songs_to_add, list): with self.file_lock: - with open("/tmp/radioPlayer_toplay", "a") as f: + with open(TOPLAY, "a") as f: for song_path in songs_to_add: f.write(f"{song_path}\n") return {"status": "ok", "message": f"{len(songs_to_add)} songs added."} elif data.get("action") == "get_toplay": with self.file_lock: - with open("/tmp/radioPlayer_toplay", "r") as f: return {"status": "ok", "data": [i.strip() for i in f.readlines() if i.strip()]} + with open(TOPLAY, "r") as f: return {"status": "ok", "data": [i.strip() for i in f.readlines() if i.strip()]} + elif data.get("action") == "clear_toplay": + with self.file_lock: + # Due to policy, i will not recommend to strip the next song but only the songs after. + with open(TOPLAY, "r") as f: first_line = f.readline() + with open(TOPLAY, "w") as f: f.write(first_line.strip() + "\n") + return {"status": "ok", "data": [first_line.strip()]} activemod = Module() \ No newline at end of file diff --git a/modules/web.html b/modules/web.html index 7792eb8..1f85019 100644 --- a/modules/web.html +++ b/modules/web.html @@ -74,6 +74,7 @@
+
@@ -371,6 +372,9 @@ ws.send(JSON.stringify({action:"add_to_toplay", songs: [path]})) Array.from(document.getElementById("playlist-box").children).forEach(c => c.classList.remove("selected")); }); + document.getElementById("clear-btn").addEventListener("click", () => { + ws.send(JSON.stringify({action:"clear_toplay"})) + }); document.getElementById("add-to-put-btn").addEventListener("click", () => { let fullPath = null; diff --git a/modules/web.py b/modules/web.py index a53f75d..a2de4cf 100644 --- a/modules/web.py +++ b/modules/web.py @@ -57,6 +57,10 @@ async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: mult result = await get_imc("activemod", {"action": "get_toplay"}) if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) else: await websocket.send(json.dumps({"data": result, "event": "toplay"})) + elif action == "clear_toplay": + result = await get_imc("activemod", {"action": "clear_toplay"}) + if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) + else: await websocket.send(json.dumps({"data": result, "event": "toplay"})) # Yes, this is not an accident elif action == "request_state": # supports requesting specific parts if provided what = msg.get("what", "")