diff --git a/src/player/current.rs b/src/player/current.rs index ac9e055..dc7ffe4 100644 --- a/src/player/current.rs +++ b/src/player/current.rs @@ -3,6 +3,7 @@ use image::DynamicImage; use crate::{ssonic::response::Song, utils::default_cover}; pub struct Metadata { + pub id: String, pub name: String, pub artist: Option, pub playing: bool, @@ -16,6 +17,7 @@ pub struct Metadata { impl Metadata { pub fn new() -> Metadata { Metadata { + id: String::new(), name: String::new(), artist: None, playing: false, @@ -32,6 +34,7 @@ impl Metadata { } pub fn update_metadata(&mut self, song: Song) { + self.id = song.id; self.name = song.title; self.artist = song.artist; self.current_time = 0; diff --git a/src/player/mod.rs b/src/player/mod.rs index c2246e6..fc570ca 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -21,6 +21,7 @@ pub enum PlayerEvent { UpdateCover(DynamicImage), UserQuit, PlayNext, + AddAudioChunks(Vec), } pub struct Player { diff --git a/src/player/player.rs b/src/player/player.rs index 769a449..8cd083e 100644 --- a/src/player/player.rs +++ b/src/player/player.rs @@ -80,8 +80,8 @@ impl Player { self.player_chan_in .send(PlayerEvent::UpdateCover(default_cover()))?; } - self.fetch_audio_chunk(song.id.clone())?; self.tui_root.metadata.update_metadata(song); + self.fetch_audio_chunk(self.tui_root.metadata.id.clone())?; Ok(()) } diff --git a/src/ssonic/client.rs b/src/ssonic/client.rs index c4d0821..cc79603 100644 --- a/src/ssonic/client.rs +++ b/src/ssonic/client.rs @@ -42,7 +42,22 @@ impl APIClient { header.insert(RANGE, range_header.parse()?); let mut response = self.request("/stream", Some(&[("id", id)]), Some(header))?; - unimplemented!() + if !response.status().is_success() { + return Err(Box::new(APIError::StatusError( + response.status(), + "/stream", + ))); + }; + + if response.headers()[CONTENT_TYPE].to_str()? == "application/json" { + self.validate(response, "/stream")?; // will never error if not image + } else { + eprintln!("Fetched chunk"); + let mut audio_chunk = Vec::new(); + response.read_to_end(&mut audio_chunk)?; + return Ok(PlayerEvent::AddAudioChunks(audio_chunk)); + } + unreachable!(); } fn get_cover_art(&mut self, cover_id: String) -> Result { @@ -65,7 +80,7 @@ impl APIClient { )?; return Ok(PlayerEvent::UpdateCover(cover)); } - unimplemented!() + unreachable!(); } fn get_random(&mut self, n: i32) -> Result {