1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
2use serde::{ Deserialize, Serialize };
3
4#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct DefaultSearchData {
7 pub seid: String,
9 pub id: u64,
11 pub r#type: u32,
13 pub show_name: String,
15 pub name: Option<String>,
17 pub goto_type: u32,
19 pub goto_value: String,
21 pub url: String,
23}
24
25#[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 live_id: Option<Vec<serde_json::Value>>,
38 }
40
41#[derive(Debug, Clone, Deserialize, Serialize)]
43pub struct HotWordDataResponse {
44 pub code: u32,
45 pub list: Vec<HotWordItem>,
46}
47
48impl BpiClient {
49 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 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}