diff --git a/Cargo.lock b/Cargo.lock index 6bb66fb..701675d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1172,6 +1172,7 @@ dependencies = [ "futures", "icarus_envy", "icarus_models", + "rand", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d2052b5..e4d6182 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ serde = { version = "1.0.218", features = ["derive"] } serde_json = { version = "1.0.139" } time = { version = "0.3.41", features = ["macros", "serde"] } uuid = { version = "1.16.0", features = ["v4", "serde"] } +rand = { version = "0.9" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" } icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.2.2-devel-84ea6e4c22-006" } diff --git a/src/main.rs b/src/main.rs index 23cfb11..5206990 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,10 +59,10 @@ async fn process_song(api_url: &String, song_queue_id: &uuid::Uuid) -> Result<() match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await { Ok(response) => { // Process data here... - match api::fetch_song_queue_data::response::parse_response(response).await { - Ok(all_bytes) => { + match api::parsing::parse_response_into_bytes(response).await { + Ok(song_bytes) => { let (directory, filename) = generate_song_queue_dir_and_filename().await; - let save_path = save_song_to_fs(&directory, &filename, &all_bytes).await; + let save_path = save_file_to_fs(&directory, &filename, &song_bytes).await; println!("Saved at: {:?}", save_path); @@ -88,13 +88,30 @@ async fn process_song(api_url: &String, song_queue_id: &uuid::Uuid) -> Result<() Ok(response) => { let coverart_queue_id = &response.data[0].id; println!("Coverart queue Id: {:?}", coverart_queue_id); - // TODO: Get queued coverart's data - // TODO: Apply metadata to the queued song (modifying file) - // 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 + + match api::get_coverart_queue::get_data(api_url, coverart_queue_id).await { + Ok(response) => match api::parsing::parse_response_into_bytes(response).await { + Ok(coverart_queue_bytes) => { + let (directory, filename) = generate_coverart_queue_dir_and_filename().await; + let save_path = save_file_to_fs(&directory, &filename, &coverart_queue_bytes).await; + + println!("Saved coverart queue file at: {:?}", save_path); + + // TODO: Apply metadata to the queued song (modifying file) + // 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: {:?}", err); + } + } + Err(err) => { + eprintln!("Error: {:?}", err); + } + } } Err(err) => { eprintln!("Error: {:?}", err); @@ -136,8 +153,37 @@ pub async fn generate_song_queue_dir_and_filename() -> (String, String) { (song.directory, song.filename) } +// TODO: Consider having something like this in icarus_models +pub async fn generate_coverart_queue_dir_and_filename() -> (String, String) { + use rand::Rng; + + let mut filename: String = String::new(); + let filename_len = 10; + + let some_chars: String = String::from("abcdefghij0123456789"); + let mut rng = rand::rng(); + + for _i in 0..filename_len { + let random_number: i32 = rng.random_range(0..=19); + let index = random_number as usize; + let rando_char = some_chars.chars().nth(index); + + if let Some(c) = rando_char { + filename.push(c); + } + } + + // TODO: Do not hard code the file extension + filename += ".jpeg"; + + // TODO: Consider separating song and coverart when saving to the filesystem + let directory = icarus_envy::environment::get_root_directory().await; + + (directory, filename) +} + // TODO: Check to see if this is available in icarus_models -pub async fn save_song_to_fs( +pub async fn save_file_to_fs( directory: &String, filename: &String, data: &[u8], @@ -182,6 +228,26 @@ mod api { client.get(api_url).send().await } + pub mod parsing { + use futures::StreamExt; + + pub async fn parse_response_into_bytes( + response: reqwest::Response, + ) -> Result, reqwest::Error> { + // 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); + } + + Ok(all_bytes) + } + } + pub mod fetch_song_queue_data { pub async fn get_data( base_url: &String, @@ -192,26 +258,6 @@ mod api { let api_url = format!("{}/{}/{}", base_url, endpoint, id); client.get(api_url).send().await } - - pub mod response { - use futures::StreamExt; - - pub async fn parse_response( - response: reqwest::Response, - ) -> Result, reqwest::Error> { - // 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); - } - - Ok(all_bytes) - } - } } pub mod get_metadata_queue { @@ -234,7 +280,7 @@ mod api { #[derive(Debug, Deserialize, Serialize)] pub struct Metadata { - pub id: uuid::Uuid, + pub song_queue_id: uuid::Uuid, pub album: String, pub album_artist: String, pub artist: String, @@ -280,6 +326,16 @@ mod api { .await } + pub async fn get_data( + base_url: &String, + coverart_queue_id: &uuid::Uuid, + ) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/coverart/queue/data"); + let api_url = format!("{}/{}/{}", base_url, endpoint, coverart_queue_id); + client.get(api_url).send().await + } + pub mod response { use serde::{Deserialize, Serialize};