File size: 1,497 Bytes
cfc46fa 410048c cfc46fa 410048c cfc46fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import os
import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse, FileResponse
from DragMusic.core import nowplaying
app = FastAPI(docs_url=None, redoc_url="/")
@app.get("/status")
def hello():
return {"message": "running"}
def get_audio_stream(file_path):
def iterfile():
with open(file_path, mode="rb") as file_like:
yield from file_like
return iterfile
@app.get("/", response_class=HTMLResponse)
def mini_web_player():
html_path = os.path.join(os.path.dirname(__file__), "DragMusic", "web", "player.html")
if not os.path.exists(html_path):
# fallback to old inline HTML if file missing
return "<h2>Mini Web Player</h2><p>UI file missing.</p>"
return FileResponse(html_path, media_type="text/html")
@app.get("/current")
def get_current_song():
song = nowplaying.get_current_song()
if song:
return JSONResponse(content=song)
return JSONResponse(content={"error": "No song playing"}, status_code=404)
@app.get("/stream")
def stream_audio():
song = nowplaying.get_current_song()
if not song or not song.get("path") or not os.path.exists(song["path"]):
return JSONResponse(content={"error": "No song playing or file missing"}, status_code=404)
file_path = song["path"]
return StreamingResponse(get_audio_stream(file_path)(), media_type="audio/mpeg")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |