From 4e59592c45b18bb02bc6310da86881b8fe83f10e Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 7 Nov 2026 15:14:57 +0100 Subject: refactor(yt/yt_dlp): Also take already constructed yt_dlps, preliminary support for match_filters --- crates/yt/src/yt_dlp/match_filters.rs | 60 +++++++++++++++++++++++++++++ crates/yt/src/yt_dlp/mod.rs | 71 +++++++++++++++++++++++------------ 2 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 crates/yt/src/yt_dlp/match_filters.rs (limited to 'crates') diff --git a/crates/yt/src/yt_dlp/match_filters.rs b/crates/yt/src/yt_dlp/match_filters.rs new file mode 100644 index 0000000..ae5675b --- /dev/null +++ b/crates/yt/src/yt_dlp/match_filters.rs @@ -0,0 +1,60 @@ +use yt_dlp::{ + hooks::__priv::pyo3::types::{PyNone, PyString}, json_get, wrap_match_filter, +}; + +enum MatchFilterOutput { + Ignore { msg: &'static str }, + Download, + InteractiveChoice, + Cancel { msg: String }, +} + +fn ignore_member_only( + arg: serde_json::Map, +) -> yt_dlp::hooks::__priv::pyo3::PyResult { + panic!("Called"); + + let ava = json_get!(arg, "availability", as_str); + if ava == "pubilc" { + Ok(MatchFilterOutput::Download) + } else { + Ok(MatchFilterOutput::Ignore { msg: "Not public"}) + } +} + +// TODO(@bpeetz): This should probably be a macro, instead of whatever _this_ is. <2026-09-20> +pub(crate) fn ignore_member_only_wrapped( + py: yt_dlp::hooks::__priv::pyo3::Python<'_>, +) -> yt_dlp::hooks::__priv::pyo3::PyResult< + yt_dlp::hooks::__priv::pyo3::Bound<'_, yt_dlp::hooks::__priv::pyo3::types::PyCFunction>, +> { + #[yt_dlp::hooks::__priv::pyo3::pyfunction] + #[pyo3(crate = "yt_dlp::hooks::__priv::pyo3")] + fn inner<'a>( + input: yt_dlp::hooks::__priv::pyo3::Bound<'_, yt_dlp::hooks::__priv::pyo3::types::PyDict>, + any: yt_dlp::hooks::__priv::pyo3::Bound<'_, yt_dlp::hooks::__priv::pyo3::types::PyAny>, + incomplete: yt_dlp::hooks::__priv::pyo3::Bound< + 'a, + yt_dlp::hooks::__priv::pyo3::types::PyBool, + >, + ) -> yt_dlp::hooks::__priv::pyo3::PyResult< + Option>, + > { + let processed_input = { + let new_dict = yt_dlp::hooks::__priv::filter_dict(input); + yt_dlp::hooks::__priv::json_dumps(&new_dict) + }; + + match ignore_member_only(processed_input)? { + MatchFilterOutput::Ignore { msg } => Ok(Some(PyString::new(incomplete.py(), &msg))), + MatchFilterOutput::Download => Ok(None), + MatchFilterOutput::InteractiveChoice => todo!(), + MatchFilterOutput::Cancel { msg } => todo!(), + } + } + + let module = yt_dlp::hooks::__priv::pyo3::types::PyModule::new(py, "match_filter")?; + let fun = yt_dlp::hooks::__priv::pyo3::wrap_pyfunction!(inner, module)?; + + Ok(fun) +} diff --git a/crates/yt/src/yt_dlp/mod.rs b/crates/yt/src/yt_dlp/mod.rs index eaa80a1..aaf68e2 100644 --- a/crates/yt/src/yt_dlp/mod.rs +++ b/crates/yt/src/yt_dlp/mod.rs @@ -13,8 +13,8 @@ use std::{borrow::ToOwned, str::FromStr, time::Duration}; use anyhow::{Context, Result}; use chrono::{DateTime, Utc}; use futures::{FutureExt, future::BoxFuture}; -use log::{error, warn}; -use serde_json::json; +use log::{error, info, warn}; +use serde_json::{Value, json}; use tokio::{fs, io}; use url::Url; use yt_dlp::{ @@ -30,37 +30,60 @@ use crate::{ subscription::Subscription, video::{Priority, TimeStamp, Video, VideoStatus}, }, + yt_dlp::match_filters::ignore_member_only_wrapped, }; -pub(crate) fn yt_dlp_opts_updating(max_backlog: usize) -> Result { - Ok(YoutubeDLOptions::new() +pub(crate) mod download_options; +pub(crate) mod hooks; +pub(crate) mod match_filters; + +pub(crate) fn yt_dlp_opts_updating( + app: &App, + max_backlog: Option, + playlist: bool, +) -> Result { + let base = YoutubeDLOptions::new() .set("playliststart", 1) - .set("playlistend", max_backlog) - .set("noplaylist", false) + .set("noplaylist", !playlist) .set( "extractor_args", json! {{"youtubetab": {"approximate_date": [""]}}}, ) - // // TODO: This also removes unlisted and other stuff. Find a good way to remove the - // // members-only videos from the feed. <2025-04-17> - // .set("match-filter", "availability=public") - .build()?) + .add_match_filter(ignore_member_only_wrapped); + + let base = if let Some(max_backlog) = max_backlog { + base.set("playlistend", max_backlog) + } else { + base + }; + + let base = if let Some(cookies) = &app.config.download.cookies { + base.set("cookiefile", cookies.to_str()) + } else if let Some(cookies_from_browser) = &app.config.download.cookies_from_browser { + base.set( + "cookiesfrombrowser", + Value::Array(vec![cookies_from_browser.to_owned().into()]), + ) + } else { + base + }; + + let base = { + let mut final_base = base; + + for (key, value) in &app.config.update.yt_dlp_options { + info!("Adding additional yt-dlp freeform option: {key} to {value}"); + final_base = final_base.set(key, value.to_owned()); + } + + final_base + }; + + Ok(base.build()?) } impl Video { - pub(crate) fn get_approx_size(&self) -> Result { - let yt_dlp = { - YoutubeDLOptions::new() - .set("prefer_free_formats", true) - .set("format", "bestvideo[height<=?1080]+bestaudio/best") - .set("fragment_retries", 10) - .set("retries", 10) - .set("getcomments", false) - .set("ignoreerrors", false) - .build() - .context("Failed to instanciate get approx size yt_dlp") - }?; - + pub(crate) fn get_approx_size(&self, yt_dlp: &YoutubeDL) -> Result { let result = yt_dlp .extract_info(&self.url, false, true) .with_context(|| format!("Failed to extract video information: '{}'", self.title))?; @@ -243,7 +266,7 @@ pub(crate) async fn get_current_cache_allocation(app: &App) -> Result { err => Err(io::Error::from(err)).with_context(|| { format!( "Failed to get dir size of download dir at: '{}'", - &app.config.paths.download_dir.display() + app.config.paths.download_dir.display() ) })?, }, -- cgit v1.3.1