From 9eb4717f59d6156931506abc2ba778d4c645c038 Mon Sep 17 00:00:00 2001 From: coast Date: Mon, 21 Jul 2025 15:53:13 +0330 Subject: [PATCH] suicide --- local/bin/radio | 57 +++++++++++++++++++++++++++++++---- local/radio | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ local/radio-kill | 20 +++++++++++++ 3 files changed, 148 insertions(+), 6 deletions(-) create mode 100755 local/radio create mode 100755 local/radio-kill diff --git a/local/bin/radio b/local/bin/radio index 5959717..20c369a 100755 --- a/local/bin/radio +++ b/local/bin/radio @@ -1,7 +1,12 @@ #!/usr/bin/env python3 import subprocess import time - +from pathlib import Path +import threading +import json +import tempfile +import urllib.request +import websocket # 128kbps Opus URL="https://radio.animebits.moe/stream/stream128.ogg" # 256kbps Opus @@ -12,15 +17,55 @@ URL="https://radio.animebits.moe/stream/stream128.ogg" #URL="https://radio.animebits.moe/stream/stream192.mp3" # Lossless FLAC #URL="https://radio.animebits.moe/stream/stream.flac" - -#volume -VOL=20 - +#config file & pid +CONF = Path.home() / ".config" / "radiorc" +PID = Path.home() / ".config" / "radio-pid" +with open(CONF) as f: + VOL = f.read().strip() +def notify(title, image_url=None): + icon_path = None + if image_url: + try: + tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png") + urllib.request.urlretrieve(image_url, tmp.name) + icon_path = tmp.name + except: + icon_path = None + args = ["notify-send", title] + if icon_path: + args += ["-i", icon_path] + subprocess.run(args) +def ws_listener(): + def on_message(ws, message): + data = json.loads(message) + if "data" in data and "title" in data["data"]: + title = data["data"]["title"] + image = data["data"].get("album_cover") + notify(title, image) + def on_error(ws, error): + pass + def on_close(ws, close_status_code, close_msg): + time.sleep(1) + run_ws() + def on_open(ws): + pass + def run_ws(): + ws = websocket.WebSocketApp("wss://radio.animebits.moe/api/events/basic", + on_message=on_message, + on_error=on_error, + on_close=on_close, + on_open=on_open) + ws.run_forever() + run_ws() +threading.Thread(target=ws_listener, daemon=True).start() while True: try: print("Playing now...") subprocess.run(["notify-send", "Playing now..."]) - subprocess.run(["mpv", "--no-video", f"--volume={VOL}", URL]) + proc = subprocess.Popen(["mpv", "--no-video", f"--volume={VOL}", URL]) + with open(PID, "w") as f: + f.write(str(proc.pid)) + proc.wait() except KeyboardInterrupt: subprocess.run(["notify-send", "Stopping now..."]) print("\nStopping now...") diff --git a/local/radio b/local/radio new file mode 100755 index 0000000..a697329 --- /dev/null +++ b/local/radio @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import os +import subprocess +import time +from pathlib import Path +import threading +import json +import tempfile +import urllib.request +import websocket +import signal + +URL = "https://radio.animebits.moe/stream/stream128.ogg" +CONF = Path.home() / ".config" / "radiorc" +PID = Path.home() / ".config" / "radio-pid" + +with open(CONF) as f: + VOL = f.read().strip() + +def notify(title, image_url=None): + icon_path = None + if image_url: + try: + tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png") + urllib.request.urlretrieve(image_url, tmp.name) + icon_path = tmp.name + except: + icon_path = None + args = ["notify-send", title] + if icon_path: + args += ["-i", icon_path] + subprocess.run(args) + +def ws_listener(): + def on_message(ws, message): + data = json.loads(message) + if "data" in data and "title" in data["data"]: + title = data["data"]["title"] + image = data["data"].get("album_cover") + notify(title, image) + + def on_error(ws, error): pass + def on_close(ws, close_status_code, close_msg): + time.sleep(1) + run_ws() + def on_open(ws): pass + + def run_ws(): + ws = websocket.WebSocketApp( + "wss://radio.animebits.moe/api/events/basic", + on_message=on_message, + on_error=on_error, + on_close=on_close, + on_open=on_open, + ) + ws.run_forever() + + run_ws() + +threading.Thread(target=ws_listener, daemon=True).start() + +while True: + try: + print("Playing now...") + subprocess.run(["notify-send", "Playing now..."]) + proc = subprocess.Popen(["mpv", "--no-video", f"--volume={VOL}", URL], preexec_fn=os.setsid) + with open(PID, "w") as f: + f.write(str(os.getpgid(proc.pid))) + proc.wait() + except KeyboardInterrupt: + subprocess.run(["notify-send", "Stopping now..."]) + print("\nStopping now...") + break + except Exception as e: + print(f"Something broke... trying again... Error: {e}") + time.sleep(2) + diff --git a/local/radio-kill b/local/radio-kill new file mode 100755 index 0000000..46e7d6e --- /dev/null +++ b/local/radio-kill @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +import subprocess +import sys +import signal + +try: + # Find PID(s) of running 'radio' process (matching name) + out = subprocess.check_output(["pgrep", "-f", "radio"]) + pids = [int(pid) for pid in out.decode().strip().split()] +except subprocess.CalledProcessError: + print("No running 'radio' process found.") + sys.exit(1) + +for pid in pids: + try: + # Send SIGKILL (kill -9) to each found pid + subprocess.run(["kill", "-9", str(pid)]) + print(f"Killed PID {pid}") + except Exception as e: + print(f"Failed to kill PID {pid}: {e}")