Skip to main content

bpi_rs/user/
batch.rs

1//! B站用户批量信息相关接口
2//!
3//! [查看 API 文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/user)
4use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
5use serde::{ Deserialize, Serialize };
6
7/// UID 查询返回的单个条目
8#[derive(Debug, Clone, Deserialize, Serialize)]
9pub struct NameUidItem {
10    /// 用户名
11    pub name: String,
12    /// 用户 mid
13    pub uid: String,
14}
15
16/// 批量用户名查 UID 的数据本体
17#[derive(Debug, Clone, Deserialize, Serialize)]
18pub struct NameToUidData {
19    pub uid_list: Vec<NameUidItem>,
20}
21
22impl BpiClient {
23    /// 批量查询用户名对应的 UID
24    ///
25    /// # 文档
26    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/user)
27    ///
28    /// # 参数
29    /// - `names`: 用户名列表,多个用户名以逗号分隔
30    pub async fn user_name_to_uid(
31        &self,
32        names: &[&str]
33    ) -> Result<BpiResponse<NameToUidData>, BpiError> {
34        let names_str = names.join(",");
35
36        self
37            .get("https://api.bilibili.com/x/polymer/web-dynamic/v1/name-to-uid")
38            .query(&[("names", names_str)])
39            .send_bpi("用户名查 UID").await
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use tracing::info;
47
48    #[tokio::test]
49    async fn test_user_name_to_uid() {
50        let bpi = BpiClient::new();
51        let resp = bpi.user_name_to_uid(&["LexBurner", "某科学"]).await;
52        assert!(resp.is_ok());
53        if let Ok(r) = resp {
54            info!("用户名查 UID 返回: {:?}", r);
55        }
56    }
57}