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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#![warn(missing_docs)]
//! bilibili-api-rs is a rust library project got inspiration from [bilibili-api](https://github.com/Passkou/bilibili-api).
//!
//! Currently "GET" apis only. Api interface `User`, `Xlive` derive from
//! [Client][crate::wbi::Client].
//!
//! Api result is part of response, alike [bilibili-api](https://github.com/Passkou/bilibili-api),
//! is response["data"]. Invalid response treated as error then bail. *Note that init retries and token
//! refresh also be treated as error.*
//!
//! *High overhead*: to anti-detect, client open one whole new connection in every request.
//!
//! ## Example
//! ```
//! use bilibili_api_rs::Client;
//! use anyhow::Result;
//! async fn test_xlive() -> Result<()> {
//!     let mut cli = Client::new();
//!     let area_virtual = 9;
//!     let type_all = 0;
//!     cli.xlive(area_virtual, type_all).list(1).await.ok(); // usually retry once for init
//!     let lives = cli.xlive(area_virtual, type_all).list(2).await?;
//!     Ok(())
//! }
//! ```
use log::debug;
use std::sync::Arc;
use tokio::sync::mpsc;

mod cred_utils;

pub mod wbi;
pub use wbi::Client;

type StateData = im::HashMap<String, String>;
type Json = serde_json::Value;

#[derive(Clone, Debug)]
struct Bench {
    data: Arc<Json>,
    state: StateData,
    tx: mpsc::Sender<StateData>,
}

impl Bench {
    pub fn new() -> (Self, mpsc::Receiver<StateData>) {
        let unstable: Json = serde_json::from_str(include_str!("api_info/unstable.json"))
            .expect("api_info/unstable.json invalid");
        let user: Json = serde_json::from_str(include_str!("api_info/user.json"))
            .expect("api_info/user.json invalid");
        let live: Json = serde_json::from_str(include_str!("api_info/live.json"))
            .expect("api_info/live.json invalid");
        let video: Json = serde_json::from_str(include_str!("api_info/video.json"))
            .expect("api_info/video.json invalid");
        let api_xlive: Json = serde_json::from_str(include_str!("api_info/xlive.json"))
            .expect("api_info/xlive.json invalid");
        let credential: Json = serde_json::from_str(include_str!("api_info/credential.json"))
            .expect("api_info/credential.json invalid");
        let wbi_oe: Json =
            serde_json::from_str(include_str!("wbi_oe.json")).expect("wbi_oe.json invalid"); // this file has to be
                                                                                             // manually maintain now,
                                                                                             // perl tool TODO
        let mut state = StateData::new();
        state.insert("SESSDATA".into(), "None".into());
        state.insert("bili_jct".into(), "None".into());
        state.insert("ac_time_value".into(), "None".into());
        let (tx, rx) = mpsc::channel(1);
        (
            Self {
                data: Arc::new(serde_json::json!({
                    "api": {
                        "user": user,
                        "live": live,
                        "video": video,
                        "xlive": api_xlive,
                        "credential": credential,
                        "unstable": unstable,
                    },
                    "cookie_state": [
                        "buvid3",
                        "buvid4",
                        "buvid_fp",
                        "_uuid",
                        "SESSDATA",
                        "ac_time_value",
                        "bili_jct",
                        "DedeUserID",
                    ],
                    "wbi_oe": wbi_oe,
                    "headers": {
                        "REFERER":  "https://www.bilibili.com",
                        "USER_AGENT": concat!(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ",
                            "AppleWebKit/537.36 (KHTML, like Gecko) ",
                            "Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54"
                        ),
                    }
                })),
                state,
                tx,
            },
            rx,
        )
    }

    #[allow(dead_code)]
    pub fn commit_state(&self, change: impl FnOnce(&mut StateData)) {
        let mut s = self.state.clone();
        change(&mut s);
        self.update_state(s);
    }

    pub fn update_state(&self, state: StateData) {
        if let Err(e) = self.tx.try_send(state) {
            debug!("bench try_send {e:?}");
        }
    }
}

/// Lodash-like get helper, implemented for `serde_json`
///
/// ```
/// use bilibili_api_rs::Lodash;
/// # use serde_json::json;
/// let v = json!({
///     "following": [ {
///         "mid": 1472906636,
///         "name": "ywwuyi",
///     }, {
///         "mid": 15810,
///         "name": "Mr.Quin",
///     }],
/// });
/// assert_eq!(v.at(json!(["following", 0, "mid"])), json!(1472906636));
/// assert_eq!(v["following"].at(json!([
///         [0, "name"],
///         [1, "name"],
///     ])),
///     json!(["ywwuyi", "Mr.Quin"]));
/// ```
pub trait Lodash {
    /// Input a matrix, output a vector; input a vector, output single one value
    #[must_use]
    fn at(&self, paths: Json) -> Self;
}

fn lodash_step<'a>(v: &'a Json, p: &Json) -> &'a Json {
    match p {
        Json::Number(n) => &v[n.as_u64().map(usize::try_from).unwrap().unwrap()],
        Json::String(s) => &v[s],
        _ => &Json::Null,
    }
}

fn lodash_get<'a>(v: &'a Json, path: &Json) -> &'a Json {
    let Json::Array(path) = path else {
        return &Json::Null;
    };
    if path.is_empty() {
        return v;
    }
    let mut it = path.iter();
    let mut v = lodash_step(v, it.next().unwrap());
    for p in it {
        v = lodash_step(v, p);
    }
    v
}

impl Lodash for Json {
    fn at(&self, paths: Json) -> Self {
        let Some(a) = paths.as_array() else {
            return Self::Null;
        };
        if a[0].is_array() {
            let mut v: Vec<Self> = Vec::new();
            for path in a {
                v.push(lodash_get(self, path).clone());
            }
            Self::Array(v)
        } else {
            lodash_get(self, &paths).clone()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::thread;

    #[test]
    fn test_lodash_at() {
        let bench = Bench::new().0;
        let v = &bench.data;
        assert_eq!(
            v.at(json!([
                ["headers", "REFERER"],
                ["headers", "__must_null__"],
            ])),
            json!(["https://www.bilibili.com", ()])
        );
        assert_eq!(
            v.at(json!(["headers", "REFERER"])),
            json!("https://www.bilibili.com")
        );
    }

    #[test]
    fn validate_wbi_user_info() {
        let bench = Bench::new().0;
        assert_eq!(
            bench.data["api"]["user"]["info"]["info"]["wbi"].as_bool(),
            Some(true)
        );
    }

    #[test]
    fn validate_method_xlive_get_list() {
        let bench = Bench::new().0;
        assert_eq!(
            bench.data["api"]["xlive"]["info"]["get_list"]["method"].as_str(),
            Some("GET")
        );
    }

    fn json_state(rx: &mut mpsc::Receiver<StateData>) -> Json {
        rx.try_recv()
            .ok()
            .map(serde_json::to_value)
            .and_then(Result::ok)
            .unwrap_or(Json::Null)
    }

    #[test]
    fn commit_state() {
        let (bench, mut rx) = Bench::new();
        bench.commit_state(|s| {
            s.clear();
            s.insert("test".into(), "value".into());
        });
        assert_eq!(json_state(&mut rx), json!({"test":"value"}));
        bench.commit_state(|s| {
            s.clear();
            s.insert("test".into(), "modified".into());
        });
        assert_eq!(json_state(&mut rx), json!({"test":"modified"}));
    }

    #[test]
    fn multithread_commit_state() {
        let (bench0, mut rx) = Bench::new();

        let bench = bench0.clone();
        let hdl = thread::spawn(move || {
            bench.commit_state(|s| {
                s.clear();
                s.insert("test".into(), "value".into());
            });
        });
        assert!(hdl.join().is_ok());
        assert_eq!(json_state(&mut rx), json!({"test":"value"}));

        let bench = bench0;
        let hdl = thread::spawn(move || {
            bench.commit_state(|s| {
                s.clear();
                s.insert("test".into(), "modified".into());
            });
        });
        assert!(hdl.join().is_ok());
        assert_eq!(json_state(&mut rx), json!({"test":"modified"}));
    }

    #[test]
    fn insure_get_nav_api_no_encwbi() {
        let bench = Bench::new().0;
        assert!(matches!(
            bench.data["api"]["credential"]["valid"]["wbi"],
            Json::Null | Json::Bool(false)
        ));
    }
}