proper response
This commit is contained in:
+21
-26
@@ -1,6 +1,8 @@
|
||||
mod workers;
|
||||
|
||||
use futures::StreamExt;
|
||||
use postcard::from_bytes;
|
||||
use types::jobs::Job;
|
||||
use postcard::{from_bytes, to_stdvec};
|
||||
use types::jobs::JobResponse;
|
||||
use which::which;
|
||||
use yt_dlp::{Downloader, client::Libraries};
|
||||
|
||||
@@ -11,7 +13,7 @@ async fn main() {
|
||||
.expect("Error creating nats client");
|
||||
|
||||
let mut subscriber = nats_client
|
||||
.queue_subscribe("corro-dj.*", "download".to_string())
|
||||
.queue_subscribe("corro-dj.*", "group1".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -25,38 +27,31 @@ async fn main() {
|
||||
// Receive and process messages
|
||||
while let Some(message) = subscriber.next().await {
|
||||
println!("Received message {:?}", message);
|
||||
let result: Job = from_bytes(&message.payload).unwrap();
|
||||
println!("{:?}", result);
|
||||
|
||||
let video = match downloader.fetch_video_infos(result.inner.str).await {
|
||||
Ok(video) => video,
|
||||
Err(why) => {
|
||||
println!("{:?}", why);
|
||||
let subject = message.subject.split(".").collect::<Vec<&str>>()[1];
|
||||
|
||||
let result = match subject {
|
||||
"download" => {
|
||||
workers::download::download(&downloader, from_bytes(&message.payload).unwrap())
|
||||
.await
|
||||
}
|
||||
_ => {
|
||||
println!("{subject}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let audio_path = match downloader
|
||||
.download_audio_stream_with_quality(
|
||||
&video,
|
||||
format!("{}.opus", video.id),
|
||||
yt_dlp::model::AudioQuality::Best,
|
||||
yt_dlp::model::AudioCodecPreference::Opus,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(path) => path,
|
||||
Err(why) => {
|
||||
println!("{:?}", why);
|
||||
continue;
|
||||
}
|
||||
let response = match result {
|
||||
Ok(response) => response,
|
||||
Err(err) => JobResponse {
|
||||
content: None,
|
||||
error: Some(format!("{err}")),
|
||||
},
|
||||
};
|
||||
|
||||
println!("reply: {:?}", audio_path);
|
||||
|
||||
if let Some(reply) = message.reply {
|
||||
nats_client
|
||||
.publish(reply, format!("{:?}", audio_path).into())
|
||||
.publish(reply, to_stdvec(&response).unwrap().into())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user