severely clean things up
This commit is contained in:
parent
d98c34fe02
commit
6446027a76
1 changed files with 61 additions and 26 deletions
87
app.py
87
app.py
|
@ -1,16 +1,54 @@
|
|||
from flask import Flask, render_template, request, send_file, Response
|
||||
import os
|
||||
import subprocess
|
||||
import signal
|
||||
|
||||
os.system("killall mpv")
|
||||
playback = False
|
||||
mpv_process: subprocess.Popen | None = None
|
||||
if not os.path.exists("music"):
|
||||
os.mkdir("music")
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def audio_player():
|
||||
os.system(f"mpv --input-ipc-server=/tmp/mpvsocket --shuffle music --no-video &")
|
||||
def shutdown(status: int = 0):
|
||||
playback_stop()
|
||||
os._exit(status)
|
||||
|
||||
|
||||
def sigh(signum: int, _frame):
|
||||
status = 0 if signum == signal.SIGTERM else 128 + signum
|
||||
shutdown(status)
|
||||
|
||||
|
||||
def playback_start():
|
||||
global mpv_process
|
||||
mpv_process = subprocess.Popen(
|
||||
[
|
||||
"mpv",
|
||||
"--input-ipc-server=/tmp/mpvsocket",
|
||||
"--shuffle",
|
||||
"music",
|
||||
"--no-video",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def playback_stop():
|
||||
global mpv_process
|
||||
if mpv_process is not None:
|
||||
mpv_process.terminate()
|
||||
mpv_process.wait()
|
||||
mpv_process = None
|
||||
|
||||
|
||||
def is_playing():
|
||||
global mpv_process
|
||||
running = mpv_process is not None
|
||||
if mpv_process is not None:
|
||||
mpv_process.poll()
|
||||
running = mpv_process.returncode is None
|
||||
|
||||
return running
|
||||
|
||||
|
||||
def sizeof_fmt(num, suffix="B"):
|
||||
|
@ -23,22 +61,20 @@ def sizeof_fmt(num, suffix="B"):
|
|||
|
||||
@app.route("/", methods=["GET"])
|
||||
def route_interface():
|
||||
global playback
|
||||
return render_template("player.html", playing=playback)
|
||||
|
||||
|
||||
@app.route("/", methods=["POST"])
|
||||
def route_toggle():
|
||||
global playback
|
||||
if playback:
|
||||
print("Killing Audio Playback..")
|
||||
os.system("killall mpv")
|
||||
playback = False
|
||||
global mpv_process
|
||||
if is_playing():
|
||||
print("Stopping Audio Playback..")
|
||||
playback_stop()
|
||||
else:
|
||||
print("Starting Audio Playback..")
|
||||
audio_player()
|
||||
playback = True
|
||||
return render_template("player.html", playing=playback)
|
||||
playback_start()
|
||||
|
||||
return render_template("player.html", playing=is_playing())
|
||||
|
||||
|
||||
@app.route("/files/<path:path>", methods=["GET"])
|
||||
|
@ -64,33 +100,32 @@ def filemgr(path=""):
|
|||
|
||||
@app.route("/api/start", methods=["POST"])
|
||||
def api_start():
|
||||
global playback
|
||||
if playback:
|
||||
if is_playing():
|
||||
return Response("Laas is already lofi'ing", 400)
|
||||
else:
|
||||
audio_player()
|
||||
playback = True
|
||||
playback_start()
|
||||
return Response("ok", 200)
|
||||
|
||||
|
||||
@app.route("/api/stop", methods=["POST"])
|
||||
def api_stop():
|
||||
global playback
|
||||
if not playback:
|
||||
if not is_playing():
|
||||
return Response("You cant stop when theres no playback, womp womp!", 400)
|
||||
else:
|
||||
os.system("killall mpv")
|
||||
playback = False
|
||||
playback_stop()
|
||||
return Response("ok", 200)
|
||||
|
||||
|
||||
@app.route("/api/status", methods=["GET"])
|
||||
def api_status():
|
||||
if playback:
|
||||
return "true"
|
||||
else:
|
||||
return "false"
|
||||
return Response(str(is_playing()).lower(), headers={"Content-Type": "text/json"})
|
||||
|
||||
|
||||
signal.signal(signal.SIGTERM, sigh)
|
||||
signal.signal(signal.SIGINT, sigh)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=1337)
|
||||
try:
|
||||
app.run(host="0.0.0.0", port=1337)
|
||||
finally:
|
||||
playback_stop()
|
||||
|
|
Loading…
Add table
Reference in a new issue