This commit is contained in:
JMARyA 2024-03-09 00:09:38 +01:00
parent fbe43dd800
commit 2e67cce1cc
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use crate::{ensure_dir_exists, Module}; use crate::{ensure_dir_exists, Module};
/// Configuration for the YouTube Module /// Configuration for the `YouTube` Module
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YouTubeConfig { pub struct YouTubeConfig {
// Interval in minutes between checks // Interval in minutes between checks
@ -56,53 +56,54 @@ impl Module for YouTubeModule {
} }
fn run(&self) { fn run(&self) {
log::info!("Running YouTube Module"); loop {
let download_options = self.config.download_options(); log::info!("Running YouTube Module");
log::info!("Checking {} channels", self.config.channels.len()); let download_options = self.config.download_options();
for (channel, channel_url) in &self.config.channels { log::info!("Checking {} channels", self.config.channels.len());
log::info!("Fetching \"{channel}\" videos"); for (channel, channel_url) in &self.config.channels {
match Self::get_latest_channel_videos( log::info!("Fetching \"{channel}\" videos");
channel_url, match Self::get_latest_channel_videos(channel_url, self.config.limit.unwrap_or(10))
&self.config.limit.unwrap_or(10).to_string(), {
) { Ok(latest_videos) => {
Ok(latest_videos) => { for (video_title, video_url) in latest_videos {
for (video_title, video_url) in latest_videos { if self.db.check_for_url(&video_url).unwrap() {
if self.db.check_for_url(&video_url).unwrap() { log::trace!(
log::trace!( "Skipping \"{video_title}\" because it was already downloaded"
"Skipping \"{video_title}\" because it was already downloaded" );
); } else {
} else { match Self::download_video(
match Self::download_video( &video_url,
&video_url, &self.root_dir.join(channel),
&self.root_dir.join(channel), &download_options,
&download_options, ) {
) { Ok(()) => {
Ok(()) => { // mark as downloaded
// mark as downloaded self.db.insert_url(&video_url).unwrap();
self.db.insert_url(&video_url).unwrap(); log::info!("Downloaded \"{video_title}\"");
log::info!("Downloaded \"{video_title}\""); }
} Err(e) => {
Err(e) => { log::error!(
log::error!("Error downloading \"{video_title}\"; Reason: {e}"); "Error downloading \"{video_title}\"; Reason: {e}"
// todo : error handling );
}
} }
} }
} }
} }
} Err(e) => {
Err(e) => { log::error!("Could not get videos from \"{channel}\". Reason: {e}");
log::error!("Could not get videos from \"{channel}\". Reason: {e}"); }
} }
} }
std::thread::sleep(std::time::Duration::from_secs(self.config.interval * 60));
} }
std::thread::sleep(std::time::Duration::from_secs(self.config.interval * 60));
} }
} }
impl YouTubeModule { impl YouTubeModule {
fn get_latest_channel_videos( fn get_latest_channel_videos(
channel: &str, channel: &str,
limit: &str, limit: u64,
) -> Result<Vec<(String, String)>, String> { ) -> Result<Vec<(String, String)>, String> {
let output = Command::new("yt-dlp") let output = Command::new("yt-dlp")
.arg("--no-warnings") .arg("--no-warnings")
@ -111,7 +112,7 @@ impl YouTubeModule {
.arg("--print") .arg("--print")
.arg("title,webpage_url") .arg("title,webpage_url")
.arg("--playlist-end") .arg("--playlist-end")
.arg(limit) .arg(limit.to_string())
.arg(channel) .arg(channel)
.output() .output()
.expect("Failed to execute yt-dlp"); .expect("Failed to execute yt-dlp");
@ -129,7 +130,7 @@ impl YouTubeModule {
} }
} }
Ok(videos) Ok(videos.into_iter().take(limit as usize).collect())
} }
fn download_video(video_url: &str, cwd: &PathBuf, opt: &DownloadOptions) -> Result<(), String> { fn download_video(video_url: &str, cwd: &PathBuf, opt: &DownloadOptions) -> Result<(), String> {