add webhook support

This commit is contained in:
JMARyA 2024-03-17 00:42:36 +01:00
parent fc0d7c0307
commit ebff54a8ba
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 852 additions and 14 deletions

821
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -13,4 +13,5 @@ rusqlite = "0.30.0"
serde = { version = "1.0.196", features = ["derive"] } serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113" serde_json = "1.0.113"
toml = "0.8.10" toml = "0.8.10"
jobdispatcher = { git = "https://git.hydrar.de/jmarya/jobdispatcher" } jobdispatcher = { git = "https://git.hydrar.de/jmarya/jobdispatcher" }
reqwest = { version = "0.11.26", features = ["blocking", "json"] }

View file

@ -42,6 +42,8 @@ pub struct SoundCloudConfig {
pub format: Option<String>, pub format: Option<String>,
// Cookie File // Cookie File
pub cookie: Option<String>, pub cookie: Option<String>,
// Webhooks for notifications
pub webhooks: Option<Vec<String>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -75,6 +77,7 @@ impl SoundCloudModule {
format: config.format, format: config.format,
cookie: config.cookie, cookie: config.cookie,
audio_only: Some(true), audio_only: Some(true),
webhooks: config.webhooks,
}, },
db, db,
root_dir, root_dir,

View file

@ -46,6 +46,8 @@ pub struct YouTubeConfig {
pub format: Option<String>, pub format: Option<String>,
// Cookie File // Cookie File
pub cookie: Option<String>, pub cookie: Option<String>,
// Webhooks for notifications
pub webhooks: Option<Vec<String>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -79,6 +81,7 @@ impl YouTubeModule {
format: config.format, format: config.format,
cookie: config.cookie, cookie: config.cookie,
audio_only: Some(false), audio_only: Some(false),
webhooks: config.webhooks,
}, },
db, db,
root_dir, root_dir,

View file

@ -54,6 +54,8 @@ pub struct YtDlpConfig {
pub format: Option<String>, pub format: Option<String>,
// Cookie File // Cookie File
pub cookie: Option<String>, pub cookie: Option<String>,
// Webhooks for notifications
pub webhooks: Option<Vec<String>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -101,11 +103,13 @@ impl Module for YtDlpModule {
self.db.insert_url(&video_url); self.db.insert_url(&video_url);
self.db.update_new_downloads(&self.name(), item, item_url); self.db.update_new_downloads(&self.name(), item, item_url);
log::info!("Downloaded \"{video_title}\""); log::info!("Downloaded \"{video_title}\"");
self.webhook_notify(&video_url, &video_title, item, true);
} }
Err(e) => { Err(e) => {
log::error!( log::error!(
"Error downloading \"{video_title}\"; Reason: {e}" "Error downloading \"{video_title}\"; Reason: {e}"
); );
self.webhook_notify(&video_url, &video_title, item, false);
} }
} }
} }
@ -127,6 +131,38 @@ impl Module for YtDlpModule {
} }
impl YtDlpModule { impl YtDlpModule {
pub fn webhook_notify(&self, video_url: &str, video_title: &str, item: &str, success: bool) {
let request = serde_json::json!({
"module": self.name(),
"url": video_url,
"title": video_title,
"item": item,
"success": success
});
let client = reqwest::blocking::Client::new();
if let Some(webhooks) = &self.config.webhooks {
for url in webhooks {
client
.post(url)
.json(&request)
.send()
.expect("Failed to send webhook request");
}
}
}
/// A function to get the latest entries (title and URL) for a given channel with a specified limit.
///
/// # Arguments
///
/// * `channel` - The name of the `YouTube` channel.
/// * `limit` - The maximum number of entries to return.
///
/// # Returns
///
/// A `Result` containing a vector of tuples if successful, where each tuple contains the title and URL of an entry.
/// An error message if execution of `yt-dlp` fails.
fn get_latest_entries(channel: &str, limit: u64) -> Result<Vec<(String, String)>, String> { fn get_latest_entries(channel: &str, limit: u64) -> Result<Vec<(String, String)>, String> {
let output = Command::new("yt-dlp") let output = Command::new("yt-dlp")
.arg("--no-warnings") .arg("--no-warnings")