bpi_rs/search/
hot.rs

1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
2use serde::{ Deserialize, Serialize };
3
4/// 默认搜索内容
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct DefaultSearchData {
7    /// 搜索 seid
8    pub seid: String,
9    /// 默认搜索 id
10    pub id: u64,
11    /// 类型,固定 0
12    pub r#type: u32,
13    /// 显示文字
14    pub show_name: String,
15    /// 空字段
16    pub name: Option<String>,
17    /// 跳转类型,1: 视频
18    pub goto_type: u32,
19    /// 搜索目标 id,视频为稿件 avid
20    pub goto_value: String,
21    /// 搜索目标跳转 url
22    pub url: String,
23}
24
25/// 热搜条目
26#[derive(Debug, Clone, Deserialize, Serialize)]
27pub struct HotWordItem {
28    pub hot_id: u64,
29    pub keyword: String,
30    pub show_name: String,
31
32    pub heat_score: u64,
33    pub word_type: u32,
34
35    // pub icon: Option<String>,
36    // pub resource_id: Option<u64>,
37    pub live_id: Option<Vec<serde_json::Value>>,
38    // pub name_type: Option<String>,
39}
40
41/// 热搜返回数据
42#[derive(Debug, Clone, Deserialize, Serialize)]
43pub struct HotWordDataResponse {
44    pub code: u32,
45    pub list: Vec<HotWordItem>,
46}
47
48impl BpiClient {
49    /// 获取默认搜索内容(web端)
50    ///
51    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/search
52    pub async fn search_default(&self) -> Result<BpiResponse<DefaultSearchData>, BpiError> {
53        let signed_params = self.get_wbi_sign2(vec![("foo", "bar")]).await?;
54
55        self
56            .get("https://api.bilibili.com/x/web-interface/wbi/search/default")
57            .query(&signed_params)
58            .send_bpi("获取默认搜索内容").await
59    }
60
61    /// 获取热搜列表(web端)
62    ///
63    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/search
64    ///
65    /// - 无参数
66    pub async fn search_hotwords(&self) -> Result<BpiResponse<HotWordDataResponse>, BpiError> {
67        let response = self.get("https://s.search.bilibili.com/main/hotword").send().await?;
68
69        let data: HotWordDataResponse = response.json().await?;
70
71        let resp: BpiResponse<HotWordDataResponse> = BpiResponse {
72            code: 0,
73            data: Some(data),
74            message: "".to_string(),
75            status: false,
76        };
77
78        Ok(resp)
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[tokio::test]
87    async fn test_get_default_search() -> Result<(), Box<BpiError>> {
88        let bpi = BpiClient::new();
89        let result = bpi.search_default().await?;
90        let data = result.into_data()?;
91        tracing::info!("{:#?}", data);
92
93        Ok(())
94    }
95
96    #[tokio::test]
97    async fn test_get_hotword_list() -> Result<(), Box<BpiError>> {
98        let bpi = BpiClient::new();
99        let result = bpi.search_hotwords().await?;
100        let data = result.into_data()?;
101        tracing::info!("{:#?}", data);
102
103        Ok(())
104    }
105}