Compare commits
9 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date | |
---|---|---|---|
79eea81e28 | |||
006fa3a2d8 | |||
2f11ff9e89 | |||
5ced62ec7a | |||
817ea9fed2 | |||
ce1d522814 | |||
c04c6cdd6c | |||
f1375f5639 | |||
b41933e05e |
2
.env.docker.sample
Normal file
2
.env.docker.sample
Normal file
@@ -0,0 +1,2 @@
|
||||
ROOT_DIRECTORY=/home/songparser/mydata
|
||||
ICARUS_BASE_API_URL=http://localhost:3000
|
2
.env.sample
Normal file
2
.env.sample
Normal file
@@ -0,0 +1,2 @@
|
||||
ROOT_DIRECTORY=/home/songparser/mydata
|
||||
ICARUS_BASE_API_URL=http://localhost:3000
|
@@ -18,7 +18,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
@@ -36,7 +36,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
@@ -54,7 +54,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
- run: rustup component add rustfmt
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
@@ -73,7 +73,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
- run: rustup component add clippy
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
@@ -92,7 +92,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
.env
|
||||
|
1901
Cargo.lock
generated
Normal file
1901
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,14 @@
|
||||
name = "songparser"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.86"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.44.1", features = ["full"] }
|
||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" }
|
||||
futures = { version = "0.3.31" }
|
||||
reqwest = { version = "0.12.19", features = ["json", "stream"] }
|
||||
serde = { version = "1.0.218", features = ["derive"] }
|
||||
serde_json = { version = "1.0.139" }
|
||||
uuid = { version = "1.16.0", features = ["v4", "serde"] }
|
||||
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" }
|
||||
|
168
src/main.rs
168
src/main.rs
@@ -1,38 +1,162 @@
|
||||
use std::error::Error;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::spawn;
|
||||
use std::io::Write;
|
||||
|
||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let listener = TcpListener::bind("127.0.0.1:8080").await?;
|
||||
println!("API calling service listening on 127.0.0.1:8080");
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let app_base_url = icarus_envy::environment::get_icarus_base_api_url().await;
|
||||
|
||||
loop {
|
||||
let (stream, addr) = listener.accept().await?;
|
||||
println!("Accepted connection from: {}", addr);
|
||||
println!("Base URL: {}", app_base_url);
|
||||
|
||||
spawn(async move {
|
||||
if let Err(e) = handle_connection(stream).await {
|
||||
eprintln!("Error handling connection from {}: {}", addr, e);
|
||||
match api::fetch_next_queue_item(&app_base_url).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
||||
.await
|
||||
{
|
||||
Ok(song_queue_item) => {
|
||||
if !song_queue_item.data.is_empty() {
|
||||
println!("Song queue item: {:?}", song_queue_item);
|
||||
|
||||
println!("Fetching song queue data");
|
||||
match api::fetch_song_queue_data::get_data(
|
||||
&app_base_url,
|
||||
&song_queue_item.data[0].id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
// Process data here...
|
||||
let all_bytes =
|
||||
api::fetch_song_queue_data::response::parse_response(
|
||||
response,
|
||||
)
|
||||
.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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("No data to fetch");
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => eprintln!("API call failed: {}", e),
|
||||
}
|
||||
|
||||
println!("Sleeping");
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_connection(mut stream: TcpStream) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
let mut buffer = [0; 1024];
|
||||
// TODO: Consider having something like this in icarus_models
|
||||
pub async fn generate_song_queue_dir_and_filename() -> (String, String) {
|
||||
let mut song = icarus_models::song::Song::default();
|
||||
song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
||||
|
||||
loop {
|
||||
let n = stream.read(&mut buffer).await?;
|
||||
song.directory = icarus_envy::environment::get_root_directory().await;
|
||||
|
||||
if n == 0 {
|
||||
break; // Connection closed
|
||||
(song.directory, song.filename)
|
||||
}
|
||||
|
||||
let request_data = String::from_utf8_lossy(&buffer[..n]).trim().to_string();
|
||||
println!("Received request: {}", request_data);
|
||||
// TODO: Check to see if this is available in icarus_models
|
||||
pub async fn save_song_to_fs(
|
||||
directory: &String,
|
||||
filename: &String,
|
||||
data: &[u8],
|
||||
) -> std::path::PathBuf {
|
||||
// TODO: Add function to save bytes to a file in icarus_models
|
||||
// repo
|
||||
let dir = std::path::Path::new(directory);
|
||||
let save_path = dir.join(filename);
|
||||
|
||||
let mut file = std::fs::File::create(&save_path).unwrap();
|
||||
file.write_all(data).unwrap();
|
||||
|
||||
save_path
|
||||
}
|
||||
|
||||
Ok(())
|
||||
mod responses {
|
||||
pub mod fetch_next_queue_item {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct QueueItem {
|
||||
pub id: uuid::Uuid,
|
||||
pub filename: String,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct SongQueueItem {
|
||||
pub message: String,
|
||||
pub data: Vec<QueueItem>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod api {
|
||||
pub async fn fetch_next_queue_item(
|
||||
base_url: &String,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
||||
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<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;
|
||||
|
||||
pub async fn parse_response(
|
||||
response: reqwest::Response,
|
||||
) -> Result<Vec<u8>, 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user