diff options
| author | Arslaan Pathan <[email protected]> | 2026-10-06 12:54:19 +1300 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2026-10-06 12:54:19 +1300 |
| commit | cc312e9054fd7a39a8937762247a1c72281c55e5 (patch) | |
| tree | adb587d01a93a7baf7411494cc50b51d824171e8 | |
| parent | 2799880278cbd24210d4c869baf5f9c4f737196d (diff) | |
| download | remote-automation-cc312e9054fd7a39a8937762247a1c72281c55e5.tar.xz remote-automation-cc312e9054fd7a39a8937762247a1c72281c55e5.zip | |
play/pause toggle
| -rw-r--r-- | backend.py | 17 |
1 files changed, 15 insertions, 2 deletions
@@ -1,12 +1,15 @@ from flask import Flask from pathlib import Path from dotenv import load_dotenv +import json import requests import random import subprocess import threading import os +MPV_SOCKET = "/tmp/remote-automation-mpv.sock" + app = Flask(__name__) print_lock = threading.Lock() @@ -47,10 +50,19 @@ def print_queue(): finally: print_lock.release() +def mpv_command(command): + with open(MPV_SOCKET, "w") as sock: + sock.write(json.dumps({"command": command}) + "\n") + sock.flush() + @app.get("/automate/music") def music(): global music_process + if music_process and music_process.poll() is None: + mpv_command(["cycle", "pause"]) + return {"status": "toggled"} + NAVIDROME_URL = os.environ["NAVIDROME_URL"] USERNAME = os.environ["NAVIDROME_USER"] PASSWORD = os.environ["NAVIDROME_PASSWORD"] @@ -97,7 +109,7 @@ def music(): params=stream_params, ).prepare() - # Stop previous song + # Stop previous song (in case it somehow survived earlier) if music_process and music_process.poll() is None: music_process.terminate() @@ -105,6 +117,7 @@ def music(): "mpv", "--no-video", "--audio-device=pipewire", + f"--input-ipc-server={MPV_SOCKET}", "--", stream_request.url, ]) @@ -147,12 +160,12 @@ def fav_song(): global music_process if music_process and music_process.poll() is None: music_process.terminate() - music_process = None music_process = subprocess.Popen([ "mpv", "--no-video", "--audio-device=pipewire", + f"--input-ipc-server={MPV_SOCKET}", "--", FAVOURITE, ]) |
