implement bot command to download video
This commit is contained in:
parent
7fb721496a
commit
72a7eb0239
2 changed files with 39 additions and 2 deletions
|
|
@ -7,8 +7,12 @@ use std::time::Duration;
|
||||||
use teloxide::dispatching::dialogue;
|
use teloxide::dispatching::dialogue;
|
||||||
use teloxide::dispatching::dialogue::InMemStorage;
|
use teloxide::dispatching::dialogue::InMemStorage;
|
||||||
use teloxide::dispatching::UpdateHandler;
|
use teloxide::dispatching::UpdateHandler;
|
||||||
|
use teloxide::types::InputFile;
|
||||||
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
||||||
|
|
||||||
|
use crate::dl::delete_if_exists;
|
||||||
|
use crate::dl::download;
|
||||||
|
|
||||||
type State = ();
|
type State = ();
|
||||||
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||||
|
|
||||||
|
|
@ -80,6 +84,22 @@ async fn cmd_test(bot: Bot, msg: Message) -> HandlerResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
async fn cmd_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
|
||||||
|
let output_path = match download(url.as_str()).await {
|
||||||
|
Ok(path) => path,
|
||||||
|
Err(e) => {
|
||||||
|
bot.send_message(msg.chat.id, e.to_string()).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = bot
|
||||||
|
.send_video(msg.chat.id, InputFile::file(&output_path))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
delete_if_exists(&output_path);
|
||||||
|
return Err(Box::new(e));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
21
src/dl.rs
21
src/dl.rs
|
|
@ -1,6 +1,9 @@
|
||||||
|
use std::fmt;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use teloxide::types::Message;
|
||||||
|
|
||||||
use self::spawn::SpawnError;
|
use self::spawn::SpawnError;
|
||||||
use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo};
|
use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo};
|
||||||
|
|
||||||
|
|
@ -26,6 +29,20 @@ impl From<YtDlpError> for DownloadError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for DownloadError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
use DownloadError as DE;
|
||||||
|
match &self {
|
||||||
|
DE::Message(msg) => write!(f, "{}", msg),
|
||||||
|
DE::NoFormatFound => write!(
|
||||||
|
f,
|
||||||
|
"no best format found. you may want to specify one yourself"
|
||||||
|
),
|
||||||
|
DE::MakePathError => write!(f, "failed to make path for download file"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn make_download_path(info: &YtDlpInfo, format: &YtDlpFormat) -> Result<String, DownloadError> {
|
fn make_download_path(info: &YtDlpInfo, format: &YtDlpFormat) -> Result<String, DownloadError> {
|
||||||
std::env::temp_dir()
|
std::env::temp_dir()
|
||||||
.join(format!("{}.{}", info.id, format.ext))
|
.join(format!("{}.{}", info.id, format.ext))
|
||||||
|
|
@ -37,7 +54,7 @@ fn make_download_path(info: &YtDlpInfo, format: &YtDlpFormat) -> Result<String,
|
||||||
fn file_exists(path: &str) -> bool {
|
fn file_exists(path: &str) -> bool {
|
||||||
match fs::metadata(path) {
|
match fs::metadata(path) {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(_) => false
|
Err(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,7 +70,7 @@ pub async fn download(url: &str) -> Result<String, DownloadError> {
|
||||||
Some(av) => av,
|
Some(av) => av,
|
||||||
None => return Err(DownloadError::NoFormatFound),
|
None => return Err(DownloadError::NoFormatFound),
|
||||||
};
|
};
|
||||||
|
|
||||||
let output_path = make_download_path(&info, &av)?;
|
let output_path = make_download_path(&info, &av)?;
|
||||||
if let Err(e) = YtDlp::download(url, &av.format_id, output_path.as_str()).await {
|
if let Err(e) = YtDlp::download(url, &av.format_id, output_path.as_str()).await {
|
||||||
delete_if_exists(&output_path);
|
delete_if_exists(&output_path);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue