Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
commit
d56dbb3d9f
5 changed files with 78 additions and 33 deletions
|
@ -1,7 +1,9 @@
|
|||
import dotenv
|
||||
from connect import get_connection
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
from state import State
|
||||
|
||||
from .room import Room
|
||||
from .song import Song, init_db, get_song_by_title_artist, add_song_in_db
|
||||
|
@ -15,8 +17,9 @@ app.config["SECRET_KEY"] = "your_secret_key"
|
|||
socketio = SocketIO(app)
|
||||
CORS(app)
|
||||
|
||||
|
||||
ROOMS: dict[int, Room] = {} # { room_id: room, ... }
|
||||
db_conn = get_connection()
|
||||
state = State(app, db_conn.cursor())
|
||||
init_db(state.db)
|
||||
|
||||
|
||||
def error(msg: str, status: int = 400) -> Response:
|
||||
|
@ -33,7 +36,7 @@ def join():
|
|||
if room_id is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
if room.pin is not None and room.pin != code:
|
||||
|
@ -47,7 +50,7 @@ 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:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
return {"success": True, "queue": room.playing}
|
||||
|
@ -58,7 +61,7 @@ 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:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
room.playing_idx += 1
|
||||
|
@ -90,7 +93,7 @@ def room_new():
|
|||
lat, lon = room_cords.split(",")
|
||||
|
||||
room = Room(
|
||||
id=max(ROOMS or [0]) + 1, #
|
||||
id=max(state.rooms or [0]) + 1, #
|
||||
coord=(float(lat), float(lon)),
|
||||
name=room_name,
|
||||
pin=room_pin,
|
||||
|
@ -101,7 +104,7 @@ def room_new():
|
|||
playing_idx=-1,
|
||||
)
|
||||
|
||||
ROOMS[room.id] = room
|
||||
state.rooms[room.id] = room
|
||||
|
||||
return {"success": True, "room_id": room.id}
|
||||
|
||||
|
@ -115,7 +118,7 @@ def room():
|
|||
"private": room.pin is not None,
|
||||
"coords": room.coord,
|
||||
}
|
||||
for room in ROOMS.values()
|
||||
for room in state.rooms.values()
|
||||
]
|
||||
|
||||
|
||||
|
@ -124,7 +127,7 @@ def add_song():
|
|||
if (room_id := request.args.get("room")) is None:
|
||||
return error("Missing room id")
|
||||
|
||||
if (room := ROOMS.get(int(room_id))) is None:
|
||||
if (room := state.rooms.get(int(room_id))) is None:
|
||||
return error("Invalid room")
|
||||
|
||||
if (query := request.args.get("query")) is None:
|
||||
|
@ -135,7 +138,7 @@ def add_song():
|
|||
if (song := get_song_by_title_artist(info.title, info.artist)) is None:
|
||||
res = download_song_mp3(info.title, info.artist)
|
||||
if res is None:
|
||||
ops
|
||||
return error("Cannot get info from YT")
|
||||
yt_id, _ = res
|
||||
## song not found, downolad from YT
|
||||
## add in DB
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue