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> {
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 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}