Add genre mixes from clustering recent listens

A new cluster_mix_rule clusters a Listenbrainz user's listens from the
last few months by MusicBrainz genre (with tracks played in the same
session pulled together) and maintains one Subsonic playlist per cluster,
filled with the cluster's most played tracks plus unheard library songs
from the best matching library genres.

- listenbrainz: fetch raw listens since a timestamp and bulk recording
  genres, handling rate limits
- cluster: tag TF-IDF + co-listen smoothing, spherical k-means with k
  chosen by silhouette score
- mix: match cluster profiles to library genres and pick songs, with a
  per-artist cap; reuse playlists across rebuilds by genre overlap
- subsonic: getGenres, getSongsByGenre, rename/delete playlist, genre and
  MBID fields on tracks, API error detection
- daemon, migration, POST/GET /api/playlist/mix and README docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
This commit is contained in:
Claude committed 2026-11-13 14:53:29 +00:00
1 parent e082209bd2
commit 18b3f0974e
13 files changed
+1728 -5

No files matched your search

+34 -2
View File
@@ -1,13 +1,16 @@
mod cluster;
mod daemon;
mod listenbrainz;
mod mix;
mod models;
mod playlist;
mod subsonic;
mod track;
use crate::daemon::{create_playlists_daemon, update_playlists_daemon};
use crate::daemon::{cluster_mixes_daemon, create_playlists_daemon, update_playlists_daemon};
use crate::models::{
CreateTrackedPlaylist, PartialCreateTrackedPlaylist, PartialTrackedPlaylist, TrackedPlaylist,
ClusterMixRule, CreateTrackedPlaylist, PartialClusterMixRule, PartialCreateTrackedPlaylist,
PartialTrackedPlaylist, TrackedPlaylist,
};
use crate::subsonic::{Subsonic, SubsonicBuilder};
use config::Config;
@@ -61,6 +64,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
create_playlists_daemon(_media_client).await.unwrap();
});
}
{
let _media_client = media_client.clone();
tokio::spawn(async move {
cluster_mixes_daemon(_media_client).await.unwrap();
});
}
rocket::build()
.manage(media_client)
@@ -72,6 +81,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
add_tracking_playlist,
all_playlists,
create_tracking_playlist_rule,
create_cluster_mix_rule,
all_cluster_mix_rules,
],
)
.launch()
@@ -178,3 +189,24 @@ async fn all_playlists(pool: &State<PgPool>) -> JsonValue {
Err(_) => json!([]),
}
}
/// Create a rule that maintains genre mixes built from clustering a user's recent listens
#[post("/mix", data = "<rule>")]
async fn create_cluster_mix_rule(
pool: &State<PgPool>,
rule: Json<PartialClusterMixRule>,
) -> Json<Response<ClusterMixRule>> {
match rule.record(pool.inner()).await {
Ok(rule) => Json(Response::Success(rule)),
Err(e) => Json(Response::Error(e.into())),
}
}
/// Get all cluster mix rules
#[get("/mix")]
async fn all_cluster_mix_rules(pool: &State<PgPool>) -> JsonValue {
match ClusterMixRule::all(pool.inner()).await {
Ok(rules) => json!(rules),
Err(_) => json!([]),
}
}