From fb5613b613935890278bf083fb10266f9525dad5 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 7 Nov 2026 15:26:06 +0100 Subject: fix(yt_dlp/options): Support (one) match_filter function --- crates/yt_dlp/src/options.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/yt_dlp/src/options.rs b/crates/yt_dlp/src/options.rs index 602ac69..cdbdb45 100644 --- a/crates/yt_dlp/src/options.rs +++ b/crates/yt_dlp/src/options.rs @@ -12,7 +12,7 @@ use std::sync; use pyo3::{ Bound, IntoPyObjectExt, PyAny, PyResult, Python, intern, - types::{PyAnyMethods, PyCFunction, PyDict, PyTuple}, + types::{PyAnyMethods, PyCFunction, PyDict, PyList, PyTuple}, }; use pyo3_pylogger::setup_logging; @@ -23,6 +23,7 @@ use crate::{ pub type HookFunction = fn(py: Python<'_>) -> PyResult>; pub type PostProcessorFunction = fn(py: Python<'_>) -> PyResult>; +pub type MatchFilterFunction = fn(py: Python<'_>) -> PyResult>; /// Options, that are used to customize the download behaviour. /// @@ -35,6 +36,7 @@ pub struct YoutubeDLOptions { progress_hook: Option, post_processor_hook: Option, post_processors: Vec, + match_filters: Vec, } impl YoutubeDLOptions { @@ -45,6 +47,7 @@ impl YoutubeDLOptions { progress_hook: None, post_processor_hook: None, post_processors: vec![], + match_filters: vec![], }; me.add_post_processor(post_processors::dearrow::process) @@ -87,6 +90,22 @@ impl YoutubeDLOptions { self } + /// From the `YoutubeDL` docs: + /// A function that gets called for every video with the signature + /// (`info_dict`, *, incomplete: bool) -> Optional[str] + /// - If it returns a message, the video is ignored. + /// - If it returns None, the video is downloaded. + /// - If it returns `utils.NO_DEFAULT`, the user is interactively + /// asked whether to download the video. + /// - Raise utils.DownloadCancelled(msg) to abort remaining + /// downloads when a video is rejected. + /// `match_filter_func` in utils/_utils.py is one example for this. + #[must_use] + pub fn add_match_filter(mut self, mf: MatchFilterFunction) -> Self { + self.match_filters.push(mf); + self + } + /// # Errors /// If the underlying [`YoutubeDL::from_options`] errors. pub fn build(self) -> Result { @@ -140,6 +159,20 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) }); } + { + // TODO: Make this work with more than one match filter. <2026-11-07> + assert_eq!(options.match_filters.len(), 1); + + opts.set_item( + intern!(py, "match_filter"), + pp(py) + .wrap_exc(py, "convertion match filter function to python")? + .into_bound_py_any(py) + .wrap_exc(py, "converting match filter function to any type")?, + ) + .wrap_exc(py, "setting match_filter yt_dlp option")?; + } + { // Setup the progress hook if let Some(ph) = options.progress_hook { -- cgit v1.3.1