Add discover UI with new song suggestions
Each mix rebuild now also finds songs that aren't in the library: the user's Listenbrainz recommendations they've never played, plus an LB Radio prompt per mix when a Listenbrainz token is configured. Each candidate goes to the mix whose genre profile it fits best and is skipped if it's already in the library or was played recently. discover/ is a new React app to listen to the mixes: library songs stream from Subsonic and suggested songs are found on YouTube with yt-dlp, both proxied through playlistd with Range support for seeking. Suggestions can be liked (kept in a "Liked new songs" list) or hidden. - discover: candidate gathering, genre fit scoring, library filtering - web: /api/discover routes, Subsonic/YouTube streaming proxy - youtube: yt-dlp search (skipping hour-long results) and audio URLs - mix tags now hold library genre names, for display in the UI - config: listenbrainz.token, discover.ui, discover.ytdlp, discover.format Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
This commit is contained in:
32 files changed
+4628
-36
No files matched your search
+375
@@ -0,0 +1,375 @@
|
||||
//! API for the discover UI: mixes, suggested songs, and audio/cover streaming
|
||||
//! from Subsonic or YouTube.
|
||||
|
||||
use crate::models::{ClusterMixRule, MixSuggestion};
|
||||
use crate::subsonic::Subsonic;
|
||||
use crate::track::Track;
|
||||
use crate::youtube::YouTube;
|
||||
use futures_util::TryStreamExt;
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{FromRequest, Outcome};
|
||||
use rocket::response::{self, Responder};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{get, post, put, routes, Request, Response, Route, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::postgres::PgPool;
|
||||
use std::convert::Infallible;
|
||||
use std::sync::Arc;
|
||||
use tokio_util::io::StreamReader;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![rules, mixes, mix, liked, set_status, rebuild, stream, cover, youtube]
|
||||
}
|
||||
|
||||
type ApiResult<T> = Result<Json<T>, Status>;
|
||||
|
||||
fn db_error(e: sqlx::Error) -> Status {
|
||||
match e {
|
||||
sqlx::Error::RowNotFound => Status::NotFound,
|
||||
e => {
|
||||
println!("Database error: {:?}", e);
|
||||
Status::InternalServerError
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_id(id: &str) -> Result<Uuid, Status> {
|
||||
Uuid::parse_str(id).map_err(|_| Status::NotFound)
|
||||
}
|
||||
|
||||
#[get("/rules")]
|
||||
async fn rules(pool: &State<PgPool>) -> ApiResult<Vec<ClusterMixRule>> {
|
||||
ClusterMixRule::all(pool.inner())
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_error)
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MixSummary {
|
||||
id: Uuid,
|
||||
playlist_id: String,
|
||||
name: Option<String>,
|
||||
tags: Vec<String>,
|
||||
position: i32,
|
||||
new_count: i64,
|
||||
}
|
||||
|
||||
#[get("/rules/<id>/mixes")]
|
||||
async fn mixes(id: &str, pool: &State<PgPool>) -> ApiResult<Vec<MixSummary>> {
|
||||
sqlx::query_as!(
|
||||
MixSummary,
|
||||
r#"
|
||||
SELECT p.id, p.playlist_id, p.name, p.tags, p.position,
|
||||
COUNT(s.id) FILTER (WHERE s.status = 'new') AS "new_count!"
|
||||
FROM "cluster_mix_playlist" p
|
||||
LEFT JOIN "mix_suggestion" s ON s.mix_id = p.id
|
||||
WHERE p.rule_id = $1
|
||||
GROUP BY p.id
|
||||
ORDER BY p.position
|
||||
"#,
|
||||
parse_id(id)?
|
||||
)
|
||||
.fetch_all(pool.inner())
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_error)
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MixDetail {
|
||||
#[serde(flatten)]
|
||||
summary: MixSummary,
|
||||
songs: Vec<Track>,
|
||||
suggestions: Vec<MixSuggestion>,
|
||||
}
|
||||
|
||||
#[get("/mixes/<id>")]
|
||||
async fn mix(
|
||||
id: &str,
|
||||
pool: &State<PgPool>,
|
||||
media_client: &State<Arc<Subsonic>>,
|
||||
) -> ApiResult<MixDetail> {
|
||||
let id = parse_id(id)?;
|
||||
let summary = sqlx::query_as!(
|
||||
MixSummary,
|
||||
r#"
|
||||
SELECT p.id, p.playlist_id, p.name, p.tags, p.position,
|
||||
COUNT(s.id) FILTER (WHERE s.status = 'new') AS "new_count!"
|
||||
FROM "cluster_mix_playlist" p
|
||||
LEFT JOIN "mix_suggestion" s ON s.mix_id = p.id
|
||||
WHERE p.id = $1
|
||||
GROUP BY p.id
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(pool.inner())
|
||||
.await
|
||||
.map_err(db_error)?;
|
||||
|
||||
let songs = media_client
|
||||
.get_playlist(&summary.playlist_id)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("Could not load playlist {}: {:?}", summary.playlist_id, e);
|
||||
Status::BadGateway
|
||||
})?
|
||||
.entry;
|
||||
|
||||
let suggestions = sqlx::query_as!(
|
||||
MixSuggestion,
|
||||
r#"
|
||||
SELECT * FROM "mix_suggestion"
|
||||
WHERE mix_id = $1 AND status <> 'dismissed'
|
||||
ORDER BY score DESC
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_all(pool.inner())
|
||||
.await
|
||||
.map_err(db_error)?;
|
||||
|
||||
Ok(Json(MixDetail {
|
||||
summary,
|
||||
songs,
|
||||
suggestions,
|
||||
}))
|
||||
}
|
||||
|
||||
#[get("/rules/<id>/liked")]
|
||||
async fn liked(id: &str, pool: &State<PgPool>) -> ApiResult<Vec<MixSuggestion>> {
|
||||
sqlx::query_as!(
|
||||
MixSuggestion,
|
||||
r#"
|
||||
SELECT * FROM "mix_suggestion"
|
||||
WHERE rule_id = $1 AND status = 'liked'
|
||||
ORDER BY created_at DESC
|
||||
"#,
|
||||
parse_id(id)?
|
||||
)
|
||||
.fetch_all(pool.inner())
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_error)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct StatusUpdate {
|
||||
status: String,
|
||||
}
|
||||
|
||||
/// Like, dismiss, or reset a suggestion
|
||||
#[put("/suggestions/<id>", data = "<update>")]
|
||||
async fn set_status(
|
||||
id: &str,
|
||||
update: Json<StatusUpdate>,
|
||||
pool: &State<PgPool>,
|
||||
) -> ApiResult<MixSuggestion> {
|
||||
if !["new", "liked", "dismissed"].contains(&update.status.as_str()) {
|
||||
return Err(Status::UnprocessableEntity);
|
||||
}
|
||||
sqlx::query_as!(
|
||||
MixSuggestion,
|
||||
r#"UPDATE "mix_suggestion" SET status = $1 WHERE id = $2 RETURNING *"#,
|
||||
update.status,
|
||||
parse_id(id)?
|
||||
)
|
||||
.fetch_one(pool.inner())
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(db_error)
|
||||
}
|
||||
|
||||
/// Rebuild a rule's mixes on the next daemon tick
|
||||
#[post("/rules/<id>/rebuild")]
|
||||
async fn rebuild(id: &str, pool: &State<PgPool>) -> Status {
|
||||
let Ok(id) = parse_id(id) else {
|
||||
return Status::NotFound;
|
||||
};
|
||||
match sqlx::query!(
|
||||
r#"UPDATE "cluster_mix_rule" SET last_run = NULL WHERE id = $1"#,
|
||||
id
|
||||
)
|
||||
.execute(pool.inner())
|
||||
.await
|
||||
{
|
||||
Ok(r) if r.rows_affected() > 0 => Status::Accepted,
|
||||
Ok(_) => Status::NotFound,
|
||||
Err(e) => db_error(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// The request's Range header, so audio can be seeked
|
||||
pub struct Range(Option<String>);
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for Range {
|
||||
type Error = Infallible;
|
||||
|
||||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||
Outcome::Success(Range(
|
||||
request.headers().get_one("Range").map(str::to_string),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Stream an upstream response through, keeping the headers a media element needs
|
||||
pub struct Proxied(reqwest::Response);
|
||||
|
||||
impl<'r> Responder<'r, 'static> for Proxied {
|
||||
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
||||
let upstream = self.0;
|
||||
let mut response = Response::build();
|
||||
response.status(Status::new(upstream.status().as_u16()));
|
||||
for name in [
|
||||
"content-type",
|
||||
"content-range",
|
||||
"accept-ranges",
|
||||
"cache-control",
|
||||
"last-modified",
|
||||
"etag",
|
||||
] {
|
||||
if let Some(value) = upstream.headers().get(name).and_then(|v| v.to_str().ok()) {
|
||||
response.raw_header(name, value.to_string());
|
||||
}
|
||||
}
|
||||
let size = upstream.content_length();
|
||||
let body = StreamReader::new(upstream.bytes_stream().map_err(std::io::Error::other));
|
||||
match size {
|
||||
// a known length lets browsers show durations and seek
|
||||
Some(size) => response.sized_body(size as usize, LengthReader(body)),
|
||||
None => response.streamed_body(body),
|
||||
};
|
||||
response.ok()
|
||||
}
|
||||
}
|
||||
|
||||
/// Rocket only accepts a known body length for seekable bodies, but never seeks one whose size
|
||||
/// is given up front. This satisfies the trait for a plain stream.
|
||||
struct LengthReader<R>(R);
|
||||
|
||||
impl<R: tokio::io::AsyncRead + Unpin> tokio::io::AsyncRead for LengthReader<R> {
|
||||
fn poll_read(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
buf: &mut tokio::io::ReadBuf<'_>,
|
||||
) -> std::task::Poll<std::io::Result<()>> {
|
||||
std::pin::Pin::new(&mut self.0).poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Unpin> tokio::io::AsyncSeek for LengthReader<R> {
|
||||
fn start_seek(self: std::pin::Pin<&mut Self>, _: std::io::SeekFrom) -> std::io::Result<()> {
|
||||
Err(std::io::Error::other("streamed body can't be seeked"))
|
||||
}
|
||||
|
||||
fn poll_complete(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
_: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::io::Result<u64>> {
|
||||
std::task::Poll::Ready(Err(std::io::Error::other("streamed body can't be seeked")))
|
||||
}
|
||||
}
|
||||
|
||||
/// Stream a song from Subsonic
|
||||
#[get("/stream/<id>")]
|
||||
async fn stream(
|
||||
id: &str,
|
||||
range: Range,
|
||||
media_client: &State<Arc<Subsonic>>,
|
||||
) -> Result<Proxied, Status> {
|
||||
let format = crate::CONFIG
|
||||
.get()
|
||||
.and_then(|c| c.get::<String>("discover.format").ok())
|
||||
.unwrap_or_else(|| "raw".to_string());
|
||||
media_client
|
||||
.raw(
|
||||
"stream",
|
||||
&[("id", id.to_string()), ("format", format)],
|
||||
range.0.as_deref(),
|
||||
)
|
||||
.await
|
||||
.map(Proxied)
|
||||
.map_err(|_| Status::BadGateway)
|
||||
}
|
||||
|
||||
/// Cover art from Subsonic
|
||||
#[get("/cover/<id>?<size>")]
|
||||
async fn cover(
|
||||
id: &str,
|
||||
size: Option<u32>,
|
||||
media_client: &State<Arc<Subsonic>>,
|
||||
) -> Result<Proxied, Status> {
|
||||
media_client
|
||||
.raw(
|
||||
"getCoverArt",
|
||||
&[
|
||||
("id", id.to_string()),
|
||||
("size", size.unwrap_or(300).to_string()),
|
||||
],
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map(Proxied)
|
||||
.map_err(|_| Status::BadGateway)
|
||||
}
|
||||
|
||||
/// Find a suggested song on YouTube and stream its audio
|
||||
#[get("/youtube/<id>")]
|
||||
async fn youtube(
|
||||
id: &str,
|
||||
range: Range,
|
||||
pool: &State<PgPool>,
|
||||
youtube: &State<YouTube>,
|
||||
) -> Result<Proxied, Status> {
|
||||
let suggestion = sqlx::query_as!(
|
||||
MixSuggestion,
|
||||
r#"SELECT * FROM "mix_suggestion" WHERE id = $1"#,
|
||||
parse_id(id)?
|
||||
)
|
||||
.fetch_one(pool.inner())
|
||||
.await
|
||||
.map_err(db_error)?;
|
||||
|
||||
let video_id = match suggestion.youtube_id {
|
||||
Some(video_id) => video_id,
|
||||
None => {
|
||||
let video_id = youtube
|
||||
.search(&suggestion.artist, &suggestion.title)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("YouTube search failed: {}", e);
|
||||
Status::BadGateway
|
||||
})?
|
||||
.ok_or(Status::NotFound)?;
|
||||
sqlx::query!(
|
||||
r#"UPDATE "mix_suggestion" SET youtube_id = $1 WHERE id = $2"#,
|
||||
video_id,
|
||||
suggestion.id
|
||||
)
|
||||
.execute(pool.inner())
|
||||
.await
|
||||
.map_err(db_error)?;
|
||||
video_id
|
||||
}
|
||||
};
|
||||
|
||||
// a cached URL may have expired; fetch a fresh one once
|
||||
for refresh in [false, true] {
|
||||
let url = youtube.audio_url(&video_id, refresh).await.map_err(|e| {
|
||||
println!("Could not get audio for {}: {}", video_id, e);
|
||||
Status::BadGateway
|
||||
})?;
|
||||
let mut request = youtube.client.get(url);
|
||||
if let Some(range) = &range.0 {
|
||||
request = request.header(reqwest::header::RANGE, range);
|
||||
}
|
||||
let response = request.send().await.map_err(|_| Status::BadGateway)?;
|
||||
if !refresh && matches!(response.status().as_u16(), 403 | 404 | 410) {
|
||||
continue;
|
||||
}
|
||||
return Ok(Proxied(response));
|
||||
}
|
||||
Err(Status::BadGateway)
|
||||
}
|
||||
Reference in new issue
Block a user