From fcb6f32df2f7080d58bca61786296c5fb64fc085 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 26 May 2026 18:24:35 +0200 Subject: feat(yt/{config,download}): Allow specifying a cookie file --- crates/yt/src/cli.rs | 5 +++++ .../download/implm/download/download_options.rs | 19 ++++++++++++------- crates/yt/src/commands/download/implm/download/mod.rs | 4 ++-- crates/yt/src/commands/mod.rs | 2 +- crates/yt/src/config/mod.rs | 10 ++++++++++ crates/yt/src/main.rs | 4 +++- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/crates/yt/src/cli.rs b/crates/yt/src/cli.rs index 9a24403..dbdcdd1 100644 --- a/crates/yt/src/cli.rs +++ b/crates/yt/src/cli.rs @@ -41,6 +41,11 @@ pub(crate) struct CliArgs { #[arg(long, short)] pub(crate) db_path: Option, + /// Set the cookie file to be passed to `yt_dlp`. This overrides the default and the value from + /// the config file. + #[arg(long, short = 'P')] + pub(crate) cookies: Option, + /// Set the path to the config.toml. /// This overrides the default. #[arg(long, short)] diff --git a/crates/yt/src/commands/download/implm/download/download_options.rs b/crates/yt/src/commands/download/implm/download/download_options.rs index c9e5272..4174266 100644 --- a/crates/yt/src/commands/download/implm/download/download_options.rs +++ b/crates/yt/src/commands/download/implm/download/download_options.rs @@ -19,9 +19,9 @@ use super::hooks::{wrapped_progress_hook, wrapped_post_processor_hook}; pub(crate) fn download_opts( app: &App, - subtitle_langs: Option<&String>, + subtitle_langs: Option<&str>, ) -> anyhow::Result { - YoutubeDLOptions::new() + let base = YoutubeDLOptions::new() .with_progress_hook(wrapped_progress_hook) .with_post_processor_hook(wrapped_post_processor_hook) .set("extract_flat", "in_playlist") @@ -36,7 +36,6 @@ pub(crate) fn download_opts( } }, ) - //.set("cookiesfrombrowser", json! {("firefox", "me.google", None::, "youtube_dlp")}) .set("prefer_free_formats", true) .set("ffmpeg_location", env!("FFMPEG_LOCATION")) .set("format", "bestvideo[height<=?1080]+bestaudio/best") @@ -111,12 +110,18 @@ pub(crate) fn download_opts( "subtitleslangs", Value::Array( subtitle_langs - .map_or("", String::as_str) + .unwrap_or("") .split(',') .map(|val| Value::String(val.to_owned())) .collect::>(), ), - ) - .build() - .context("Failed to instanciate download yt_dlp") + ); + + if let Some(cookies) = &app.config.global.cookies { + base.set("cookies", cookies.to_str()) + } else { + base + } + .build() + .context("Failed to instanciate download yt_dlp") } diff --git a/crates/yt/src/commands/download/implm/download/mod.rs b/crates/yt/src/commands/download/implm/download/mod.rs index f761c70..39f7960 100644 --- a/crates/yt/src/commands/download/implm/download/mod.rs +++ b/crates/yt/src/commands/download/implm/download/mod.rs @@ -44,13 +44,13 @@ impl CurrentDownload { let extractor_hash = video.extractor_hash; debug!("Download started: {}", &video.title); - let yt_dlp = Arc::new(download_opts(app, video.subtitle_langs.as_ref())?); + let yt_dlp = Arc::new(download_opts(app, video.subtitle_langs.as_deref())?); let local_yt_dlp = Arc::clone(&yt_dlp); let task_handle = tokio::task::spawn_blocking(move || { let mut result = local_yt_dlp - .download(&[video.url.clone()]) + .download(std::slice::from_ref(&video.url)) .with_context(|| format!("Failed to download video: '{}'", video.title))?; assert_eq!(result.len(), 1); diff --git a/crates/yt/src/commands/mod.rs b/crates/yt/src/commands/mod.rs index 431acef..493ddc6 100644 --- a/crates/yt/src/commands/mod.rs +++ b/crates/yt/src/commands/mod.rs @@ -134,7 +134,7 @@ fn complete_subscription(current: &OsStr) -> Vec { return output; }; - let Ok(config) = Config::from_config_file(None, None, None) else { + let Ok(config) = Config::from_config_file(None, None, None, None) else { return output; }; diff --git a/crates/yt/src/config/mod.rs b/crates/yt/src/config/mod.rs index 05bb4cf..24866eb 100644 --- a/crates/yt/src/config/mod.rs +++ b/crates/yt/src/config/mod.rs @@ -57,6 +57,16 @@ mk_config! { ) ) } => set_static_should_display_color, + + /// The cookie file to pass to `yt_dlp`. + cookies: Option where cookies_path: Option =! { + |config_value: Option>| + if let Some(co) = cookies_path { + Ok::<_, anyhow::Error>(Some(co)) + } else { + Ok(config_value.unwrap_or(None)) + } + }, }, select: SelectConfig = { /// The playback speed to use, when it is not overridden. diff --git a/crates/yt/src/main.rs b/crates/yt/src/main.rs index 705e642..50e4a09 100644 --- a/crates/yt/src/main.rs +++ b/crates/yt/src/main.rs @@ -66,7 +66,9 @@ async fn main() -> Result<()> { } }); - let config = Config::from_config_file(args.config_path, args.color, args.db_path)?; + let config = + Config::from_config_file(args.config_path, args.color, args.cookies, args.db_path)?; + if args.version { version::show(&config).await?; return Ok(()); -- cgit v1.3.1