fix(share): fall back to song.link url lookup
This commit is contained in:
1 parent
1ccb78ef52
commit
5bf69bc877
5 files changed
+68
-26
No files matched your search
@@ -2,25 +2,41 @@
|
||||
|
||||
use tauri::State;
|
||||
|
||||
use crate::share::{ShareError, SongLinkResponse};
|
||||
use crate::share::{ContentType, ShareError, SongLinkResponse};
|
||||
use crate::AppState;
|
||||
|
||||
/// Get song.link URL for a track using ISRC
|
||||
/// Qobuz isn't supported by Odesli, so we use ISRC to find the track
|
||||
/// Get song.link URL for a track using ISRC or a direct URL fallback
|
||||
/// Qobuz isn't supported by Odesli, so we prefer ISRC but can fall back to URL
|
||||
#[tauri::command]
|
||||
pub async fn share_track_songlink(
|
||||
isrc: String,
|
||||
isrc: Option<String>,
|
||||
url: Option<String>,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<SongLinkResponse, String> {
|
||||
if isrc.is_empty() {
|
||||
return Err(ShareError::MissingIsrc.to_string());
|
||||
let isrc = isrc.unwrap_or_default().trim().to_string();
|
||||
let url = url.unwrap_or_default().trim().to_string();
|
||||
|
||||
if !isrc.is_empty() {
|
||||
log::info!("Command: share_track_songlink ISRC={}", isrc);
|
||||
match state.songlink.get_by_isrc(&isrc).await {
|
||||
Ok(result) => return Ok(result),
|
||||
Err(err) => {
|
||||
log::warn!("ISRC lookup failed: {}", err);
|
||||
if url.is_empty() {
|
||||
return Err(err.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Command: share_track_songlink ISRC={}", isrc);
|
||||
if url.is_empty() {
|
||||
return Err(ShareError::MissingIdentifier.to_string());
|
||||
}
|
||||
|
||||
log::info!("Command: share_track_songlink URL={}", url);
|
||||
state
|
||||
.songlink
|
||||
.get_by_isrc(&isrc)
|
||||
.get_by_url(&url, ContentType::Track)
|
||||
.await
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ pub enum ShareError {
|
||||
#[error("Missing ISRC for track")]
|
||||
MissingIsrc,
|
||||
|
||||
#[error("Missing ISRC or URL for track")]
|
||||
MissingIdentifier,
|
||||
|
||||
#[error("Missing UPC for album")]
|
||||
MissingUpc,
|
||||
|
||||
|
||||
@@ -8,5 +8,5 @@ pub mod models;
|
||||
pub mod songlink;
|
||||
|
||||
pub use errors::ShareError;
|
||||
pub use models::SongLinkResponse;
|
||||
pub use models::{ContentType, SongLinkResponse};
|
||||
pub use songlink::SongLinkClient;
|
||||
@@ -144,6 +144,40 @@ impl SongLinkClient {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Get song.link URL by URL (fallback when ISRC/UPC are missing)
|
||||
pub async fn get_by_url(&self, url: &str, content_type: ContentType) -> Result<SongLinkResponse, ShareError> {
|
||||
let cache_key = format!("url:{}", url);
|
||||
|
||||
if let Some(cached) = self.get_from_cache(&cache_key) {
|
||||
log::debug!("Cache hit for URL: {}", url);
|
||||
return Ok(cached);
|
||||
}
|
||||
|
||||
log::info!("Fetching song.link for URL: {}", url);
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.get(ODESLI_API_URL)
|
||||
.query(&[("url", url), ("userCountry", "US")])
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await.unwrap_or_default();
|
||||
return Err(ShareError::OdesliError(format!(
|
||||
"HTTP {}: {}",
|
||||
status, text
|
||||
)));
|
||||
}
|
||||
|
||||
let odesli: OdesliResponse = response.json().await?;
|
||||
let result = self.convert_response(odesli, url.to_string(), content_type)?;
|
||||
|
||||
self.store_in_cache(cache_key, result.clone());
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Convert Odesli response to our simplified format
|
||||
fn convert_response(
|
||||
&self,
|
||||
|
||||
Reference in new issue
Block a user