aboutsummaryrefslogtreecommitdiffstats
path: root/crates/yt_dlp/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/yt_dlp/src/lib.rs')
-rw-r--r--crates/yt_dlp/src/lib.rs104
1 files changed, 101 insertions, 3 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")
});