This commit is contained in:
Francesco De Carlo 2025-08-01 23:07:45 +02:00
commit 9ba0e04501
3 changed files with 37 additions and 18 deletions

View file

@ -3,7 +3,7 @@ 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 *
@ -121,6 +121,8 @@ def room():
init_db()
test_algo()
exit()
if __name__ == "__main__":
socketio.run(app, debug=True)

View file

@ -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)

View file

@ -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