Skip to main content

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    /// # 文档
52    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/search)
53    pub async fn search_default(&self) -> Result<BpiResponse<DefaultSearchData>, BpiError> {
54        let signed_params = self.get_wbi_sign2(vec![("foo", "bar")]).await?;
55
56        self
57            .get("https://api.bilibili.com/x/web-interface/wbi/search/default")
58            .query(&signed_params)
59            .send_bpi("获取默认搜索内容").await
60    }
61
62    /// 获取热搜列表(web端)
63    ///
64    /// # 文档
65    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/search)
66    ///
67    /// - 无参数
68    pub async fn search_hotwords(&self) -> Result<BpiResponse<HotWordDataResponse>, BpiError> {
69        let response = self.get("https://s.search.bilibili.com/main/hotword").send().await?;
70
71        let data: HotWordDataResponse = response.json().await?;
72
73        let resp: BpiResponse<HotWordDataResponse> = BpiResponse {
74            code: 0,
75            data: Some(data),
76            message: "".to_string(),
77            status: false,
78        };
79
80        Ok(resp)
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[tokio::test]
89    async fn test_get_default_search() -> Result<(), Box<BpiError>> {
90        let bpi = BpiClient::new();
91        let result = bpi.search_default().await?;
92        let data = result.into_data()?;
93        tracing::info!("{:#?}", data);
94
95        Ok(())
96    }
97
98    #[tokio::test]
99    async fn test_get_hotword_list() -> Result<(), Box<BpiError>> {
100        let bpi = BpiClient::new();
101        let result = bpi.search_hotwords().await?;
102        let data = result.into_data()?;
103        tracing::info!("{:#?}", data);
104
105        Ok(())
106    }
107}