Add queue endpoint

This commit is contained in:
Leonardo Segala 2025-08-01 21:39:46 +02:00
parent e22ad91601
commit 63bacf9aae
2 changed files with 38 additions and 10 deletions

View file

@ -3,7 +3,9 @@ from flask import Flask, Response, jsonify, request
from flask_cors import CORS
from .room import Room
from .song import init_db
from .song import Song, init_db
# from .song_fetch import *
dotenv.load_dotenv()
@ -12,7 +14,18 @@ app = Flask(__name__)
CORS(app)
ROOMS: dict[int, Room] = {} # { room_id: room, ... }
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")],
)
} # { room_id: room, ... }
def error(msg: str, status: int = 400) -> Response:
@ -21,12 +34,7 @@ def error(msg: str, status: int = 400) -> Response:
return res
@app.route("/api")
def index():
return "hello from flask"
@app.route("/api/join")
@app.get("/api/join")
def join():
room_id = request.args.get("room")
code = request.args.get("code")
@ -43,6 +51,25 @@ def join():
return {"success": True, "ws": f"/ws/{room_id}"}
@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")
def queue_next(): ...
@app.post("/api/queue/renew")
def queue_renew(): ...
init_db()
if __name__ == "__main__":
app.run(debug=True)

View file

@ -21,8 +21,9 @@ class Room:
pin: int | None
tags: set[str]
songs: dict[str, UserScoredSong]
history: list[Song]
songs: dict[str, UserScoredSong] # canzoni + voto
history: list[Song] # canzoni riprodotte (in ordine)
playing: list[Song] # canzoni che sono i riproduzione
def rank_song(self, song: Song, user_score: int) -> float:
rank = 0.0