2025-08-01 20:59:09 +02:00
|
|
|
import dotenv
|
2025-08-01 18:46:38 +02:00
|
|
|
from flask import Flask, Response, jsonify, request
|
2025-08-01 22:07:39 +02:00
|
|
|
|
2025-08-01 18:07:11 +02:00
|
|
|
from flask_cors import CORS
|
2025-08-01 22:07:39 +02:00
|
|
|
from flask_socketio import SocketIO, emit
|
2025-08-01 19:16:20 +02:00
|
|
|
|
|
|
|
from .room import Room
|
2025-08-01 21:39:46 +02:00
|
|
|
from .song import Song, init_db
|
|
|
|
|
|
|
|
# from .song_fetch import *
|
2025-08-01 18:07:11 +02:00
|
|
|
|
2025-08-01 19:13:12 +02:00
|
|
|
dotenv.load_dotenv()
|
2025-08-01 19:16:20 +02:00
|
|
|
|
2025-08-01 18:23:27 +02:00
|
|
|
|
2025-08-01 18:07:11 +02:00
|
|
|
app = Flask(__name__)
|
2025-08-01 22:07:39 +02:00
|
|
|
app.config["SECRET_KEY"] = "your_secret_key"
|
|
|
|
socketio = SocketIO(app)
|
2025-08-01 18:07:11 +02:00
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
|
2025-08-01 21:39:46 +02:00
|
|
|
ROOMS: dict[int, Room] = {
|
|
|
|
1234: Room(
|
|
|
|
1234,
|
|
|
|
(0.0, 1.0),
|
|
|
|
"Test Room",
|
|
|
|
None,
|
|
|
|
{"Rock", "Metal"},
|
|
|
|
{},
|
|
|
|
[],
|
|
|
|
[Song(mbid="test", title="<title placeholder>", artist="<artist placeholder>", tags=["Metal"], image_id="img-id", youtube_id="yt-id")],
|
2025-08-01 22:07:39 +02:00
|
|
|
playing_idx=1,
|
2025-08-01 21:39:46 +02:00
|
|
|
)
|
|
|
|
} # { room_id: room, ... }
|
2025-08-01 18:23:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def error(msg: str, status: int = 400) -> Response:
|
|
|
|
res = jsonify({"success": False, "error": msg})
|
|
|
|
res.status_code = status
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2025-08-01 21:39:46 +02:00
|
|
|
@app.get("/api/join")
|
2025-08-01 18:23:27 +02:00
|
|
|
def join():
|
|
|
|
room_id = request.args.get("room")
|
|
|
|
code = request.args.get("code")
|
|
|
|
|
|
|
|
if room_id is None:
|
|
|
|
return error("Missing room id")
|
|
|
|
|
|
|
|
if (room := ROOMS.get(int(room_id))) is None:
|
|
|
|
return error("Invalid room")
|
|
|
|
|
|
|
|
if room.pin is not None and room.pin != code:
|
|
|
|
return error("Invalid code")
|
|
|
|
|
|
|
|
return {"success": True, "ws": f"/ws/{room_id}"}
|
|
|
|
|
|
|
|
|
2025-08-01 21:39:46 +02:00
|
|
|
@app.get("/api/queue")
|
|
|
|
def queue():
|
|
|
|
if (room_id := request.args.get("room")) is None:
|
|
|
|
return error("Missing room id")
|
|
|
|
|
|
|
|
if (room := ROOMS.get(int(room_id))) is None:
|
|
|
|
return error("Invalid room")
|
|
|
|
|
|
|
|
return {"success": True, "queue": room.playing}
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/queue/next")
|
2025-08-01 22:07:39 +02:00
|
|
|
def queue_next():
|
|
|
|
if (room_id := request.args.get("room")) is None:
|
|
|
|
return error("Missing room id")
|
|
|
|
|
|
|
|
if (room := ROOMS.get(int(room_id))) is None:
|
|
|
|
return error("Invalid room")
|
|
|
|
|
|
|
|
room.playing_idx += 1
|
|
|
|
|
|
|
|
if room.playing_idx >= len(room.playing):
|
|
|
|
## queue ended
|
|
|
|
|
|
|
|
# room.renew_queue()
|
|
|
|
emit("update_songs", {"songs": [1, 2, 3]}, broadcast=True, namespace="/")
|
2025-08-01 21:39:46 +02:00
|
|
|
|
2025-08-01 22:07:39 +02:00
|
|
|
return {"success": True, "ended": True, "index": room.playing_idx, "queue": []}
|
2025-08-01 21:39:46 +02:00
|
|
|
|
2025-08-01 22:07:39 +02:00
|
|
|
return {"success": True, "ended": False, "index": room.playing_idx}
|
2025-08-01 21:39:46 +02:00
|
|
|
|
|
|
|
|
2025-08-01 20:56:48 +02:00
|
|
|
init_db()
|
2025-08-01 22:10:59 +02:00
|
|
|
exit()
|
2025-08-01 18:07:11 +02:00
|
|
|
if __name__ == "__main__":
|
2025-08-01 22:07:39 +02:00
|
|
|
socketio.run(app, debug=True)
|