From d98a84e1d02aaae0bb28325d0e4800a5a83019f3 Mon Sep 17 00:00:00 2001 From: grufkork Date: Sun, 8 Sep 2024 23:05:21 +0200 Subject: [PATCH] Crude relink, pin dep, FLAC support (?) --- Cargo.lock | 16 ++++++++-------- Cargo.toml | 2 +- src/downloader.rs | 36 ++++++++++++++++++++++++------------ src/error.rs | 6 ++++++ src/main.rs | 6 ------ 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f715104..d6e694c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1858,7 +1858,7 @@ dependencies = [ [[package]] name = "librespot" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "data-encoding", "env_logger", @@ -1884,7 +1884,7 @@ dependencies = [ [[package]] name = "librespot-audio" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "aes", "byteorder", @@ -1906,7 +1906,7 @@ dependencies = [ [[package]] name = "librespot-connect" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "form_urlencoded", "futures-util", @@ -1926,7 +1926,7 @@ dependencies = [ [[package]] name = "librespot-core" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "aes", "base64 0.22.1", @@ -1979,7 +1979,7 @@ dependencies = [ [[package]] name = "librespot-discovery" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "aes", "base64 0.22.1", @@ -2006,7 +2006,7 @@ dependencies = [ [[package]] name = "librespot-metadata" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "async-trait", "byteorder", @@ -2024,7 +2024,7 @@ dependencies = [ [[package]] name = "librespot-playback" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "byteorder", "cpal", @@ -2047,7 +2047,7 @@ dependencies = [ [[package]] name = "librespot-protocol" version = "0.5.0-dev" -source = "git+https://github.com/librespot-org/librespot#f6473319f6309b2702a20ff5371763c48e129676" +source = "git+https://github.com/librespot-org/librespot?rev=f647331#f6473319f6309b2702a20ff5371763c48e129676" dependencies = [ "protobuf", "protobuf-codegen", diff --git a/Cargo.toml b/Cargo.toml index 4699d72..9828eba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ reqwest = "0" colored = "2" lame = "0" aspotify = "0" -librespot = { git = "https://github.com/librespot-org/librespot" } +librespot = { git = "https://github.com/librespot-org/librespot", rev = "f647331" } async-std = { version = "1", features = ["attributes", "tokio1"] } serde_json = "1" async-stream = "0" diff --git a/src/downloader.rs b/src/downloader.rs index 0696ac1..aad9858 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -201,6 +201,7 @@ async fn communication_thread( } /// Spotify downloader + pub struct DownloaderInternal { spotify: Spotify, pub tx: Sender, @@ -485,21 +486,24 @@ impl DownloaderInternal { Ok(()) } + async fn find_alternative(session: &Session, track: Track) -> Result { let librespot::metadata::track::Tracks(ids) = track.alternatives; - for alt in ids { - let t = Track::get(session, &alt).await.unwrap(); // TODO ? - let librespot::metadata::availability::Availabilities(avalabilities) = t.availability; - for a in avalabilities { - // TODO: figure out if available - } + for id in ids { + let t = Track::get(session, &id).await?; + if !Self::track_has_alternatives(&t) { + return Ok(t); + } } - - Err(SpotifyError::Unavailable) } + fn track_has_alternatives(track: &Track) -> bool { + let librespot::metadata::track::Tracks(alts) = &track.alternatives; + !alts.is_empty() + } + /// Download track by id async fn download_track( session: &Session, @@ -513,9 +517,15 @@ impl DownloaderInternal { let mut track = Track::get(session, &id).await?; // Fallback if unavailable - /*if !track.available { - track = DownloaderInternal::find_alternative(session, track).await?; - }*/ //TODO + if Self::track_has_alternatives(&track) { + track = Self::find_alternative(session, track).await?; + } + + + + // if !track.available { + // track = DownloaderInternal::find_alternative(session, track).await?; + // } //TODO // Quality fallback let mut quality = config.quality; @@ -689,6 +699,7 @@ pub enum AudioFormat { Aac, Mp3, Mp4, + Flac, Unknown, } @@ -700,6 +711,7 @@ impl AudioFormat { AudioFormat::Aac => "m4a", AudioFormat::Mp3 => "mp3", AudioFormat::Mp4 => "mp4", + AudioFormat::Flac => "flac", AudioFormat::Unknown => "", } .to_string() @@ -719,7 +731,7 @@ impl From for AudioFormat { FileFormat::MP3_160_ENC => Self::Mp3, FileFormat::AAC_24 => Self::Aac, FileFormat::AAC_48 => Self::Aac, - FileFormat::FLAC_FLAC => Self::Unknown + FileFormat::FLAC_FLAC => Self::Flac } } } diff --git a/src/error.rs b/src/error.rs index 7fb7147..2920a76 100644 --- a/src/error.rs +++ b/src/error.rs @@ -68,6 +68,12 @@ impl From for SpotifyError { } } +impl From for SpotifyError { + fn from(e: librespot::core::error::Error) -> Self { + SpotifyError::Error(e.to_string()) + } +} + impl From for SpotifyError { fn from(e: librespot::core::session::SessionError) -> Self { match e { diff --git a/src/main.rs b/src/main.rs index a1955d8..4c3fe2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,12 +27,6 @@ async fn main() { start().await; } -impl From for SpotifyError { - fn from(e: librespot::core::error::Error) -> Self { - SpotifyError::Error(e.to_string()) - } -} - #[cfg(windows)] #[tokio::main] async fn main() {