Spaces:
Running
Running
Add mini web player with UI and streaming for /play command
Browse files- DragMusic/core/nowplaying.py +12 -0
- DragMusic/plugins/play/play.py +5 -1
- DragMusic/web/player.html +100 -0
- server.py +32 -1
DragMusic/core/nowplaying.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Module to store the current song info and file path for web streaming
|
2 |
+
|
3 |
+
from typing import Optional, Dict
|
4 |
+
|
5 |
+
_current_song: Optional[Dict] = None
|
6 |
+
|
7 |
+
def set_current_song(song_info: Dict):
|
8 |
+
global _current_song
|
9 |
+
_current_song = song_info
|
10 |
+
|
11 |
+
def get_current_song() -> Optional[Dict]:
|
12 |
+
return _current_song
|
DragMusic/plugins/play/play.py
CHANGED
@@ -22,6 +22,7 @@ from DragMusic.utils.inline import (
|
|
22 |
)
|
23 |
from DragMusic.utils.logger import play_logs
|
24 |
from DragMusic.utils.stream.stream import stream
|
|
|
25 |
from config import BANNED_USERS, lyrical
|
26 |
|
27 |
|
@@ -91,7 +92,8 @@ async def play_commnd(
|
|
91 |
"path": file_path,
|
92 |
"dur": dur,
|
93 |
}
|
94 |
-
|
|
|
95 |
try:
|
96 |
await stream(
|
97 |
_,
|
@@ -136,6 +138,8 @@ async def play_commnd(
|
|
136 |
"path": file_path,
|
137 |
"dur": dur,
|
138 |
}
|
|
|
|
|
139 |
try:
|
140 |
await stream(
|
141 |
_,
|
|
|
22 |
)
|
23 |
from DragMusic.utils.logger import play_logs
|
24 |
from DragMusic.utils.stream.stream import stream
|
25 |
+
from DragMusic.core import nowplaying
|
26 |
from config import BANNED_USERS, lyrical
|
27 |
|
28 |
|
|
|
92 |
"path": file_path,
|
93 |
"dur": dur,
|
94 |
}
|
95 |
+
# Store current song info for web player
|
96 |
+
nowplaying.set_current_song(details)
|
97 |
try:
|
98 |
await stream(
|
99 |
_,
|
|
|
138 |
"path": file_path,
|
139 |
"dur": dur,
|
140 |
}
|
141 |
+
# Store current song info for web player
|
142 |
+
nowplaying.set_current_song(details)
|
143 |
try:
|
144 |
await stream(
|
145 |
_,
|
DragMusic/web/player.html
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Mini Web Player</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
background: #181818;
|
10 |
+
color: #fff;
|
11 |
+
font-family: 'Segoe UI', Arial, sans-serif;
|
12 |
+
display: flex;
|
13 |
+
flex-direction: column;
|
14 |
+
align-items: center;
|
15 |
+
justify-content: center;
|
16 |
+
min-height: 100vh;
|
17 |
+
margin: 0;
|
18 |
+
}
|
19 |
+
.player-container {
|
20 |
+
background: #232323;
|
21 |
+
border-radius: 16px;
|
22 |
+
box-shadow: 0 4px 24px rgba(0,0,0,0.4);
|
23 |
+
padding: 32px 24px 24px 24px;
|
24 |
+
max-width: 350px;
|
25 |
+
width: 100%;
|
26 |
+
display: flex;
|
27 |
+
flex-direction: column;
|
28 |
+
align-items: center;
|
29 |
+
}
|
30 |
+
.album-art {
|
31 |
+
width: 180px;
|
32 |
+
height: 180px;
|
33 |
+
border-radius: 12px;
|
34 |
+
background: #333;
|
35 |
+
margin-bottom: 20px;
|
36 |
+
object-fit: cover;
|
37 |
+
box-shadow: 0 2px 12px rgba(0,0,0,0.3);
|
38 |
+
}
|
39 |
+
.song-title {
|
40 |
+
font-size: 1.2em;
|
41 |
+
font-weight: bold;
|
42 |
+
margin-bottom: 6px;
|
43 |
+
text-align: center;
|
44 |
+
}
|
45 |
+
.song-meta {
|
46 |
+
font-size: 0.95em;
|
47 |
+
color: #aaa;
|
48 |
+
margin-bottom: 18px;
|
49 |
+
text-align: center;
|
50 |
+
}
|
51 |
+
audio {
|
52 |
+
width: 100%;
|
53 |
+
margin-top: 10px;
|
54 |
+
}
|
55 |
+
.no-song {
|
56 |
+
color: #ff7675;
|
57 |
+
margin-top: 20px;
|
58 |
+
text-align: center;
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
</head>
|
62 |
+
<body>
|
63 |
+
<div class="player-container">
|
64 |
+
<img id="album-art" class="album-art" src="" alt="Album Art" style="display:none;"/>
|
65 |
+
<div id="song-title" class="song-title">Loading...</div>
|
66 |
+
<div id="song-meta" class="song-meta"></div>
|
67 |
+
<audio id="audio" controls autoplay>
|
68 |
+
<source id="audio-src" src="/stream" type="audio/mpeg">
|
69 |
+
Your browser does not support the audio element.
|
70 |
+
</audio>
|
71 |
+
<div id="no-song" class="no-song" style="display:none;">No song playing.</div>
|
72 |
+
</div>
|
73 |
+
<script>
|
74 |
+
async function fetchSongInfo() {
|
75 |
+
try {
|
76 |
+
const res = await fetch('/current');
|
77 |
+
if (!res.ok) throw new Error('No song');
|
78 |
+
const data = await res.json();
|
79 |
+
document.getElementById('no-song').style.display = 'none';
|
80 |
+
document.getElementById('song-title').innerText = data.title || 'Unknown Title';
|
81 |
+
document.getElementById('song-meta').innerText = (data.dur ? `Duration: ${data.dur}` : '') + (data.link ? ` | <a href='${data.link}' target='_blank' style='color:#1db954;'>Source</a>` : '');
|
82 |
+
// Album art
|
83 |
+
if (data.thumb || data.img) {
|
84 |
+
document.getElementById('album-art').src = data.thumb || data.img;
|
85 |
+
document.getElementById('album-art').style.display = 'block';
|
86 |
+
} else {
|
87 |
+
document.getElementById('album-art').style.display = 'none';
|
88 |
+
}
|
89 |
+
} catch (e) {
|
90 |
+
document.getElementById('song-title').innerText = '';
|
91 |
+
document.getElementById('song-meta').innerText = '';
|
92 |
+
document.getElementById('album-art').style.display = 'none';
|
93 |
+
document.getElementById('no-song').style.display = 'block';
|
94 |
+
}
|
95 |
+
}
|
96 |
+
fetchSongInfo();
|
97 |
+
setInterval(fetchSongInfo, 5000);
|
98 |
+
</script>
|
99 |
+
</body>
|
100 |
+
</html>
|
server.py
CHANGED
@@ -1,12 +1,43 @@
|
|
1 |
import os
|
2 |
import uvicorn
|
3 |
from fastapi import FastAPI
|
|
|
|
|
4 |
|
5 |
app = FastAPI(docs_url=None, redoc_url="/")
|
6 |
|
7 |
@app.get("/status")
|
8 |
def hello():
|
9 |
return {"message": "running"}
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if __name__ == "__main__":
|
12 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
import os
|
2 |
import uvicorn
|
3 |
from fastapi import FastAPI
|
4 |
+
from fastapi.responses import HTMLResponse, StreamingResponse, JSONResponse, FileResponse
|
5 |
+
from DragMusic.core import nowplaying
|
6 |
|
7 |
app = FastAPI(docs_url=None, redoc_url="/")
|
8 |
|
9 |
@app.get("/status")
|
10 |
def hello():
|
11 |
return {"message": "running"}
|
12 |
+
|
13 |
+
def get_audio_stream(file_path):
|
14 |
+
def iterfile():
|
15 |
+
with open(file_path, mode="rb") as file_like:
|
16 |
+
yield from file_like
|
17 |
+
return iterfile
|
18 |
+
|
19 |
+
@app.get("/", response_class=HTMLResponse)
|
20 |
+
def mini_web_player():
|
21 |
+
html_path = os.path.join(os.path.dirname(__file__), "DragMusic", "web", "player.html")
|
22 |
+
if not os.path.exists(html_path):
|
23 |
+
# fallback to old inline HTML if file missing
|
24 |
+
return "<h2>Mini Web Player</h2><p>UI file missing.</p>"
|
25 |
+
return FileResponse(html_path, media_type="text/html")
|
26 |
+
|
27 |
+
@app.get("/current")
|
28 |
+
def get_current_song():
|
29 |
+
song = nowplaying.get_current_song()
|
30 |
+
if song:
|
31 |
+
return JSONResponse(content=song)
|
32 |
+
return JSONResponse(content={"error": "No song playing"}, status_code=404)
|
33 |
+
|
34 |
+
@app.get("/stream")
|
35 |
+
def stream_audio():
|
36 |
+
song = nowplaying.get_current_song()
|
37 |
+
if not song or not song.get("path") or not os.path.exists(song["path"]):
|
38 |
+
return JSONResponse(content={"error": "No song playing or file missing"}, status_code=404)
|
39 |
+
file_path = song["path"]
|
40 |
+
return StreamingResponse(get_audio_stream(file_path)(), media_type="audio/mpeg")
|
41 |
+
|
42 |
if __name__ == "__main__":
|
43 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|