diff --git a/src/main.rs b/src/main.rs index 8a0e0ab..adbe5f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +use std::io::Write; + +use futures::StreamExt; + #[tokio::main] async fn main() -> Result<(), Box> { let app_base_url = get_icarus_url().await; @@ -15,19 +19,53 @@ async fn main() -> Result<(), Box> { if !song_queue_item.data.is_empty() { println!("Song queue item: {:?}", song_queue_item); - // Process data here... + match api::fetch_song_queue_data::get_data(&app_base_url, &song_queue_item.data[0].id).await { + Ok(response) => { + // TODO: At some point, handle the flow if the size is small or + // large + let mut byte_stream = response.bytes_stream(); + let mut all_bytes = Vec::new(); + + while let Some(chunk) = byte_stream.next().await { + let chunk = chunk?; + all_bytes.extend_from_slice(&chunk); + } + + let mut song = icarus_models::song::Song::default(); + song.data = all_bytes; + song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true); + + // TODO: Add function to save bytes to a file in icarus_models + + song.directory = icarus_envy::environment::get_root_directory().await; + + let dir = std::path::Path::new(&song.directory); + let save_path = dir.join(&song.filename); + + let mut file = std::fs::File::create(&save_path).unwrap(); + file.write_all(&song.data).unwrap(); + println!("File saved to: {:?}", save_path); + +// Process data here... + +// TODO: Parse the response body to a struct +// TODO: Get queued song data +// TODO: Get queued song's metadata +// TODO: Get queued coverart +// TODO: Get queued coverart's data +// TODO: Apply metadata to the queued song +// TODO: Update the queued song with the updated queued song +// TODO: Create song +// TODO: Create coverart +// TODO: Wipe data from queued song +// TODO: Wipe data from queued coverart + + } + Err(err) => { + eprintln!("Error fetching song queue data: {:?}", err); + } + } - // TODO: Parse the response body to a struct - // TODO: Get queued song data - // TODO: Get queued song's metadata - // TODO: Get queued coverart - // TODO: Get queued coverart's data - // TODO: Apply metadata to the queued song - // TODO: Update the queued song with the updated queued song - // TODO: Create song - // TODO: Create coverart - // TODO: Wipe data from queued song - // TODO: Wipe data from queued coverart } else { println!("No data to fetch"); } @@ -73,6 +111,21 @@ mod api { let api_url = format!("{}/{}", base_url, fetch_endpoint); client.get(api_url).send().await } + + pub mod fetch_song_queue_data { + pub async fn get_data(base_url: &String, id: &uuid::Uuid) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/song/queue"); + let api_url = format!("{}/{}/{}", base_url, endpoint, id); + client.get(api_url).send().await + } + + pub mod response { + // use serde::{Deserialize, Serialize}; + // pub struct Response { + // } + } + } } async fn get_icarus_url() -> String {