Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
5caa19fd9f
14 changed files with 360 additions and 92 deletions
|
@ -1,9 +1,9 @@
|
|||
import uuid
|
||||
from dataclasses import asdict
|
||||
import dotenv
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
import uuid
|
||||
from dataclasses import asdict
|
||||
from flask_socketio import SocketIO, join_room, leave_room
|
||||
|
||||
from .state import State
|
||||
from .connect import get_connection
|
||||
|
@ -12,16 +12,16 @@ from .song import Song, init_db, get_song_by_title_artist, add_song_in_db, get_s
|
|||
from .song_fetch import lastfm_query_search, yt_get_audio_url, yt_search_song
|
||||
from .qrcode_gen import generate_qr
|
||||
|
||||
from typing import Any
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config["SECRET_KEY"] = "your_secret_key"
|
||||
socketio = SocketIO(app)
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", path="/ws")
|
||||
CORS(app)
|
||||
|
||||
|
||||
db_conn = get_connection()
|
||||
state = State(app, socketio, db_conn.cursor())
|
||||
init_db(state.db)
|
||||
|
@ -46,15 +46,28 @@ def error(msg: str, status: int = 400) -> Response:
|
|||
return res
|
||||
|
||||
|
||||
def ws_broadcast(event: str, *, room_id: int | None = None, data: Any) -> None:
|
||||
final = {}
|
||||
@socketio.on("connect")
|
||||
def handle_connection():
|
||||
print("somebody connected to socket.io", flush=True)
|
||||
|
||||
if room_id is not None:
|
||||
final["room_id"] = room_id
|
||||
|
||||
final["data"] = data
|
||||
@socketio.on("disconnect")
|
||||
def handle_disconnection():
|
||||
print("somebody disconnected from socket.io", flush=True)
|
||||
|
||||
emit(event, final, broadcast=True, namespace="/")
|
||||
|
||||
@socketio.on("join_room")
|
||||
def on_join(data):
|
||||
room = data["id"]
|
||||
join_room(room)
|
||||
print(f"somebody joined {room=}", flush=True)
|
||||
|
||||
|
||||
@socketio.on("leave_room")
|
||||
def on_leave(data):
|
||||
room = data["id"]
|
||||
leave_room(room)
|
||||
print(f"somebody left {room=}", flush=True)
|
||||
|
||||
|
||||
@app.get("/api/join")
|
||||
|
@ -98,10 +111,11 @@ def queue_next():
|
|||
if room.playing_idx >= len(room.playing):
|
||||
## queue ended
|
||||
|
||||
room.renew_queue()
|
||||
emit("update_songs", {"songs": [1, 2, 3]}, broadcast=True, namespace="/")
|
||||
# room.renew_queue()
|
||||
data = {"success": True, "ended": True, "index": room.playing_idx, "queue": room.playing}
|
||||
state.socketio.emit("new_queue", data, to=str(room.id))
|
||||
|
||||
return {"success": True, "ended": True, "index": room.playing_idx, "queue": room.playing}
|
||||
return data
|
||||
|
||||
return {"success": True, "ended": False, "index": room.playing_idx}
|
||||
|
||||
|
@ -188,6 +202,7 @@ def add_song():
|
|||
## add the song in the room if does not exists
|
||||
if song.uuid not in room.songs:
|
||||
room.songs[song.uuid] = (song, 1) # start with one vote
|
||||
socketio.emit("new_song", {"song": song, "user_score": 1}, to=str(room.id))
|
||||
|
||||
return {"success": True, "song": song}
|
||||
|
||||
|
@ -219,6 +234,7 @@ def post_song_vote():
|
|||
|
||||
## update the song
|
||||
room.songs[song_id] = (song_info[0], song_info[1] + int(request.args["increment"]))
|
||||
socketio.emit("new_vote", {"song": song_info[0], "user_score": song_info[1]})
|
||||
|
||||
return {"success": True}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue