Re-organized providers and added a few

This commit is contained in:
Connor Johnstone
2026-03-20 14:52:16 -04:00
parent fed86c9e85
commit eaaff5f98f
12 changed files with 353 additions and 185 deletions

View File

@@ -97,6 +97,12 @@ impl From<shanty_tag::TagError> for ApiError {
}
}
impl From<shanty_data::DataError> for ApiError {
fn from(e: shanty_data::DataError) -> Self {
ApiError::Internal(e.to_string())
}
}
impl From<shanty_org::OrgError> for ApiError {
fn from(e: shanty_org::OrgError) -> Self {
ApiError::Internal(e.to_string())

View File

@@ -5,9 +5,10 @@ use clap::Parser;
use tracing_actix_web::TracingLogger;
use tracing_subscriber::EnvFilter;
use shanty_data::MusicBrainzFetcher;
use shanty_data::WikipediaFetcher;
use shanty_db::Database;
use shanty_search::MusicBrainzSearch;
use shanty_tag::MusicBrainzClient;
use shanty_web::config::AppConfig;
use shanty_web::routes;
@@ -53,8 +54,9 @@ async fn main() -> anyhow::Result<()> {
tracing::info!(url = %config.database_url, "connecting to database");
let db = Database::new(&config.database_url).await?;
let mb_client = MusicBrainzClient::new()?;
let mb_client = MusicBrainzFetcher::new()?;
let search = MusicBrainzSearch::new()?;
let wiki_fetcher = WikipediaFetcher::new()?;
let bind = format!("{}:{}", config.web.bind, config.web.port);
tracing::info!(bind = %bind, "starting server");
@@ -64,6 +66,7 @@ async fn main() -> anyhow::Result<()> {
db,
mb_client,
search,
wiki_fetcher,
config: std::sync::Arc::new(tokio::sync::RwLock::new(config)),
config_path,
tasks: TaskManager::new(),

View File

@@ -2,9 +2,9 @@ use actix_session::Session;
use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use shanty_data::MetadataFetcher;
use shanty_db::entities::wanted_item::WantedStatus;
use shanty_db::queries;
use shanty_tag::provider::MetadataProvider;
use crate::auth;
use crate::error::ApiError;
@@ -127,7 +127,7 @@ async fn resolve_release_from_group(
// Since we can't call get_json directly, use the artist_releases approach
// to find a release that matches this group.
//
// Actually, the simplest: the MetadataProvider trait has get_artist_releases
// Actually, the simplest: the MetadataFetcher trait has get_artist_releases
// which returns releases, but we need releases for a release GROUP.
// Let's add a direct HTTP call here via reqwest.

View File

@@ -2,10 +2,10 @@ use actix_session::Session;
use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use shanty_data::{ArtistBioFetcher, ArtistImageFetcher, MetadataFetcher};
use shanty_db::entities::wanted_item::WantedStatus;
use shanty_db::queries;
use shanty_search::SearchProvider;
use shanty_tag::provider::MetadataProvider;
use crate::auth;
use crate::error::ApiError;
@@ -346,9 +346,24 @@ pub async fn enrich_artist(
}
};
// Fetch Wikipedia photo + bio (cached)
let (artist_photo, artist_bio) = fetch_wikipedia_data(state, &mbid, &artist_info).await;
tracing::debug!(mbid = %mbid, has_photo = artist_photo.is_some(), has_bio = artist_bio.is_some(), "wikipedia data");
// Fetch artist photo + bio + banner (cached, provider-aware)
let config = state.config.read().await;
let image_source = config.metadata.artist_image_source.clone();
let bio_source = config.metadata.artist_bio_source.clone();
let lastfm_api_key = config.metadata.lastfm_api_key.clone();
let fanart_api_key = config.metadata.fanart_api_key.clone();
drop(config);
let (artist_photo, artist_bio, artist_banner) = fetch_artist_enrichment(
state,
&mbid,
&artist_info,
&image_source,
&bio_source,
lastfm_api_key.as_deref(),
fanart_api_key.as_deref(),
)
.await;
tracing::debug!(mbid = %mbid, has_photo = artist_photo.is_some(), has_bio = artist_bio.is_some(), has_banner = artist_banner.is_some(), "artist enrichment data");
// Fetch release groups and filter by allowed secondary types
let all_release_groups = state
@@ -591,6 +606,7 @@ pub async fn enrich_artist(
"artist_info": artist_info,
"artist_photo": artist_photo,
"artist_bio": artist_bio,
"artist_banner": artist_banner,
}))
}
@@ -614,137 +630,130 @@ pub async fn enrich_all_watched_artists(state: &AppState) -> Result<u32, ApiErro
Ok(count)
}
/// Fetch artist photo and bio from Wikipedia, with caching.
async fn fetch_wikipedia_data(
/// Fetch artist photo, bio, and banner using configured providers, with per-source caching.
async fn fetch_artist_enrichment(
state: &AppState,
mbid: &str,
artist_info: &Option<shanty_tag::provider::ArtistInfo>,
) -> (Option<String>, Option<String>) {
let cache_key = format!("artist_wiki:{mbid}");
artist_info: &Option<shanty_data::ArtistInfo>,
image_source: &str,
bio_source: &str,
lastfm_api_key: Option<&str>,
fanart_api_key: Option<&str>,
) -> (Option<String>, Option<String>, Option<String>) {
let Some(info) = artist_info.as_ref() else {
tracing::debug!(mbid = mbid, "no artist info for enrichment");
return (None, None, None);
};
// Check cache first
if let Ok(Some(json)) = queries::cache::get(state.db.conn(), &cache_key).await
&& let Ok(cached) = serde_json::from_str::<serde_json::Value>(&json)
{
return (
cached
.get("photo_url")
.and_then(|v| v.as_str())
.map(String::from),
cached.get("bio").and_then(|v| v.as_str()).map(String::from),
);
}
// Find Wikipedia URL from artist info — try direct link first, then resolve via Wikidata
let wiki_url = if let Some(info) = artist_info.as_ref() {
if let Some(u) = info.urls.iter().find(|u| u.link_type == "wikipedia") {
Some(u.url.clone())
} else if let Some(wd) = info.urls.iter().find(|u| u.link_type == "wikidata") {
// Extract Wikidata entity ID and resolve to Wikipedia URL
let entity_id = wd.url.split('/').next_back().unwrap_or("");
resolve_wikidata_to_wikipedia(entity_id).await
} else {
None
}
// Build fanart.tv fetcher once if needed (used for both image and banner)
let fanart_fetcher = if image_source == "fanarttv" {
fanart_api_key.and_then(|key| shanty_data::FanartTvFetcher::new(key.to_string()).ok())
} else {
None
};
let Some(wiki_url) = wiki_url else {
tracing::debug!(mbid = mbid, "no wikipedia URL found");
return (None, None);
};
tracing::debug!(mbid = mbid, wiki_url = %wiki_url, "found wikipedia URL");
// Parse article title from URL (e.g., https://en.wikipedia.org/wiki/Pink_Floyd → Pink_Floyd)
let title = wiki_url.split("/wiki/").nth(1).unwrap_or("").to_string();
if title.is_empty() {
return (None, None);
}
// Detect language from URL (e.g., en.wikipedia.org → en)
let lang = wiki_url
.split("://")
.nth(1)
.and_then(|s| s.split('.').next())
.unwrap_or("en");
// Call Wikipedia REST API
let api_url = format!("https://{lang}.wikipedia.org/api/rest_v1/page/summary/{title}");
let client = reqwest::Client::builder()
.user_agent("Shanty/0.1.0 (shanty-music-app)")
.build()
.ok();
let Some(client) = client else {
return (None, None);
// Fetch image (cached per source — only cache hits, not misses)
let image_cache_key = format!("artist_image:{image_source}:{mbid}");
let photo_url = if let Ok(Some(cached)) =
queries::cache::get(state.db.conn(), &image_cache_key).await
&& !cached.is_empty()
{
Some(cached)
} else {
let url = match image_source {
"wikipedia" => state
.wiki_fetcher
.get_artist_image(info)
.await
.unwrap_or(None),
"fanarttv" => match &fanart_fetcher {
Some(f) => f.get_artist_image(info).await.unwrap_or(None),
None => {
tracing::warn!("fanart.tv selected but SHANTY_FANART_API_KEY not set");
None
}
},
_ => None,
};
if let Some(ref val) = url {
let _ = queries::cache::set(
state.db.conn(),
&image_cache_key,
image_source,
val,
30 * 86400,
)
.await;
}
url
};
let resp = match client.get(&api_url).send().await {
Ok(r) if r.status().is_success() => r,
_ => return (None, None),
// Fetch banner (cached per source — only for providers that support banners)
let banner_cache_key = format!("artist_banner:{image_source}:{mbid}");
let banner = if let Ok(Some(cached)) =
queries::cache::get(state.db.conn(), &banner_cache_key).await
&& !cached.is_empty()
{
Some(cached)
} else {
let url = match image_source {
"fanarttv" => match &fanart_fetcher {
Some(f) => f.get_artist_banner(info).await.unwrap_or(None),
None => None,
},
_ => None,
};
if let Some(ref val) = url {
let _ = queries::cache::set(
state.db.conn(),
&banner_cache_key,
image_source,
val,
30 * 86400,
)
.await;
}
url
};
let body: serde_json::Value = match resp.json().await {
Ok(v) => v,
Err(_) => return (None, None),
// Fetch bio (cached per source — only cache hits, not misses)
let bio_cache_key = format!("artist_bio:{bio_source}:{mbid}");
let bio = if let Ok(Some(cached)) = queries::cache::get(state.db.conn(), &bio_cache_key).await
&& !cached.is_empty()
{
Some(cached)
} else {
let text = match bio_source {
"wikipedia" => state
.wiki_fetcher
.get_artist_bio(info)
.await
.unwrap_or(None),
"lastfm" => {
if let Some(key) = lastfm_api_key {
match shanty_data::LastFmBioFetcher::new(key.to_string()) {
Ok(fetcher) => fetcher.get_artist_bio(info).await.unwrap_or(None),
Err(e) => {
tracing::warn!(error = %e, "failed to create Last.fm fetcher");
None
}
}
} else {
tracing::warn!("Last.fm bio source selected but SHANTY_LASTFM_API_KEY not set");
None
}
}
_ => None,
};
if let Some(ref val) = text {
let _ =
queries::cache::set(state.db.conn(), &bio_cache_key, bio_source, val, 30 * 86400)
.await;
}
text
};
let photo_url = body
.get("thumbnail")
.and_then(|t| t.get("source"))
.and_then(|s| s.as_str())
.map(String::from);
let bio = body
.get("extract")
.and_then(|e| e.as_str())
.map(String::from);
// Cache for 30 days
let cache_val = serde_json::json!({ "photo_url": photo_url, "bio": bio });
let _ = queries::cache::set(
state.db.conn(),
&cache_key,
"wikipedia",
&cache_val.to_string(),
30 * 86400,
)
.await;
(photo_url, bio)
}
/// Resolve a Wikidata entity ID to an English Wikipedia URL.
async fn resolve_wikidata_to_wikipedia(entity_id: &str) -> Option<String> {
if entity_id.is_empty() {
return None;
}
let url = format!(
"https://www.wikidata.org/w/api.php?action=wbgetentities&ids={entity_id}&props=sitelinks&sitefilter=enwiki&format=json"
);
let client = reqwest::Client::builder()
.user_agent("Shanty/0.1.0 (shanty-music-app)")
.build()
.ok()?;
let resp: serde_json::Value = client.get(&url).send().await.ok()?.json().await.ok()?;
let title = resp
.get("entities")
.and_then(|e| e.get(entity_id))
.and_then(|e| e.get("sitelinks"))
.and_then(|s| s.get("enwiki"))
.and_then(|w| w.get("title"))
.and_then(|t| t.as_str())?;
Some(format!(
"https://en.wikipedia.org/wiki/{}",
title.replace(' ', "_")
))
(photo_url, bio, banner)
}
async fn add_artist(

View File

@@ -2,6 +2,7 @@ use actix_session::Session;
use actix_web::{HttpResponse, web};
use serde::Deserialize;
use shanty_data::LyricsFetcher;
use shanty_db::queries;
use crate::auth;
@@ -38,59 +39,20 @@ async fn get_lyrics(
.body(json));
}
// Call LRCLIB API
let client = reqwest::Client::builder()
.user_agent("Shanty/0.1.0 (shanty-music-app)")
.build()
.map_err(|e| ApiError::Internal(e.to_string()))?;
// Use LrclibFetcher from shanty-data
let fetcher = shanty_data::LrclibFetcher::new()
.map_err(|e| ApiError::Internal(format!("failed to create lyrics fetcher: {e}")))?;
let url = format!(
"https://lrclib.net/api/search?artist_name={}&track_name={}",
urlencoded(artist),
urlencoded(title),
);
let resp = client
.get(&url)
.send()
let lyrics_result = fetcher
.get_lyrics(artist, title)
.await
.map_err(|e| ApiError::Internal(format!("LRCLIB request failed: {e}")))?;
.map_err(|e| ApiError::Internal(format!("lyrics fetch failed: {e}")))?;
if !resp.status().is_success() {
return Ok(HttpResponse::Ok().json(serde_json::json!({
"found": false,
"lyrics": null,
"synced_lyrics": null,
})));
}
let results: Vec<serde_json::Value> = resp
.json()
.await
.map_err(|e| ApiError::Internal(format!("LRCLIB parse failed: {e}")))?;
let result = if let Some(entry) = results.first() {
let plain = entry
.get("plainLyrics")
.and_then(|v| v.as_str())
.map(String::from);
let synced = entry
.get("syncedLyrics")
.and_then(|v| v.as_str())
.map(String::from);
serde_json::json!({
"found": plain.is_some() || synced.is_some(),
"lyrics": plain,
"synced_lyrics": synced,
})
} else {
serde_json::json!({
"found": false,
"lyrics": null,
"synced_lyrics": null,
})
};
let result = serde_json::json!({
"found": lyrics_result.found,
"lyrics": lyrics_result.lyrics,
"synced_lyrics": lyrics_result.synced_lyrics,
});
// Cache for 30 days
let _ = queries::cache::set(
@@ -104,10 +66,3 @@ async fn get_lyrics(
Ok(HttpResponse::Ok().json(result))
}
fn urlencoded(s: &str) -> String {
s.replace(' ', "+")
.replace('&', "%26")
.replace('=', "%3D")
.replace('#', "%23")
}

View File

@@ -32,6 +32,8 @@ struct AuthStatus {
ytdlp_version: Option<String>,
ytdlp_latest: Option<String>,
ytdlp_update_available: bool,
lastfm_api_key_set: bool,
fanart_api_key_set: bool,
}
/// GET /api/ytauth/status — check YouTube auth state.
@@ -78,6 +80,8 @@ async fn status(state: web::Data<AppState>, session: Session) -> Result<HttpResp
ytdlp_version,
ytdlp_latest,
ytdlp_update_available,
lastfm_api_key_set: config.metadata.lastfm_api_key.is_some(),
fanart_api_key_set: config.metadata.fanart_api_key.is_some(),
}))
}

View File

@@ -1,9 +1,10 @@
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock};
use shanty_data::MusicBrainzFetcher;
use shanty_data::WikipediaFetcher;
use shanty_db::Database;
use shanty_search::MusicBrainzSearch;
use shanty_tag::MusicBrainzClient;
use crate::config::AppConfig;
use crate::tasks::TaskManager;
@@ -15,8 +16,9 @@ pub struct FirefoxLoginSession {
pub struct AppState {
pub db: Database,
pub mb_client: MusicBrainzClient,
pub mb_client: MusicBrainzFetcher,
pub search: MusicBrainzSearch,
pub wiki_fetcher: WikipediaFetcher,
pub config: Arc<RwLock<AppConfig>>,
pub config_path: Option<String>,
pub tasks: TaskManager,