diff --git a/.gitignore b/.gitignore index 1b551e192..1fbf6662e 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,8 @@ static/*upscayl* # Local build temp /.tmp/ +/tmp/ +/tools/ # Documentation (kept external) qbz-nix-docs/ diff --git a/src-tauri/src/api/client.rs b/src-tauri/src/api/client.rs index e8591787b..75d05bc7e 100644 --- a/src-tauri/src/api/client.rs +++ b/src-tauri/src/api/client.rs @@ -1036,8 +1036,145 @@ impl QobuzClient { Ok(serde_json::from_value(response)?) } + /// Get one purchases page from Qobuz. + pub async fn get_user_purchases_page(&self, limit: u32, offset: u32) -> Result { + let url = endpoints::build_url(paths::PURCHASE_GET_USER_PURCHASES); + let http_response = self + .http + .get(&url) + .headers(self.authenticated_headers().await?) + .query(&[ + ("limit", limit.to_string()), + ("offset", offset.to_string()), + ]) + .send() + .await?; + log::debug!( + "[API] get_user_purchases_page(limit={}, offset={}) status={}", + limit, + offset, + http_response.status() + ); + let response: Value = http_response.json().await?; + Ok(serde_json::from_value(response)?) + } + + /// Get all purchases by paginating through Qobuz purchases API. + pub async fn get_user_purchases_all(&self) -> Result { + let page_limit = 500u32; + let mut offset = 0u32; + + let mut all_albums: Vec = Vec::new(); + let mut all_tracks: Vec = Vec::new(); + let mut albums_total: u32; + let mut tracks_total: u32; + let mut totals_initialized = false; + + loop { + let page = self.get_user_purchases_page(page_limit, offset).await?; + if !totals_initialized { + albums_total = page.albums.total; + tracks_total = page.tracks.total; + totals_initialized = true; + } else { + albums_total = page.albums.total; + tracks_total = page.tracks.total; + } + + let got = page.albums.items.len() as u32; + all_albums.extend(page.albums.items); + all_tracks.extend(page.tracks.items); + + if got == 0 || offset + got >= albums_total { + break; + } + offset += got; + } + + Ok(PurchaseResponse { + albums: SearchResultsPage { + items: all_albums, + total: if totals_initialized { albums_total } else { 0 }, + offset: 0, + limit: page_limit, + }, + tracks: SearchResultsPage { + items: all_tracks, + total: if totals_initialized { tracks_total } else { 0 }, + offset: 0, + limit: page_limit, + }, + }) + } + // === Authenticated endpoints === + /// Get a signed file URL for a specific format_id. + pub async fn get_track_file_url_by_format( + &self, + track_id: u64, + format_id: u32, + ) -> Result { + let url = endpoints::build_url(paths::TRACK_GET_FILE_URL); + let timestamp = get_timestamp(); + let secret = self.secret().await?; + let signature = sign_get_file_url(track_id, format_id, timestamp, &secret); + + let response = self + .http + .get(&url) + .headers(self.authenticated_headers().await?) + .query(&[ + ("track_id", track_id.to_string()), + ("format_id", format_id.to_string()), + ("intent", "stream".to_string()), + ("request_ts", timestamp.to_string()), + ("request_sig", signature), + ]) + .send() + .await?; + + match response.status() { + StatusCode::OK => { + let json: Value = response.json().await?; + + let restrictions: Vec = json + .get("restrictions") + .and_then(|v| serde_json::from_value(v.clone()).ok()) + .unwrap_or_default(); + + let stream_url = json + .get("url") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(); + + if stream_url.is_empty() { + return Err(ApiError::TrackUnavailable(track_id)); + } + + Ok(StreamUrl { + url: stream_url, + format_id: json.get("format_id").and_then(|v| v.as_u64()).unwrap_or(0) as u32, + mime_type: json + .get("mime_type") + .and_then(|v| v.as_str()) + .unwrap_or("") + .to_string(), + sampling_rate: json + .get("sampling_rate") + .and_then(|v| v.as_f64()) + .unwrap_or(0.0), + bit_depth: json.get("bit_depth").and_then(|v| v.as_u64()).map(|v| v as u32), + track_id, + restrictions, + }) + } + StatusCode::BAD_REQUEST => Err(ApiError::InvalidAppSecret), + status => Err(ApiError::ApiResponse(format!("Unexpected status: {}", status))), + } + } + /// Get stream URL for a track (requires auth + signature) pub async fn get_stream_url(&self, track_id: u64, quality: Quality) -> Result { log::info!("Getting stream URL for track {} with quality {:?}", track_id, quality); diff --git a/src-tauri/src/api/endpoints.rs b/src-tauri/src/api/endpoints.rs index 796b41a48..4fd0712dd 100644 --- a/src-tauri/src/api/endpoints.rs +++ b/src-tauri/src/api/endpoints.rs @@ -6,6 +6,7 @@ pub const BASE_URL: &str = "https://www.qobuz.com/api.json/0.2"; pub mod paths { // User pub const USER_LOGIN: &str = "/user/login"; + pub const PURCHASE_GET_USER_PURCHASES: &str = "/purchase/getUserPurchases"; // Track pub const TRACK_GET: &str = "/track/get"; diff --git a/src-tauri/src/api/models.rs b/src-tauri/src/api/models.rs index 7caa2dab3..4f140ec6c 100644 --- a/src-tauri/src/api/models.rs +++ b/src-tauri/src/api/models.rs @@ -2,6 +2,10 @@ use serde::{Deserialize, Serialize}; +fn serde_true() -> bool { + true +} + /// Audio quality format IDs #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[repr(u32)] @@ -377,6 +381,61 @@ pub struct SearchResultsPage { pub limit: u32, } +// ============ Purchases API Models ============ + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PurchaseResponse { + pub albums: SearchResultsPage, + pub tracks: SearchResultsPage, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PurchaseAlbum { + pub id: String, + pub title: String, + #[serde(default)] + pub artist: Artist, + #[serde(default)] + pub image: ImageSet, + pub release_date_original: Option, + pub label: Option