diff --git a/backend/src/app.py b/backend/src/app.py index 6902f7c..9ddb702 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -1,10 +1,9 @@ import dotenv from flask import Flask, Response, jsonify, request - from flask_cors import CORS from flask_socketio import SocketIO, emit -from .room import Room +from .room import Room, test_algo from .song import Song, init_db # from .song_fetch import * @@ -89,6 +88,8 @@ def queue_next(): init_db() +test_algo() exit() + if __name__ == "__main__": socketio.run(app, debug=True) diff --git a/backend/src/room.py b/backend/src/room.py index 7fc051d..bee8b7f 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -9,6 +9,7 @@ TAG_WEIGHT = 0.1 RANDOM_WEIGHT = 0.1 RECENT_PENALTY = 0.5 RECENT_COUNT = 10 +QUEUE_SIZE = 3 type UserScoredSong = tuple[Song, int] @@ -34,11 +35,26 @@ class Room: pin: int | None tags: set[str] - songs: dict[str, UserScoredSong] # canzoni + voto - history: list[Song] # canzoni riprodotte (in ordine) - playing: list[Song] # canzoni che sono i riproduzione + songs: dict[str, UserScoredSong] # all songs + user score (the playlist) + history: list[Song] # all songs previously played + + playing: list[Song] # queue playing_idx: int + def renew_queue(self): + for song in self.playing: + self.history.append(song) + self.playing.clear() + self.playing_idx = 0 + + rankings: dict[str, float] = {} + for id, (song, user_score) in self.songs.items(): + rankings[id] = self.rank_song(song, user_score).total() + + # sort dict items by their values and pick the highest 3 + top_items = sorted(rankings.items(), key=lambda item: item[1], reverse=True)[:QUEUE_SIZE] # [:3] + self.playing = list(map(lambda x: self.songs[x[0]][0], top_items)) # remove the ranking and take only the songs + def rank_song_from_id(self, id: str) -> Rank: scored = self.songs[id] return self.rank_song(scored[0], scored[1]) @@ -109,18 +125,18 @@ def test_algo(): None, set(["rock", "rap"]), { - "paulham": (songs[0], 2), - "cisco": (songs[1], 1), - "vpn": (songs[2], 5), - "gang": (songs[3], 1), - "bertha1": (songs[4], 1), - "bertha2": (songs[5], 1), - "bertha3": (songs[6], 1), - "cexx": (songs[7], -123123), + "paulham": (songs[0], 7), + "cisco": (songs[1], 5), + "vpn": (songs[2], 11), + "gang": (songs[3], 10), + "bertha1": (songs[4], 4), + "bertha2": (songs[5], 5), + "bertha3": (songs[6], -4), + "cexx": (songs[7], 12), }, - [songs[4], songs[5], songs[0]], + [], + [songs[2], songs[0], songs[1]], + 1, ) - - print(room.rank_song_from_id("paulham"), room.rank_song_from_id("paulham").total()) - print(room.rank_song_from_id("vpn"), room.rank_song_from_id("vpn").total()) - print(room.rank_song_from_id("cexx"), room.rank_song_from_id("cexx").total()) + room.renew_queue() + print(room.playing) diff --git a/backend/src/song.py b/backend/src/song.py index 598975f..8be443d 100644 --- a/backend/src/song.py +++ b/backend/src/song.py @@ -1,4 +1,5 @@ from dataclasses import dataclass + from .connect import get_connection @@ -19,7 +20,7 @@ def init_db(): conn.close() -@dataclass +@dataclass(frozen=True) class Song: mbid: str title: str