wip
This commit is contained in:
+27
-12
@@ -1,5 +1,7 @@
|
|||||||
mod workers;
|
mod workers;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use postcard::{from_bytes, to_stdvec};
|
use postcard::{from_bytes, to_stdvec};
|
||||||
use types::jobs::JobResponse;
|
use types::jobs::JobResponse;
|
||||||
@@ -8,6 +10,21 @@ use yt_dlp::{Downloader, client::Libraries};
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
||||||
|
|
||||||
|
let mut client = Client::builder(&token, intents)
|
||||||
|
.event_handler(Handler)
|
||||||
|
.framework(framework)
|
||||||
|
.register_songbird()
|
||||||
|
// We insert our own HTTP client here to make use of in
|
||||||
|
// `~play`. If we wanted, we could supply cookies and auth
|
||||||
|
// details ahead of time.
|
||||||
|
//
|
||||||
|
// Generally, we don't want to make a new Client for every request!
|
||||||
|
.type_map_insert::<HttpKey>(HttpClient::new())
|
||||||
|
.await
|
||||||
|
.expect("Err creating client");
|
||||||
|
|
||||||
let nats_client = async_nats::connect("nats://localhost:4222")
|
let nats_client = async_nats::connect("nats://localhost:4222")
|
||||||
.await
|
.await
|
||||||
.expect("Error creating nats client");
|
.expect("Error creating nats client");
|
||||||
@@ -17,12 +34,13 @@ async fn main() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let libraries = Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap());
|
let yt_downloader = Downloader::builder(
|
||||||
|
Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap()),
|
||||||
let downloader = Downloader::builder(libraries, "output")
|
"output",
|
||||||
.build()
|
)
|
||||||
.await
|
.build()
|
||||||
.unwrap();
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// Receive and process messages
|
// Receive and process messages
|
||||||
while let Some(message) = subscriber.next().await {
|
while let Some(message) = subscriber.next().await {
|
||||||
@@ -32,20 +50,17 @@ async fn main() {
|
|||||||
|
|
||||||
let result = match subject {
|
let result = match subject {
|
||||||
"download" => {
|
"download" => {
|
||||||
workers::download::download(&downloader, from_bytes(&message.payload).unwrap())
|
workers::download::download(&yt_downloader, from_bytes(&message.payload).unwrap())
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
_ => {
|
_ => Err(format!("subject {subject} does not exists")),
|
||||||
println!("{subject}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = match result {
|
let response = match result {
|
||||||
Ok(response) => response,
|
Ok(response) => response,
|
||||||
Err(err) => JobResponse {
|
Err(err) => JobResponse {
|
||||||
content: None,
|
content: None,
|
||||||
error: Some(format!("{err}")),
|
error: Some(err),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
use types::jobs::{DownloadJob, DownloadResponse, JobResponse};
|
use types::jobs::{DownloadJob, DownloadResponse, JobResponse};
|
||||||
use yt_dlp::{Downloader, error::Error};
|
use yt_dlp::Downloader;
|
||||||
|
|
||||||
pub async fn download(
|
pub async fn download(
|
||||||
downloader: &Downloader,
|
downloader: &Downloader,
|
||||||
job: DownloadJob,
|
job: DownloadJob,
|
||||||
) -> Result<JobResponse<DownloadResponse>, Error> {
|
) -> Result<JobResponse<DownloadResponse>, String> {
|
||||||
println!("job: {:?}", job);
|
println!("job: {:?}", job);
|
||||||
|
|
||||||
let video = match downloader.fetch_video_infos(job.url).await {
|
let video = match downloader.fetch_video_infos(job.url).await {
|
||||||
Ok(video) => video,
|
Ok(video) => video,
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let audio_path = match downloader
|
let audio_path = match downloader
|
||||||
@@ -22,7 +22,7 @@ pub async fn download(
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(path) => path,
|
Ok(path) => path,
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("reply: {:?}", audio_path);
|
println!("reply: {:?}", audio_path);
|
||||||
|
|||||||
Reference in New Issue
Block a user