update to the playlists. testing
This commit is contained in:
+60
-4
@@ -1,14 +1,62 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use actix_session::Session;
|
||||
use actix_web::{HttpResponse, web};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use sea_orm::DatabaseConnection;
|
||||
use shanty_data::HybridMusicBrainzFetcher;
|
||||
use shanty_db::queries;
|
||||
use shanty_playlist::{self, PlaylistRequest};
|
||||
use shanty_playlist::{self, CountryLookup, PlaylistRequest, SimilarConfig};
|
||||
|
||||
use crate::auth;
|
||||
use crate::error::ApiError;
|
||||
use crate::state::AppState;
|
||||
|
||||
/// Country lookup backed by search_cache + local MusicBrainz DB.
|
||||
/// Never hits the remote MB API — unknown artists pass through the filter.
|
||||
struct CachedCountryLookup<'a> {
|
||||
conn: &'a DatabaseConnection,
|
||||
mb: &'a HybridMusicBrainzFetcher,
|
||||
}
|
||||
|
||||
impl CountryLookup for CachedCountryLookup<'_> {
|
||||
fn get_country<'a>(
|
||||
&'a self,
|
||||
mbid: &'a str,
|
||||
) -> Pin<Box<dyn Future<Output = Option<String>> + Send + 'a>> {
|
||||
Box::pin(async move {
|
||||
// 1. Check search_cache
|
||||
let cache_key = format!("mb_artist_country:{mbid}");
|
||||
if let Ok(Some(cached)) = queries::cache::get(self.conn, &cache_key).await {
|
||||
return if cached == "__none__" {
|
||||
None
|
||||
} else {
|
||||
Some(cached)
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Try local MB DB only (no remote API)
|
||||
let info = self.mb.get_artist_info_local(mbid)?;
|
||||
let country = info.country;
|
||||
|
||||
// Cache the result (30 days)
|
||||
let cache_val = country.as_deref().unwrap_or("__none__");
|
||||
let _ = queries::cache::set(
|
||||
self.conn,
|
||||
&cache_key,
|
||||
"musicbrainz",
|
||||
cache_val,
|
||||
30 * 24 * 3600,
|
||||
)
|
||||
.await;
|
||||
|
||||
country
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(web::resource("/playlists/generate").route(web::post().to(generate_playlist)))
|
||||
.service(
|
||||
@@ -58,13 +106,21 @@ async fn generate_playlist(
|
||||
}
|
||||
let fetcher = shanty_data::LastFmSimilarFetcher::new(api_key)
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||
let similar_config = SimilarConfig::from_request(&req);
|
||||
let country_lookup = if similar_config.country_filter {
|
||||
Some(CachedCountryLookup {
|
||||
conn,
|
||||
mb: &state.mb_client,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
shanty_playlist::similar_artists(
|
||||
conn,
|
||||
&fetcher,
|
||||
req.seed_artists,
|
||||
req.count,
|
||||
req.popularity_bias,
|
||||
&req.ordering,
|
||||
&similar_config,
|
||||
country_lookup.as_ref().map(|c| c as &dyn CountryLookup),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(e.to_string()))?
|
||||
|
||||
Reference in New Issue
Block a user