make volume route always available (fixed)
This commit is contained in:
parent
2418657122
commit
1293a6f7d0
1 changed files with 13 additions and 15 deletions
28
app.py
28
app.py
|
@ -15,6 +15,8 @@ music_path = Path(os.environ.get("MUSIC_PATH", "./music"))
|
|||
mpv_process: subprocess.Popen | None = None
|
||||
app = Flask(__name__)
|
||||
|
||||
volume: float = 50
|
||||
|
||||
|
||||
def cleanup_unclean():
|
||||
if mpv_pidfile.is_file():
|
||||
|
@ -49,6 +51,7 @@ def playback_start():
|
|||
"--shuffle",
|
||||
str(music_path),
|
||||
"--no-video",
|
||||
f"--volume={volume}",
|
||||
],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
|
@ -232,26 +235,21 @@ def api_isengard():
|
|||
|
||||
@app.route("/api/volume", methods=["GET", "PUT"])
|
||||
def api_volume():
|
||||
if not is_playing():
|
||||
return jsonify("not playing"), 400
|
||||
if request.method == "PUT":
|
||||
global volume
|
||||
try:
|
||||
volume = float(request.get_data().decode())
|
||||
if not 0 <= volume <= 100:
|
||||
raise ValueError()
|
||||
except Exception:
|
||||
return jsonify("bad volume"), 400
|
||||
|
||||
sock = mpv_socket_open()
|
||||
if sock is not None:
|
||||
if request.method == "PUT":
|
||||
volume = request.get_data().decode()
|
||||
mpv_socket_command(sock, {"command": ["set", "volume", volume]})
|
||||
return jsonify("ok")
|
||||
|
||||
else:
|
||||
reply_json = mpv_socket_command(
|
||||
sock, {"command": ["get_property", "volume"]}
|
||||
)
|
||||
if "data" in reply_json:
|
||||
return jsonify(float(reply_json["data"]))
|
||||
|
||||
mpv_socket_command(sock, {"command": ["set", "volume", volume]})
|
||||
sock.close()
|
||||
|
||||
return jsonify("unknown error"), 500
|
||||
return jsonify(volume)
|
||||
|
||||
|
||||
signal.signal(signal.SIGTERM, sigh)
|
||||
|
|
Loading…
Add table
Reference in a new issue