allow playing urls

This commit is contained in:
deneb 2025-02-27 22:33:27 +01:00
parent 2b6ee976fe
commit 272d781e7a

38
app.py
View file

@ -1,3 +1,4 @@
import re
from typing import Any, Mapping
from flask import Flask, render_template, request, send_file, jsonify
import os
@ -49,7 +50,7 @@ def playback_start():
"mpv",
f"--input-ipc-server={mpv_socket}",
"--shuffle",
#"--loop-playlist",
# "--loop-playlist",
"--no-video",
f"--volume={volume}",
str(music_path),
@ -206,26 +207,29 @@ def api_nowplaying():
return jsonify(response)
@app.route("/api/play/<filename>", methods=["POST"])
def api_play_file(filename: str, error_str: str = "Could not play file '{filename}'"):
full_filename = music_path.joinpath(filename)
@app.route("/api/play/<path:filename_or_url>", methods=["POST"])
def api_play_file(filename_or_url: str, error_str: str = "Could not play file '{filename}'"):
if re.match("^https?://.*", filename_or_url):
playback_uri = filename_or_url
else:
file_path = music_path.joinpath(filename_or_url)
if not file_path.exists():
return jsonify(error_str.format(filename=filename_or_url)), 404
playback_uri = str(file_path)
status = 404
if full_filename.exists():
status = 500
if not is_playing():
playback_start()
if not is_playing():
playback_start()
sock = mpv_socket_open()
if sock is not None:
mpv_socket_command(sock, {"command": ["loadfile", str(full_filename)]})
sock.close()
sock = mpv_socket_open()
if sock is not None:
mpv_socket_command(sock, {"command": ["loadfile", playback_uri]})
sock.close()
time.sleep(0.1)
if is_playing():
return jsonify("ok")
time.sleep(0.1)
if is_playing():
return jsonify("ok")
return jsonify(error_str.format(filename=filename)), status
return jsonify(error_str.format(filename=filename_or_url)), 500
@app.route("/api/isengard", methods=["POST"])