aboutsummaryrefslogtreecommitdiffstats
path: root/crates/yt_dlp/src/info_json.rs
blob: df492181d37b959512c74499a19f4227e76b1f12 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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>.

use pyo3::{
    Bound, Python, intern,
    types::{PyAnyMethods, PyDict},
};

pub type InfoJson = serde_json::Map<String, serde_json::Value>;

/// # Panics
/// If expectation about python operations fail.
#[must_use]
pub fn json_loads(
    input: serde_json::Map<String, serde_json::Value>,
    py: Python<'_>,
) -> Bound<'_, PyDict> {
    let json = py.import(intern!(py, "json")).expect("Module exists");
    let loads = json.getattr(intern!(py, "loads")).expect("Method exists");
    let self_str = serde_json::to_string(&serde_json::Value::Object(input)).expect("Vaild json");
    let dict = loads
        .call((self_str,), None)
        .expect("Vaild json is always a valid dict");

    dict.cast_into().expect("Should always be a dict")
}

/// # Panics
/// If expectation about python operations fail.
#[must_use]
pub fn json_dumps(input: &Bound<'_, PyDict>) -> serde_json::Map<String, serde_json::Value> {
    let py = input.py();

    let json = py.import(intern!(py, "json")).expect("Module exists");
    let dumps = json.getattr(intern!(py, "dumps")).expect("Method exists");
    let dict = dumps
        .call((input,), None)
        .map_err(|err| err.print(py))
        .expect("Might not always work, but for our dicts it works");

    let string: String = dict.extract().expect("Should always be a string");

    let value: serde_json::Value = serde_json::from_str(&string).expect("Should be valid json");

    match value {
        serde_json::Value::Object(map) => map,
        _ => unreachable!("These should not be json.dumps output"),
    }
}

/// # Panics
/// If expectation about python operations fail.
#[must_use]
pub fn filter_dict(input: Bound<'_, PyDict>) -> Bound<'_, PyDict> {
    let new_dict = PyDict::new(input.py());

    input
        .into_iter()
        .filter_map(|(name, value)| {
            let real_name = name.extract::<String>().expect("Should always be a string");

            if real_name.starts_with('_') {
                None
            } else {
                let value = if value.is_instance_of::<PyDict>() {
                    filter_dict(
                        value
                            .cast_into::<PyDict>()
                            .expect("to be a dict, because we checked"),
                    )
                    .into_any()
                } else {
                    value
                };
                Some((real_name, value))
            }
        })
        .for_each(|(key, value)| {
            new_dict
                .set_item(&key, value)
                .expect("This is a transposition, should always be valid");
        });

    new_dict
}