use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
use serde::{ Deserialize, Serialize };
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DefaultSearchData {
pub seid: String,
pub id: u64,
pub r#type: u32,
pub show_name: String,
pub name: Option<String>,
pub goto_type: u32,
pub goto_value: String,
pub url: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HotWordItem {
pub hot_id: u64,
pub keyword: String,
pub show_name: String,
pub heat_score: u64,
pub word_type: u32,
pub live_id: Option<Vec<serde_json::Value>>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HotWordDataResponse {
pub code: u32,
pub list: Vec<HotWordItem>,
}
impl BpiClient {
pub async fn search_default(&self) -> Result<BpiResponse<DefaultSearchData>, BpiError> {
let signed_params = self.get_wbi_sign2(vec![("foo", "bar")]).await?;
self
.get("https://api.bilibili.com/x/web-interface/wbi/search/default")
.query(&signed_params)
.send_bpi("获取默认搜索内容").await
}
pub async fn search_hotwords(&self) -> Result<BpiResponse<HotWordDataResponse>, BpiError> {
let response = self.get("https://s.search.bilibili.com/main/hotword").send().await?;
let data: HotWordDataResponse = response.json().await?;
let resp: BpiResponse<HotWordDataResponse> = BpiResponse {
code: 0,
data: Some(data),
message: "".to_string(),
status: false,
};
Ok(resp)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_get_default_search() -> Result<(), Box<BpiError>> {
let bpi = BpiClient::new();
let result = bpi.search_default().await?;
let data = result.into_data()?;
tracing::info!("{:#?}", data);
Ok(())
}
#[tokio::test]
async fn test_get_hotword_list() -> Result<(), Box<BpiError>> {
let bpi = BpiClient::new();
let result = bpi.search_hotwords().await?;
let data = result.into_data()?;
tracing::info!("{:#?}", data);
Ok(())
}
}