diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 14:50:54 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-11-07 14:50:54 +0100 |
| commit | a0a8778eaa4f50e9c612a8d370912ccfd325c2ac (patch) | |
| tree | 067a40620829450d2909f189348641d31508684a | |
| parent | fix(yt_dlp/pp/dearrow): Actually make the API calls as intended (diff) | |
| download | yt-a0a8778eaa4f50e9c612a8d370912ccfd325c2ac.zip | |
refactor(yt_dlp/): Make the options more like the builder pattern
| -rw-r--r-- | crates/yt_dlp/src/lib.rs | 104 | ||||
| -rw-r--r-- | crates/yt_dlp/src/options.rs | 7 |
2 files changed, 103 insertions, 8 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index 22c9185..57ddddb 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -106,7 +106,6 @@ pub(crate) use py_kw_args; #[derive(Debug)] pub struct YoutubeDL { inner: Py<PyAny>, - options: serde_json::Map<String, serde_json::Value>, } impl YoutubeDL { @@ -132,6 +131,101 @@ impl YoutubeDL { }) } + /// Change one of the options. Run the provided closure and re-set the option again. + /// + /// This can be useful, for sharing one [`yt_dlp`] instance, between multiple calls to + /// download. E.g. for changing the subtitle lang option. + pub fn with_reset_option<T>( + &mut self, + key: impl Into<String>, + value: impl Into<serde_json::Value>, + fun: impl FnOnce(&mut Self) -> T, + ) -> T { + let key = key.into(); + let value = value.into(); + + debug!("yt-dlp: temporarily setting {key} to {value}"); + + let previous = self.set_option(key.clone(), value); + + let output = fun(self); + + if let Some(prev) = previous { + debug!("yt-dlp: reset setting {key} to {prev}"); + self.set_option(key, prev); + } else { + // TODO(@bpeetz): We would need to remove the option again here. <2026-08-18> + } + + output + } + + /// Change one of the options. Returns the previous value. + /// + /// This can be useful, for sharing one [`yt_dlp`] instance, between multiple calls to + /// download. E.g. for changing the subtitle lang option. + /// + /// # Panics + /// If internal [`yt_dlp`] API contracts fail + pub fn get_option(&self, key: impl Into<String>) -> Option<serde_json::Value> { + Python::attach(|py| { + let params = self + .inner + .bind(py) + .getattr(intern!(py, "params")) + .wrap_exc(py, "getting yt_dlp params") + .expect("YoutubeDL should always have params attr") + .cast_into::<PyDict>() + .expect("to be a dictionary"); + + let key = key.into(); + + let previous = params.get_item(key.clone()); + + match previous { + Ok(val) => Some(json_dumps_any(val, py)), + Err(_) => None, + } + }) + } + + /// Change one of the options. Returns the previous value. + /// + /// This can be useful, for sharing one [`yt_dlp`] instance, between multiple calls to + /// download. E.g. for changing the subtitle lang option. + /// + /// # Panics + /// If internal [`yt_dlp`] API contracts fail + pub fn set_option( + &mut self, + key: impl Into<String>, + value: impl Into<serde_json::Value>, + ) -> Option<serde_json::Value> { + Python::attach(|py| { + let params = self + .inner + .bind(py) + .getattr(intern!(py, "params")) + .wrap_exc(py, "getting yt_dlp params") + .expect("YoutubeDL should always have params attr") + .cast_into::<PyDict>() + .expect("to be a dictionary"); + + let key = key.into(); + let value = value.into(); + + let previous = params.get_item(key.clone()); + params + .set_item(key, json_loads_any(value, py)) + .expect("it should be possible to set this key"); + + match previous { + Ok(val) => Some(json_dumps_any(val, py)), + Err(_) => None, + } + }) + } + /// Download a given list of URLs. /// Returns the paths they were downloaded to. /// @@ -211,7 +305,9 @@ impl YoutubeDL { // already resolved. Do nothing } else if let Ok(generator) = generator.cast::<PyIterator>() { // A python generator object. - let max_backlog = json_try_get!(self.options, "playlistend", as_u64) + let max_backlog = self + .get_option("playlistend") + .map(|o| json_cast!(o, as_u64)) .map_or(10000, |playlistend| { usize::try_from(playlistend).expect("Should work") }); @@ -230,7 +326,9 @@ impl YoutubeDL { .wrap_exc(py, "setting playlist 'entries'")?; } else { // Probably some sort of paged list (`OnDemand` or otherwise) - let max_backlog = json_try_get!(self.options, "playlistend", as_u64) + let max_backlog = self + .get_option("playlistend") + .map(|o| json_cast!(o, as_u64)) .map_or(10000, |playlistend| { usize::try_from(playlistend).expect("Should work") }); diff --git a/crates/yt_dlp/src/options.rs b/crates/yt_dlp/src/options.rs index e3373b2..602ac69 100644 --- a/crates/yt_dlp/src/options.rs +++ b/crates/yt_dlp/src/options.rs @@ -47,7 +47,7 @@ impl YoutubeDLOptions { post_processors: vec![], }; - me.with_post_processor(post_processors::dearrow::process) + me.add_post_processor(post_processors::dearrow::process) } #[must_use] @@ -82,7 +82,7 @@ impl YoutubeDLOptions { } #[must_use] - pub fn with_post_processor(mut self, pp: PostProcessorFunction) -> Self { + pub fn add_post_processor(mut self, pp: PostProcessorFunction) -> Self { self.post_processors.push(pp); self } @@ -119,8 +119,6 @@ impl YoutubeDL { pub fn from_options(options: YoutubeDLOptions) -> Result<Self, build::Error> { Python::initialize(); - let output_options = options.options.clone(); - let yt_dlp_module = Python::attach(|py| { let opts = json_loads(options.options, py); @@ -226,7 +224,6 @@ signal.signal(signal.SIGINT, signal.SIG_DFL) Ok(Self { inner: yt_dlp_module, - options: output_options, }) } } |
