Created
May 28, 2023 09:14
-
-
Save bryal/33ab1abd035dccaa57274d25b3671a6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys, os, socket, subprocess | |
from pathlib import Path | |
STATIONS = [ | |
("radio noden", "https://streamer.radio.co/sb562d9095/listen"), | |
("p1 - sr", "https://sverigesradio.se/topsy/direkt/132-hi-aac.m3u"), | |
("p2 - sr", "https://http-live.sr.se/p2musik-aac-320"), | |
("p3 - sr", "https://sverigesradio.se/topsy/direkt/164-hi-aac.m3u"), | |
("p3 din gata - sr", "https://sverigesradio.se/topsy/direkt/2576-hi-aac.m3u"), | |
("p4 - sr", "https://sverigesradio.se/topsy/direkt/212-hi-aac.m3u"), | |
("classical - kusc", "http://128.aac.pls.kusc.live/"), | |
("lush - soma fm", "http://somafm.com/lush130.pls"), | |
("sonic universe - soma fm", "http://somafm.com/sonicuniverse130.pls"), | |
("cliqhop idm - soma fm", "http://somafm.com/cliqhop130.pls"), | |
("def con radio - soma fm", "http://somafm.com/defcon130.pls"), | |
("the trip - soma fm", "http://somafm.com/thetrip130.pls"), | |
("dub step beyond - soma fm", "http://somafm.com/dubstep130.pls"), | |
("space station soma - soma fm", "http://somafm.com/spacestation130.pls"), | |
("deep space one - soma fm", "http://somafm.com/deepspaceone130.pls"), | |
("game - rainwave", "https://rainwave.cc/tune_in/1.ogg.m3u"), | |
("oc remix - rainwave", "https://rainwave.cc/tune_in/2.ogg.m3u"), | |
] | |
rundir = Path(f"/run/user/{os.getuid()}/radijo") | |
if not os.path.exists(rundir): | |
os.makedirs(rundir) | |
mpv_input_path = rundir / "mpv-input" | |
current_station_path = rundir / "current-station" | |
mpv = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
try: | |
mpv.connect(str(mpv_input_path)) | |
connected = True | |
except Exception as e: | |
connected = False | |
def notify(title, body=""): | |
p = rundir / "notification-id" | |
try: | |
with open(p, "r") as f: | |
prev = ["-r", str(int(f.read().strip()))] | |
except Exception as e: | |
print(f"exception: {e}") | |
prev = [] | |
subprocess.Popen(["notify-send", "-p", *prev, title, body], stdout=open(p, "w")) | |
def load_station(): | |
try: | |
with open(current_station_path, "r") as f: | |
return int(f.read()) % len(STATIONS) | |
except: | |
return 0 | |
def store_station(i): | |
with open(current_station_path, "w") as f: | |
f.write(str(i)) | |
def toggle(): | |
if connected: | |
mpv.sendall(b"quit\n") | |
else: | |
play(load_station()) | |
def play(i=0): | |
name, uri = STATIONS[i] | |
notify(f"{name} ⋆ RadiJo") | |
store_station(i) | |
if connected: | |
mpv.sendall(f"loadfile {uri}\n".encode()) | |
else: | |
os.execvp("mpv", ["--audio-samplerate=48000", "--no-video", "--no-msg-color", f"--input-ipc-server={mpv_input_path}", uri]) | |
if len(sys.argv) == 1: | |
print("error: missing arg") | |
exit(1) | |
match sys.argv[1]: | |
case "toggle": | |
toggle() | |
case "play": | |
names = [name for name, _ in STATIONS] | |
target = subprocess.run(["bemenu"], input="\n".join(names).encode(), capture_output=True).stdout.decode().strip() | |
play(names.index(target)) | |
case "next": | |
play((load_station() + 1) % len(STATIONS)) | |
case "prev": | |
play((load_station() - 1) % len(STATIONS)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment