diff options
Diffstat (limited to 'crates/yt_dlp/src')
| -rw-r--r-- | crates/yt_dlp/src/options.rs | 35 |
1 files changed, 34 insertions, 1 deletions
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<Bound<'_, PyCFunction>>; pub type PostProcessorFunction = fn(py: Python<'_>) -> PyResult<Bound<'_, PyAny>>; +pub type MatchFilterFunction = fn(py: Python<'_>) -> PyResult<Bound<'_, PyCFunction>>; /// Options, that are used to customize the download behaviour. /// @@ -35,6 +36,7 @@ pub struct YoutubeDLOptions { progress_hook: Option<HookFunction>, post_processor_hook: Option<HookFunction>, post_processors: Vec<PostProcessorFunction>, + match_filters: Vec<MatchFilterFunction>, } 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<YoutubeDL, build::Error> { @@ -141,6 +160,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 { opts.set_item( |
