aboutsummaryrefslogtreecommitdiffstats
path: root/crates/yt_dlp/src/hooks.rs
blob: df70ecde40bc4dc4c00ef46e2704d762bfb5e0ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

#[macro_export]
macro_rules! wrap_progress_hook {
    ($name:ident, $new_name:ident) => {
        yt_dlp::wrap_hook! {"progress_hook", $name, $new_name}
    };
}

#[macro_export]
macro_rules! wrap_post_processor_hook {
    ($name:ident, $new_name:ident) => {
        yt_dlp::wrap_hook! {"post_processor_hook", $name, $new_name}
    };
}

#[macro_export]
macro_rules! wrap_hook {
    ($ty:literal, $name:ident, $new_name:ident) => {
        pub(crate) fn $new_name(
            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(
                input: yt_dlp::hooks::__priv::pyo3::Bound<
                    '_,
                    yt_dlp::hooks::__priv::pyo3::types::PyDict,
                >,
            ) -> yt_dlp::hooks::__priv::pyo3::PyResult<()> {
                let processed_input = {
                    let new_dict = yt_dlp::hooks::__priv::filter_dict(input);
                    yt_dlp::hooks::__priv::json_dumps(&new_dict)
                };

                $name(processed_input)?;

                Ok(())
            }

            let module = yt_dlp::hooks::__priv::pyo3::types::PyModule::new(py, $ty)?;
            let fun = yt_dlp::hooks::__priv::pyo3::wrap_pyfunction!(inner, module)?;

            Ok(fun)
        }
    };
}

pub mod __priv {
    pub use crate::info_json::{json_dumps, json_loads, filter_dict};
    pub use pyo3;
}