This commit is contained in:
Francesco De Carlo 2025-08-01 23:25:11 +02:00
commit 9ce161d515
4 changed files with 30 additions and 20 deletions

View file

@ -1,10 +1,12 @@
import dotenv import dotenv
from connect import get_connection
from flask import Flask, Response, jsonify, request from flask import Flask, Response, jsonify, request
from flask_cors import CORS from flask_cors import CORS
from flask_socketio import SocketIO, emit from flask_socketio import SocketIO, emit
from state import State
from .room import Room, test_algo from .room import Room
from .song import Song, init_db from .song import init_db
# from .song_fetch import * # from .song_fetch import *
@ -16,8 +18,9 @@ app.config["SECRET_KEY"] = "your_secret_key"
socketio = SocketIO(app) socketio = SocketIO(app)
CORS(app) CORS(app)
db_conn = get_connection()
ROOMS: dict[int, Room] = {} # { room_id: room, ... } state = State(app, db_conn.cursor())
init_db(state.db)
def error(msg: str, status: int = 400) -> Response: def error(msg: str, status: int = 400) -> Response:
@ -34,7 +37,7 @@ def join():
if room_id is None: if room_id is None:
return error("Missing room id") 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 error("Invalid room")
if room.pin is not None and room.pin != code: if room.pin is not None and room.pin != code:
@ -48,7 +51,7 @@ def queue():
if (room_id := request.args.get("room")) is None: if (room_id := request.args.get("room")) is None:
return error("Missing room id") 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 error("Invalid room")
return {"success": True, "queue": room.playing} return {"success": True, "queue": room.playing}
@ -59,7 +62,7 @@ def queue_next():
if (room_id := request.args.get("room")) is None: if (room_id := request.args.get("room")) is None:
return error("Missing room id") 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 error("Invalid room")
room.playing_idx += 1 room.playing_idx += 1
@ -91,7 +94,7 @@ def room_new():
lat, lon = room_cords.split(",") lat, lon = room_cords.split(",")
room = Room( room = Room(
id=max(ROOMS or [0]) + 1, # id=max(state.rooms or [0]) + 1, #
coord=(float(lat), float(lon)), coord=(float(lat), float(lon)),
name=room_name, name=room_name,
pin=room_pin, pin=room_pin,
@ -102,7 +105,7 @@ def room_new():
playing_idx=-1, playing_idx=-1,
) )
ROOMS[room.id] = room state.rooms[room.id] = room
return {"success": True, "room_id": room.id} return {"success": True, "room_id": room.id}
@ -116,13 +119,9 @@ def room():
"private": room.pin is not None, "private": room.pin is not None,
"coords": room.coord, "coords": room.coord,
} }
for room in ROOMS.values() for room in state.rooms.values()
] ]
init_db()
test_algo()
exit()
if __name__ == "__main__": if __name__ == "__main__":
socketio.run(app, debug=True) socketio.run(app, debug=True)

View file

@ -4,4 +4,5 @@ import sqlite3
def get_connection(): def get_connection():
conn = sqlite3.connect(".data/jukebox.db") conn = sqlite3.connect(".data/jukebox.db")
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.autocommit = True
return conn return conn

View file

@ -1,12 +1,11 @@
from dataclasses import dataclass from dataclasses import dataclass
from sqlite3 import Cursor
from .connect import get_connection from .connect import get_connection
def init_db(): def init_db(db: Cursor):
conn = get_connection() db.execute("""
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS songs ( CREATE TABLE IF NOT EXISTS songs (
uuid TEXT PRIMARY KEY, uuid TEXT PRIMARY KEY,
title TEXT NOT NULL, title TEXT NOT NULL,
@ -16,8 +15,6 @@ def init_db():
youtube_id TEXT NOT NULL youtube_id TEXT NOT NULL
); );
""") """)
conn.commit()
conn.close()
@dataclass(frozen=True) @dataclass(frozen=True)

13
backend/src/state.py Normal file
View file

@ -0,0 +1,13 @@
from dataclasses import dataclass
from sqlite3 import Cursor
from flask import Flask
from .room import Room
@dataclass
class State:
app: Flask
db: Cursor
rooms: dict[int, Room] = {} # { room_id: room, ... }