mirror of
https://github.com/thegeneralist01/social-media-video-downloader-bot
synced 2026-01-09 14:50:30 +01:00
initial commit
This commit is contained in:
parent
739ddf8cbe
commit
71ad4861bc
6 changed files with 82 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
/target
|
||||
.env
|
||||
downloads/*
|
||||
|
|
|
|||
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -41,6 +41,12 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.97"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
|
||||
|
||||
[[package]]
|
||||
name = "aquamarine"
|
||||
version = "0.6.0"
|
||||
|
|
@ -1367,6 +1373,7 @@ checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd"
|
|||
name = "social-media-video-downloader-bot"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"dotenv",
|
||||
"log",
|
||||
"openssl",
|
||||
|
|
|
|||
|
|
@ -10,3 +10,4 @@ log = "0.4"
|
|||
pretty_env_logger = "0.5"
|
||||
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
|
||||
openssl = { version = "0.10.35", features = ["vendored"] }
|
||||
anyhow = "1.0.97"
|
||||
|
|
|
|||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod youtube;
|
||||
56
src/main.rs
56
src/main.rs
|
|
@ -1,7 +1,26 @@
|
|||
use teloxide::prelude::*;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use social_media_video_downloader_bot::youtube::download;
|
||||
use teloxide::{prelude::*, types::InputFile};
|
||||
|
||||
const URL_WHITELIST: [&str; 10] = [
|
||||
// HTTPS
|
||||
"https://www.youtube.com/",
|
||||
"https://youtu.be/",
|
||||
"https://www.youtube.com/shorts/",
|
||||
"https://youtube.com/shorts/",
|
||||
"https://x.com/",
|
||||
// HTTP
|
||||
"http://www.youtube.com/",
|
||||
"http://youtu.be/",
|
||||
"http://www.youtube.com/shorts/",
|
||||
"http://youtube.com/shorts/",
|
||||
"http://x.com/",
|
||||
];
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
pretty_env_logger::init();
|
||||
log::info!("Starting the bot");
|
||||
|
|
@ -9,7 +28,36 @@ async fn main() {
|
|||
let bot = Bot::from_env();
|
||||
|
||||
teloxide::repl(bot, |bot: Bot, msg: Message| async move {
|
||||
bot.send_message(msg.chat.id, "hi").await.ok();
|
||||
if let Some(text) = msg.text() {
|
||||
if text.to_lowercase() == "/start" {
|
||||
bot.send_message(msg.chat.id, "Send a social media video link to download it!\nCurrently, the following social medias are supported: YouTube, X (twitter).").await.ok();
|
||||
} else if URL_WHITELIST.iter().any(|url| text.starts_with(url)) {
|
||||
log::info!("Video downloaded");
|
||||
let prepping_msg = bot.send_message(msg.chat.id, "Prepping the video...").await;
|
||||
let user_id = msg.from.as_ref().map(|user| user.id).unwrap();
|
||||
let sent = download(text, user_id).await.unwrap();
|
||||
|
||||
match sent {
|
||||
true => {
|
||||
let filename = format!("./downloads/{}.mp4", user_id);
|
||||
let path = Path::new(&filename);
|
||||
bot.send_video(msg.chat.id, InputFile::file(path)).await.ok();
|
||||
if path.exists() {
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
},
|
||||
false => {
|
||||
bot.send_message(msg.chat.id, "Failed to download the video.\nPlease check if the link is valid.").await.ok();
|
||||
},
|
||||
};
|
||||
if let Ok(prepping_msg) = prepping_msg {
|
||||
bot.delete_message(msg.chat.id, prepping_msg.id).await.ok();
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}).await;
|
||||
})
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
20
src/youtube.rs
Normal file
20
src/youtube.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use std::process::Command;
|
||||
|
||||
use anyhow::Result;
|
||||
use teloxide::types::UserId;
|
||||
|
||||
pub async fn download(url: &str, user_id: UserId) -> Result<bool> {
|
||||
// The command I use:
|
||||
// yt-dlp "https://www.youtube.com/watch?v=$1" -o "$2" --merge-output-format mp4
|
||||
let cmd = Command::new("yt-dlp")
|
||||
.arg(url)
|
||||
.arg("-o")
|
||||
// .arg("%(title).90s.%(ext)s")
|
||||
.arg(format!("./downloads/{}.%(ext)s", user_id))
|
||||
.arg("--merge-output-format")
|
||||
.arg("mp4")
|
||||
.output()
|
||||
.expect("Failed to execute command");
|
||||
log::debug!("yt-dlp output: {:?}", cmd);
|
||||
Ok(cmd.status.success())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue