Compare commits
3 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
29b806fd02 | |||
557264482f | |||
4e07ee5d0f |
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1172,6 +1172,7 @@ dependencies = [
|
|||||||
"futures",
|
"futures",
|
||||||
"icarus_envy",
|
"icarus_envy",
|
||||||
"icarus_models",
|
"icarus_models",
|
||||||
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@@ -12,5 +12,6 @@ serde = { version = "1.0.218", features = ["derive"] }
|
|||||||
serde_json = { version = "1.0.139" }
|
serde_json = { version = "1.0.139" }
|
||||||
time = { version = "0.3.41", features = ["macros", "serde"] }
|
time = { version = "0.3.41", features = ["macros", "serde"] }
|
||||||
uuid = { version = "1.16.0", features = ["v4", "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_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" }
|
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.2.2-devel-84ea6e4c22-006" }
|
||||||
|
221
src/main.rs
221
src/main.rs
@@ -9,47 +9,69 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
loop {
|
loop {
|
||||||
println!("Base URL: {}", app_base_url);
|
println!("Base URL: {}", app_base_url);
|
||||||
|
|
||||||
match api::fetch_next_queue_item(&app_base_url).await {
|
match is_queue_empty(&app_base_url).await {
|
||||||
|
Ok((empty, song_queue_item)) => {
|
||||||
|
if !empty {
|
||||||
|
println!("Queue is not empty");
|
||||||
|
println!("SongQueueItem: {:?}", song_queue_item);
|
||||||
|
let song_queue_id = song_queue_item.data[0].id;
|
||||||
|
|
||||||
|
// TODO: Do something with the result later
|
||||||
|
let _ = process_song(&app_base_url, &song_queue_id).await;
|
||||||
|
} else {
|
||||||
|
println!("Queue is empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error checking if queue is empty: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Sleeping");
|
||||||
|
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn is_queue_empty(
|
||||||
|
api_url: &String,
|
||||||
|
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
||||||
|
match api::fetch_next_queue_item(api_url).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
match response
|
match response
|
||||||
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(song_queue_item) => {
|
Ok(response) => {
|
||||||
if !song_queue_item.data.is_empty() {
|
if response.data.is_empty() {
|
||||||
println!("Song queue item: {:?}", song_queue_item);
|
Ok((true, response))
|
||||||
let song_queue_id = song_queue_item.data[0].id;
|
} else {
|
||||||
|
Ok((false, response))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!("Fetching song queue data");
|
async fn process_song(api_url: &String, song_queue_id: &uuid::Uuid) -> Result<(), reqwest::Error> {
|
||||||
match api::fetch_song_queue_data::get_data(
|
match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await {
|
||||||
&app_base_url,
|
|
||||||
&song_queue_id,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
// Process data here...
|
// Process data here...
|
||||||
let all_bytes =
|
match api::parsing::parse_response_into_bytes(response).await {
|
||||||
api::fetch_song_queue_data::response::parse_response(
|
Ok(song_bytes) => {
|
||||||
response,
|
let (directory, filename) = generate_song_queue_dir_and_filename().await;
|
||||||
)
|
let save_path = save_file_to_fs(&directory, &filename, &song_bytes).await;
|
||||||
.await?;
|
|
||||||
|
|
||||||
let (directory, filename) =
|
|
||||||
generate_song_queue_dir_and_filename().await;
|
|
||||||
let save_path =
|
|
||||||
save_song_to_fs(&directory, &filename, &all_bytes).await;
|
|
||||||
|
|
||||||
println!("Saved at: {:?}", save_path);
|
println!("Saved at: {:?}", save_path);
|
||||||
|
|
||||||
match api::get_metadata_queue::get(
|
match api::get_metadata_queue::get(api_url, song_queue_id).await {
|
||||||
&app_base_url,
|
Ok(response) => {
|
||||||
&song_queue_id,
|
match response
|
||||||
)
|
.json::<api::get_metadata_queue::response::Response>()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(response) => {
|
|
||||||
match response.json::<api::get_metadata_queue::response::Response>().await {
|
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let id = &response.data[0].id;
|
let id = &response.data[0].id;
|
||||||
let metadata = &response.data[0].metadata;
|
let metadata = &response.data[0].metadata;
|
||||||
@@ -57,8 +79,24 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
println!("Id: {:?}", id);
|
println!("Id: {:?}", id);
|
||||||
println!("Metadata: {:?}", metadata);
|
println!("Metadata: {:?}", metadata);
|
||||||
println!("Created at: {:?}", created_at);
|
println!("Created at: {:?}", created_at);
|
||||||
// TODO: Get queued coverart
|
|
||||||
// TODO: Get queued coverart's data
|
println!("Getting coverart queue");
|
||||||
|
match api::get_coverart_queue::get(api_url, song_queue_id).await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
match response.json::<api::get_coverart_queue::response::Response>().await {
|
||||||
|
Ok(response) => {
|
||||||
|
let coverart_queue_id = &response.data[0].id;
|
||||||
|
println!("Coverart queue Id: {:?}", coverart_queue_id);
|
||||||
|
|
||||||
|
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: Apply metadata to the queued song (modifying file)
|
||||||
// TODO: Update the queued song with the updated queued song
|
// TODO: Update the queued song with the updated queued song
|
||||||
// TODO: Create song
|
// TODO: Create song
|
||||||
@@ -70,6 +108,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
eprintln!("Error: {:?}", err);
|
eprintln!("Error: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {:?}", err);
|
eprintln!("Error: {:?}", err);
|
||||||
@@ -77,23 +119,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error fetching song queue data: {:?}", err);
|
eprintln!("Error: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
Ok(())
|
||||||
println!("No data to fetch");
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {:?}", err);
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {:?}", err);
|
eprintln!("Error: {:?}", err);
|
||||||
|
Err(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!("API call failed: {}", e),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
println!("Sleeping");
|
Err(err) => Err(err),
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,8 +153,37 @@ pub async fn generate_song_queue_dir_and_filename() -> (String, String) {
|
|||||||
(song.directory, song.filename)
|
(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
|
// 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,
|
directory: &String,
|
||||||
filename: &String,
|
filename: &String,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
@@ -153,21 +228,10 @@ mod api {
|
|||||||
client.get(api_url).send().await
|
client.get(api_url).send().await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod fetch_song_queue_data {
|
pub mod parsing {
|
||||||
pub async fn get_data(
|
|
||||||
base_url: &String,
|
|
||||||
id: &uuid::Uuid,
|
|
||||||
) -> Result<reqwest::Response, reqwest::Error> {
|
|
||||||
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 futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
|
||||||
pub async fn parse_response(
|
pub async fn parse_response_into_bytes(
|
||||||
response: reqwest::Response,
|
response: reqwest::Response,
|
||||||
) -> Result<Vec<u8>, reqwest::Error> {
|
) -> Result<Vec<u8>, reqwest::Error> {
|
||||||
// TODO: At some point, handle the flow if the size is small or
|
// TODO: At some point, handle the flow if the size is small or
|
||||||
@@ -183,6 +247,17 @@ mod api {
|
|||||||
Ok(all_bytes)
|
Ok(all_bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod fetch_song_queue_data {
|
||||||
|
pub async fn get_data(
|
||||||
|
base_url: &String,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
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 get_metadata_queue {
|
pub mod get_metadata_queue {
|
||||||
@@ -205,7 +280,7 @@ mod api {
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct Metadata {
|
pub struct Metadata {
|
||||||
pub id: uuid::Uuid,
|
pub song_queue_id: uuid::Uuid,
|
||||||
pub album: String,
|
pub album: String,
|
||||||
pub album_artist: String,
|
pub album_artist: String,
|
||||||
pub artist: String,
|
pub artist: String,
|
||||||
@@ -235,4 +310,46 @@ mod api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod get_coverart_queue {
|
||||||
|
pub async fn get(
|
||||||
|
base_url: &String,
|
||||||
|
song_queue_id: &uuid::Uuid,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let endpoint = String::from("api/v2/coverart/queue");
|
||||||
|
let api_url = format!("{}/{}", base_url, endpoint);
|
||||||
|
client
|
||||||
|
.get(api_url)
|
||||||
|
.query(&[("song_queue_id", song_queue_id)])
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_data(
|
||||||
|
base_url: &String,
|
||||||
|
coverart_queue_id: &uuid::Uuid,
|
||||||
|
) -> Result<reqwest::Response, reqwest::Error> {
|
||||||
|
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};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct CoverArtQueue {
|
||||||
|
pub id: uuid::Uuid,
|
||||||
|
pub song_queue_id: uuid::Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<CoverArtQueue>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user